3 ms·
Can you write a well-defined C program that reads uninitialised memory?
by chrisseaton 7y ago
Can you write a well-defined C program that reads uninitialised memory?
- dooglius 7y agoNot portably, you could of course use inline assembly in practice, but the normal way of doing it is UB.
- BenjiWiebe 7y agoWouldn't this do it? int a; printf("%d", (&a + sizeof(int)));
- chrisseaton 7y ago...but that isn't a well-defined program, is it?
- saagarjha 7y agoReads? I think so; I believe the bad part is when you try to use it.
- chrisseaton 7y agoNo, it is undefined behaviour. Check the spec.
- saagarjha 7y agoI couldn't find the verbiage for that. The standard seems to have this under undefined behavior: > The value of an object with automatic storage duration is used while it is indeterminate. But reading the value without using it seems fine?
- chrisseaton 7y agoThe 'object' in this context is the storage location, not the value you read from it. You're 'using' the 'object' when you read from it.
- deleted 7y ago[deleted]
- saagarjha 7y agoOh, not that kind of read. I meant "read" in the context of "without actually using its value in a way that affects your program execution"–not, in what I believe the C++ term is–an ODR use. As in here: https://news.ycombinator.com/item?id=22740582 https://news.ycombinator.com/item?id=22740582