Sign in to follow this  
yurian

I don't understand what the = NULL means?

Recommended Posts

yurian    182
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?

Share this post


Link to post
Share on other sites
kiwibonga    183
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.

Share this post


Link to post
Share on other sites
gharen2    520
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.

Share this post


Link to post
Share on other sites
gekko    478
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.

Share this post


Link to post
Share on other sites
DvDmanDT    1941
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.

Share this post


Link to post
Share on other sites
kiwibonga    183
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

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
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 nothing
if (foo) *foo = 42; //safe -- the if's condition evaluates to false

int* foo; // pointer is UNINITIALIZED, not NULL
delete 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));

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this