5 ms·
>inability to read uninitialized memory printf("%x", *(int*)0x12345678);
by gruez 7y ago
>inability to read uninitialized memory
printf("%x", *(int*)0x12345678);
- deleted 7y ago[deleted]
- chrisseaton 7y agoThis is not a well-defined C program though.
- fennecfoxen 7y agoAt some point, the question of "well-defined C program" is philosophical. Let's get down to brass tacks: If someone sat down and wrote this, would it happily compile? Would it compile under some compilers and configurations but not others? Would it throw warnings instead of errors under some configurations? Which of these configurations are the default configurations? Which non-default configurations are considered best practices? How complex are the best practices, and how widespread is knowledge about them? In short, how much does the concept of a "well-defined C program" differ from the concept of a "C program" as implemented in practice? Can we say that most C programs, or even a medium-sized percentage of C programs, are well-defined?
- deleted 7y ago[deleted]
- overgard 7y agoMost C programs are indeed not well-defined. Probably most of them.
- hexane360 7y agoEven casting 0x12345678 to int* is undefined behavior
- icedchai 7y agoThis happens all the time on embedded systems, when you're writing a device driver, etc. Practically speaking, it's well defined what will happen.
- NobodyNada 7y agoIt's implementation-defined, not undefined (see [0]). That means the behavior is well-defined by your implementation rather than by the C standard, so the code may work on one implementation but not on others. [0]: https://stackoverflow.com/q/2397984/3476191 https://stackoverflow.com/q/2397984/3476191
- nkurz 7y agoWhat makes you sure of this? I'm reasonably familiar with modern C, but I don't feel confident of the answer here. A search of Stackoverflow doesn't bring up anything that seems authoritative for C. The most relevant quotation I can find is in the Rational for C99, where Section 6.3.2.3 has: Implicit in the Standard is the notion of invalid pointers. In discussing pointers, the Standard typically refers to “a pointer to an object” or “a pointer to a function” or “a null pointer.” A special case in address arithmetic allows for a pointer to just past the end of an array. Any other pointer is invalid. An invalid pointer might be created in several ways. An arbitrary value can be assigned (via a cast) to a pointer variable. (This could even create a valid pointer, depending on the value.) A pointer to an object becomes invalid if the memory containing the object is deallocated or moved by realloc. Pointer arithmetic can produce pointers outside the range of an array. Regardless how an invalid pointer is created, any use of it yields undefined behavior. Even assignment, comparison with a null pointer constant, or comparison with itself, might on some systems result in an exception. I'm not a language lawyer, but I suspect this means that even initialization to the wrong literal might well be undefined behavior. What makes you confident that it's not, and instead is merely implementation defined?
- icedchai 7y agoI found this: https://stackoverflow.com/questions/51083356/does-the-c-standard-permit-assigning-an-arbitrary-value-to-a-pointer-and-increme https://stackoverflow.com/questions/51083356/does-the-c-stan... What if it's not an "invalid pointer", but a pointer to a memory-mapped IO address, ROM, etc? I grew up learning C on 16-bit machines in the early 90's. Hard coded pointer values were very, very common.