By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

What does a NULL pointer do?

P: 2
I am using the ReadFile() function in Visual C++. One of its parameter is a pointer which needs to point to a valid variable, but then it says that it can be NULL when another input flag parameter is set to certain state. So my question is, if I do not set the flag to that state, and still set the pointer to NULL, will this error being caught during compilation or it will just fail during runtime? There were some compiler I used before and it just set NULL pointer to zero, but zero is actually a valid memory address on that particular processor. Is there a standard way on how a NULL pointer is being treated?
Dec 10 '10 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
To start with NULL is a macro that normally evaluates to 0 in C++ or ((void*)0) (0 cast to void *) in C.

Both the C and C++ standards require that the compiler translate 0 used in a pointer context to the NULL pointer representation of the platform. A NULL pointer is a pointer that doesn't point anywhere, all platforms must have one but it is not required that it actually have the value 0 so if 0 is a valid memory address on the platform you use I would expect 0 used in a pointer context to be translated to something else by the compiler.

If the API you are using says that a functionparameter can be NULL if a flag is in certain state it means that the function will not use the passed pointer if that flag is set. If the flag is set then you can expect the function to try and use the pointer and if you passed NULL then you may well get some sort memory exception (SIG_SEGV). If you don't get a memory exception you will have certainly invoked undefined behaviour which is bad.

So in C and C++ programming you can always use 0 as the NULL pointer, which is necessary to allow portable programming, the compiler should sort it out for you on platforms where the actual NULL pointer value is something different.
Dec 10 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.