|
Blotto Skorzany posted:You can't just interpret any random integer value as an address without a cast in ANSI C, and even with a cast there are some caveats (cf. the C FAQ # 4.14 and 4.5). You can take the address of any normal int that you declare because every variable declared with automatic storage duration (a normally declared variable; don't worry too much about what 'storage duration' means at this point) can be treated as if it has an address somewhere, and in fact probably does live on the stack. That FAQ is a bit outdated, you can assume that a pointer can roundtrip between an integer in every single non-joke architecture, and most of the jokes as well. (Specifically, any implementation that defines intptr_t). There are some additional edge cases around accessing the zero page (i.e. you essentially can't in C in a standards compliant way), but doing stuff with the zero page is mostly pointless in the modern era anyway. Also, this isn't disagreeing with you, but a sufficiently smart compiler can avoid spilling a variable to the stack even if you take its address, as long as it can prove that the pointer you get from it never escapes. b0lt fucked around with this message at 02:49 on Sep 9, 2015 |
# ? Sep 9, 2015 02:44 |
|
|
# ? May 15, 2024 04:15 |
|
If you want to deref an int as if it were a pointer you'd have to do some nonsense like:code:
But you'll get a compiler warning and it's almost never a good idea. Best case it segfaults, worst case the entire program is a huge security bug of some sort. I'm sure there's some case where it may be useful, but the examples mostly consist of "C pub trivia" questions. Maybe you could do something of the sort with embedded devices, but if you're really dead set on accessing things via integer arithmetic you may as well bite the bullet and write that function in pure assembly at that point. Linear Zoetrope fucked around with this message at 02:54 on Sep 9, 2015 |
# ? Sep 9, 2015 02:51 |
|
If you're on an embedded system you might want to do something like this:code:
And perhaps you'd want to manipulate a pointer in some way, in which case you'd do this: code:
Again, mostly useful on embedded systems where it may be important to work with where things actually are in memory.
|
# ? Sep 9, 2015 03:19 |
|
Jsor posted:If you want to deref an int as if it were a pointer you'd have to do some nonsense like: *(int*)(intptr_t)x is an easier method that's at least as standards compliant. Another common idiom in C is to have an API along the lines of void do_something(callback_fn_t callback, void *data), where the callback function receives the data argument passed in earlier. It's not strictly compliant to cast a regular integer to a pointer and pass it in, but no compiler or platform would hate you enough to make it not work.
|
# ? Sep 9, 2015 03:47 |
|
Pollyanna posted:
It's not. Much like you use int foo = 13; to have a variable containing the integer value 13, you say int *foo = 0x10 to have a variable containing the pointer value 0x10. Pollyanna posted:I don't quite understand why it doesn't compile. I expected to get the value of whatever was at memory address 0x01, which I expect to just be some random value. Instead, it's invalid. What exactly does *thing return here? In the same way you can't do int foo = "bar"; because strings are not integers, you also can't do int *foo = 0x10; because C's int type is not the same as a pointer. As for what's at C address 0x10, who knows! On most systems, it will crash. This is because we've collectively considered it useful behavior to have accessing a NULL pointer crash the program, the NULL pointer is equivalent to the memory address at 0x0 on most systems, and for reasons you'll learn about later, the CPU considers memory in chunks called "pages". So, if 0x0 will crash the program, so will 0x10. That said, I have seen embedded systems where they're pushing the system to the limit, and they need the extra space in memory, so dereferencing 0x0 works fine. As for what points there, it's up to the system. Once you get a better handle on pointers, we can tell you about the difference between virtual address space, physical memory, and how one maps to the other.
|
# ? Sep 9, 2015 04:55 |
|
Suspicious Dish posted:That said, I have seen embedded systems where they're pushing the system to the limit, and they need the extra space in memory, so dereferencing 0x0 works fine. Also really ancient Unices. Ultrix anyone?
|
# ? Sep 9, 2015 14:23 |
|
Pollyanna are you by chance studying C with "Learn C The Hard Way"?
|
# ? Sep 9, 2015 15:11 |
|
Jsor posted:Maybe you could do something of the sort with embedded devices, but if you're really dead set on accessing things via integer arithmetic you may as well bite the bullet and write that function in pure assembly at that point. If you're writing applications code then doing integer arithmetic on pointers is quite likely out of scope and something you shouldn't do. I'd probably go further and say you simply shouldn't write applications in C.
|
# ? Sep 9, 2015 17:54 |
|
ExcessBLarg! posted:It's not an uncommon thing to do in low-level kernel code, particularly memory managers or anything interacting with an MMU. Yes, you could write these functions in assembly, but the whole purpose of C originally was to provide a higher-level structured language to replace assembly language (particularly in the UNIX kernel) for this very kind of code. I agree wholeheartedly. After learning about how all of C's features are so awesome and magical for OSes, and trying to implement various bits and pieces of a Unix analogue, I don't think I'd ever dream of trying to do day-to-day software development in C. That pain is only worth it if you actually need to do things at a low level.
|
# ? Sep 9, 2015 19:18 |
|
Is there an easy shell way (in linux) to compare 2 directories and copy the outlier files to a 3rd directory based on filename only. I know file contents will differ but I just need the outliers. eg: big_images/001.jpg big_images/003.jpg small_images/001.jpg small_images/002.jpg <- copy/link this file to some other dir small_images/003.jpg I'm talking about ~150k files in each dir with about 5-10% outliers.
|
# ? Sep 9, 2015 22:38 |
|
Part of the reason I'm learning C is because I want to do things with some Arduinos and a Raspberry Pi I've got lying around, so why not dip my toe in the embedded domain while I'm at it?Symbolic Butt posted:Pollyanna are you by chance studying C with "Learn C The Hard Way"? Yes I am! Holy loving jesus. At least Valgrind actually works on recent OSX versions now. LeftistMuslimObama posted:I agree wholeheartedly. After learning about how all of C's features are so awesome and magical for OSes, and trying to implement various bits and pieces of a Unix analogue, I don't think I'd ever dream of trying to do day-to-day software development in C. That pain is only worth it if you actually need to do things at a low level. I read somewhere that larger, more abstract applications are best implemented with higher level languages like Ruby or Python. Languages like C are great for lower-level details like the smaller programs used to interface with things like network ports. The C programs are almost like implementation details. That does make me wonder how you get a Ruby program to start up a C program, and write that program such that it could potentially be used by a Java program. Someone should make a common program communication interface, like the Unix pipe.
|
# ? Sep 9, 2015 22:52 |
|
Shaocaholica posted:Is there an easy shell way (in linux) to compare 2 directories and copy the outlier files to a 3rd directory based on filename only. I know file contents will differ but I just need the outliers. If your folders are "big" and "small" then code:
code:
code:
code:
|
# ? Sep 9, 2015 23:21 |
|
Pollyanna posted:Someone should make a common program communication interface, like the Unix pipe. Several dozen someones have! The concept you're getting at is called remote procedure call or, generically, inter-process communication.
|
# ? Sep 9, 2015 23:43 |
|
Pollyanna posted:I read somewhere that larger, more abstract applications are best implemented with higher level languages like Ruby or Python. Ruby and Python are generally better suited for "programming in the small", prototyping, and building concise and functional applications quickly, but aren't necessarily geared towards long-term projects with many potential maintainers. It's exactly the features of Ruby and Python that make it wonderful for quick stuff (duck typing, general expressiveness, etc.) that are at odds with large applications (static typing, strictly defined interfaces, etc.). Of course, you could write applications of all sizes in either, and folks do use Java for prototyping or Ruby for large applications. But that's the target audience. Pollyanna posted:Languages like C are great for lower-level details like the smaller programs used to interface with things like network ports. Networking code can be written in any language these days. At the application layer, C (e.g., POSIX libc)'s offering aren't very different from other languages. The Sockets API is also somewhat painful. Lower-level networking code (TCP/IP, Ethernet, etc.) is pretty much all written in C and done in the kernel. Pollyanna posted:That does make me wonder how you get a Ruby program to start up a C program, and write that program such that it could potentially be used by a Java program. Pollyanna posted:Someone should make a common program communication interface, like the Unix pipe. ExcessBLarg! fucked around with this message at 00:15 on Sep 10, 2015 |
# ? Sep 10, 2015 00:11 |
|
If you give somebody a socket of bytes, they will invent a message passing scheme on top of it. The new hotness is protobuf. Excuse me, flatbuffers. Excuse me, cap'n'proto.
|
# ? Sep 10, 2015 00:29 |
|
I'm pretty sure at my last job at some point we were sending JSON over Protobuf. I think now they're serializing protobufs to JSON.
|
# ? Sep 10, 2015 00:44 |
|
feedmegin posted:Ultrix anyone? No thank you.
|
# ? Sep 10, 2015 01:48 |
|
Nippashish posted:If your folders are "big" and "small" then Thanks. Ended up using a hokey way and I forgot to mention I only needed the outliers in one direction (in small). code:
|
# ? Sep 10, 2015 06:05 |
ExcessBLarg! posted:"Larger" applications are probably "best" implemented with Java or C# as those languages were designed to target the space of long-lived, maintainable, enterprise-class applications. Thanks for this writeup. I've been learning how to program mostly in C for the past ~2 months because I want to learn more about how a computer actually works before using higher level features. Although I don't regret doing that it was painful at times, like trying to write an assembler in C with the standard C library. I was skeptical at first about higher level languages but I now understand that the extra performance overhead can definitely be worth it depending on what you're trying to accomplish. Now I'm reading up on C# for when I want to do something that's hard to do in C. My programmer friend jokingly calls me a masochist for leaning how to program at a (very) low level first but I'm a stubborn fool like that.
|
|
# ? Sep 10, 2015 08:44 |
|
Ekster posted:Thanks for this writeup. I've been learning how to program mostly in C for the past ~2 months because I want to learn more about how a computer actually works before using higher level features. Although I don't regret doing that it was painful at times, like trying to write an assembler in C with the standard C library. I was skeptical at first about higher level languages but I now understand that the extra performance overhead can definitely be worth it depending on what you're trying to accomplish. Now I'm reading up on C# for when I want to do something that's hard to do in C. You may be interested in NAND 2 Tetris which starts you off with a virtual hardware simulator, and has you build up from basic logic gates/hardware, to assembly, ultimately to a Java/C#-like language where you write Tetris.
|
# ? Sep 10, 2015 08:50 |
Jsor posted:You may be interested in NAND 2 Tetris which starts you off with a virtual hardware simulator, and has you build up from basic logic gates/hardware, to assembly, ultimately to a Java/C#-like language where you write Tetris. I'm actually following that course right now, the assembler I wrote was part of an exercise. It's really good and I highly recommend it. I got side-tracked for a while when I needed to implement a hashmap in C but I've decided to continue the course in C# and learn how to implement algorithms/data structures later for now.
|
|
# ? Sep 10, 2015 08:56 |
|
Ekster posted:Although I don't regret doing that it was painful at times, like trying to write an assembler in C with the standard C library.
|
# ? Sep 10, 2015 15:16 |
|
Hi everyone! First and foremost thank you for all of the help, it was much appreciated!! Just a quick update I kind of figured out everything and stumbled on some good solutions. I remembered I had a contact over in SF and asked him what his group uses to program, and they use IntelliJ and the company gets a discount for *reasons*. I asked for pricing and I can get a license through the company for PyCharm, so I got a hold of the community edition, and through a series of fun events got someone to install the interpreter. If/when I design anything of actual value, I can show them what was created then get cost justification ez pz. I'm going through treehouse lessons and Python is like incredibly intuitive and I love it! I can actually understand what I'm programming and I'm happy with that. So as frustrating as it was for all of you, thank you for the help.
|
# ? Sep 10, 2015 16:05 |
|
python is great, it's what I first learned in high school (well, beyond a little Qbasic) and also what the intro programming class at my uni used
|
# ? Sep 10, 2015 16:40 |
|
I'm looking for a Geo Ip provider that will give me the company name a visitor to my site is coming from. Like, if someone who is working at IBM hits my site, I want to be able to know they are at IBM. So far I've found this https://www.maxmind.com/en/geoip2-services-and-databases, which appears to do what I'm looking for ( but doesn't come up with my company when I browse from work, and I work for a mid-sized company), but was curious as to whether or not anyone knows of any alternatives?
|
# ? Sep 10, 2015 19:32 |
|
Could you use a high-volume whois service and parse out the orgname line? For example:code:
code:
dougdrums fucked around with this message at 21:05 on Sep 10, 2015 |
# ? Sep 10, 2015 20:11 |
|
I'm looking for more of something that maps network ips to company names. Like my example, I need to know if someone who works at IBM and is on their network is looking at my site, if that makes sense. I know Hubspot and Marketo offer something like this. I want to do what they are doing.
|
# ? Sep 10, 2015 22:26 |
Am I the only person who doesn't immediately see what gong on when I look at code like this?code:
|
|
# ? Sep 10, 2015 22:48 |
|
Centripetal Horse posted:Am I the only person who doesn't immediately see what gong on when I look at code like this? Nobody should be expected to immediately grasp it, that's code golf and should be refactored.
|
# ? Sep 10, 2015 23:05 |
|
Centripetal Horse posted:Am I the only person who doesn't immediately see what gong on when I look at code like this? I don't know what language that is, but is that using a variable named C and another named c?
|
# ? Sep 10, 2015 23:07 |
|
I'm guessing JavaScript? based on the usage of bitwise-or and lack of semicolon
|
# ? Sep 10, 2015 23:14 |
|
I was thinking it couldn't be a bitwise or in whatever language it was, because (anything|0) is a no-op. (In fact, I think on some assemblers, the nop instruction is literally a macro for OR reg, 0).
|
# ? Sep 10, 2015 23:29 |
|
idontcare posted:I'm looking for more of something that maps network ips to company names. Like my example, I need to know if someone who works at IBM and is on their network is looking at my site, if that makes sense. I know Hubspot and Marketo offer something like this. I want to do what they are doing. I'm not sure I understand what you're looking for? The only conceivable way I can think of is to pull the data from a registry, because they're the only source for it as far as I know. I'm so stumped, I actually called Marketo (Hubspot gave me a machine) and asked your exact question and they told me they provide no such product. Looking at MaxMind again (https://www.maxmind.com/en/geoip2-isp-database), it looks like they do it nearly the same way. Actually I think I might understand you, are you looking to see if a user's company is statistically identifiable by way of cookies and whatnot? I.E. user x has "works at IBM" on facebook and tweets about watson? dougdrums fucked around with this message at 23:52 on Sep 10, 2015 |
# ? Sep 10, 2015 23:39 |
|
Jsor posted:I was thinking it couldn't be a bitwise or in whatever language it was, because (anything|0) is a no-op. (In fact, I think on some assemblers, the nop instruction is literally a macro for OR reg, 0).
|
# ? Sep 10, 2015 23:41 |
|
Sedro posted:In JS you can use x | 0 and x >> 0 to C-style cast some number x to an integer Well that's... something
|
# ? Sep 10, 2015 23:44 |
No Safe Word posted:Nobody should be expected to immediately grasp it, that's code golf and should be refactored. So, maybe it's not just me? I'd really like for it to not be that I really am just that bad at reading and parsing code. Fergus Mac Roich posted:I don't know what language that is, but is that using a variable named C and another named c? Sedro posted:I'm guessing JavaScript? based on the usage of bitwise-or and lack of semicolon It is Javascript. Yeah, that's two variables using the same letter. The idea was to write the code in as few characters as possible, and the concepts being represented by those variables both start with "c". Sedro posted:In JS you can use x | 0 and x >> 0 to C-style cast some number x to an integer I haven't touched Javascript in years, but that was actually the easiest part for me to understand. "Oh, I see. That's just making sure it's an int."
|
|
# ? Sep 11, 2015 00:10 |
|
If you are asking if people can understand that by reading in the normal, natural way, no. But, not knowing JS, I'll claim that you should probably be able to figure out what it does with a close examination. I assume this is a game or contest.
|
# ? Sep 11, 2015 01:26 |
|
Centripetal Horse posted:I haven't touched Javascript in years, but that was actually the easiest part for me to understand. "Oh, I see. That's just making sure it's an int." I thought the ternary operator was the easy bit, it's checking C against N and returning a fixed tuple if that fails or computing the real vector. What's eating me is C being referenced in the same line as --C. What does a javascript call a sequence point?
|
# ? Sep 11, 2015 01:38 |
Fergus Mac Roich posted:If you are asking if people can understand that by reading in the normal, natural way, no. But, not knowing JS, I'll claim that you should probably be able to figure out what it does with a close examination. I assume this is a game or contest. I wasn't asking if I should be able to read it natural-language style. I think the number of people who can do that is quite small. I was able to figure out what it does. I can always figure out what other people's code is doing, but I sometimes wonder if it takes me longer than it should. I've worked with numerous coders who were so competent that it was almost frightening. I've written hundreds of thousands of lines of code across countless projects, all of which do what I want them to do, but I see poo poo like that snippet, and I think, "That didn't even occur to me, but I'll bet Bob would have thought of that." The code in question was the answer to a challenge on http://www.codefights.com/ JawnV6 posted:What's eating me is C being referenced in the same line as --C. What does a javascript call a sequence point? I don't know, but C is definitely decremented before the ? : is evaluated. Also, I just tested, and it doesn't matter if the -- operator precedes or succeeds the variable. You're right, the ternary operator was really the most obvious bit of that code. I wasn't including that, because it's super-simple, and I don't think anyone would consider that when judging how hard it is to evaluate that bit of code.
|
|
# ? Sep 11, 2015 02:47 |
|
|
# ? May 15, 2024 04:15 |
|
Centripetal Horse posted:I wasn't asking if I should be able to read it natural-language style. I think the number of people who can do that is quite small. I was able to figure out what it does. I can always figure out what other people's code is doing, but I sometimes wonder if it takes me longer than it should. I've worked with numerous coders who were so competent that it was almost frightening. I've written hundreds of thousands of lines of code across countless projects, all of which do what I want them to do, but I see poo poo like that snippet, and I think, "That didn't even occur to me, but I'll bet Bob would have thought of that." Code golf is a special niche where you use all the language tricks and weird edge cases you've picked up to cram the functionality/behaviour you need into the fewest characters possible. Its goals are pretty much the opposite of being universally clear and readable, and it encourages the use of all kinds of esoteric hacks that the average person might be completely unaware of I mean there's nothing wrong with that as a challenge, but I wouldn't worry if you can't unpack something so condensed and potentially weird. There's a reason we don't write real code like that, and why clear behaviour is preferred over clever fuckery, or the fuckery is explained in documentation if there's an actual benefit to it being there You'll get better at reading this kind of thing as you get more familiar with it, either through learning the language and its toolbox or from playing code golf yourself (and probably more from the latter). I guess the more important question is, once you unpack what the hell is going on in there with a bit of whitespace and readable names, does it make sense? Do you understand how it works and what tricks are being used? Or if you look up the tricks, do you feel like you should have got it, or do you go '...the hell is that about?'
|
# ? Sep 11, 2015 03:20 |