473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

What does a NULL pointer do?

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
1 2242
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
0
by: Bruce W.1 | last post by:
It's there on the Visual Studio toolbar. The framework docs and a Usenet search turned up nothing. What is the Pointer Web Server Control? What does it do? Thanks for your help.
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
5
by: Niu Xiao | last post by:
I saw a lot of codes like: void foo(void* arg) void bar(void** arg) f((void*)p) but what does void pointer mean in c? I just know it stands for generic pointer. thanks.
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.