+ 1

NULL vs 0

Scenario: I type in “int x;” into a C++ program, and then run the program. Will x be assigned to value 0, or value NULL?

4th Dec 2019, 3:36 PM
OnesAndZeroes10
OnesAndZeroes10 - avatar
3 Respostas
+ 2
x will neither be 0 and nor be NULL, it will have a garbage value. Edit: If you try to access a variable that is not initialized then it will lead to undefined behaviour.
4th Dec 2019, 3:52 PM
Avinesh
Avinesh - avatar
+ 1
Whether value initialization takes place or not is contextual. Where you declare the variable, whether or not you add specifier (e.g. volatile or static) matters. Even there is no one voice on this doubt as some compilers behave differently. See the following thread and read the answer by ~swim~. This subject is something I'm honestly still struggling to get over with. https://www.sololearn.com/Discuss/1128723/?ref=app
5th Dec 2019, 1:19 AM
Ipang
+ 1
And NULL is pointer related value. NULL should never be assigned to non pointer type. my understanding of NULL is that it is a pointer that doesn't contain a valid usable address (which obviously contains valid data). By using NULL in comparison we check whether a pointer is good (it points to a valid memory address). By assigning NULL (or nullptr in C++) to a pointer, we are saying that the said pointer isn't pointing to a valid address, hence it should not be used because there's no valid data to dereference (read from that address). Pointer subject is big. So please correct me if I misunderstood something or I made a wrong point in my explanation. You will be doing me a big favour and helping me to advance by your correction 👍
5th Dec 2019, 1:31 AM
Ipang