I don't understand what the = NULL means?

Started by
6 comments, last by MaulingMonkey 15 years, 12 months ago
I am talking about things like BITMAP *bitmappicture = NULL; HDC hDc = NULL; I mean, when I compile this, nothing changes when I take out the = NULL; So I am wondering what it does?
Advertisement
NULL initializes it as NULL. If you don't do that, it'll initialize it to something random. When you run it from Visual Studio, it'll initialize it as zero, so you'll be fine if you access it before it's initialized... But if you try to run the exe directly, you'll start getting weird "access violation" errors you weren't getting before.
Quote:Original post by kiwibonga
NULL initializes it as NULL. If you don't do that, it'll initialize it to something random. When you run it from Visual Studio, it'll initialize it as zero, so you'll be fine if you access it before it's initialized... But if you try to run the exe directly, you'll start getting weird "access violation" errors you weren't getting before.


More accurately, everything is initialised to zero (NULL) in debug mode, but not release mode.
It's initializing the pointer to NULL. Nothing happens when you take it out because then it's a definition. It's no different than these:

int a = 0;
int b;

You can instantiate a pointer without initializing it, but it will point to some random value in memory.

But NULL itself is defined as follows:

C: #define NULL ((void*)0)
C++: #define NULL 0

In other words, in C it is a void pointer, and in C++ it's defined as 0.
-- gekko
In debug mode, VS (at least 2005) initializes pointers to 0xCCCCCCCC (or is it 0xCDCDCDCD?). I'm pretty sure Release mode doesn't initialize pointers automaticly. You should always initialize your pointers manually anyway.
In Visual Studio, all unititialised data is given the value 0xCCCCCCCC in debug mode. It doesn't in release mode.
NextWar: The Quest for Earth available now for Windows Phone 7.
It does for me in release mode (VS 2008)... Happened recently, too... I scratched my head for hours over what turned out to be a member variable I forgot to initialize :X
Null pointers are safe to delete and will evaluate to false if converted to bool:

int* foo = NULL;delete foo; //safe -- deleteing a null pointer does nothingif (foo) *foo = 42; //safe -- the if's condition evaluates to falseint* foo; // pointer is UNINITIALIZED, not NULLdelete foo; // UNSAFE -- probably explodes (tries to access unallocated memory)if (foo) *foo = 42; // UNSAFE -- probably explodes (tries to write to unallocated memory)




Fun fact I feel worth mentioning: Although "pointer = 0;" is guaranteed to null a pointer just like "pointer = NULL", the physical value isn't guaranteed to be 0. Among other things, it means these two ugly statements aren't guaranteed to properly null the pointer:

memset(&pointer,0,sizeof(pointer));
memset(&pointer_containing_structure,0,sizeof(pointer_containing_structure));

This topic is closed to new replies.

Advertisement