Search code examples
cpointersinitializationdereference

How to check if allocated memory is initialized or not


I am exposed to the pointer int* i, of which I only know its memory is allocated but am not sure it has been initialized to some integer or not.

If I try to deference it, what would happen? In other words, how should I check if it is initialized or not? If it is not, I'd like to assign an integer value to that address; otherwise I do nothing.


Solution

  • Define "initialized". There will always be some value, and there is no way you can tell whether that value is a garbage or an integer, because any 32 bits of garbage will yield some value. You can dereference it though, no problem.