473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL pointer and zero value

vp
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions ;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like

memset( garMF, 0, sizeof(ModuleFu nctions)*N );
Thanks for your help,

DT
Nov 14 '05 #1
31 3538
vp wrote:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"


No. NULL is not necessarily all-bits-zero.

Your technique will, however, work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL. If you like
programming-by-casino, go ahead. :-)

<snip>

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #2
Richard Heathfield <in*****@addres s.co.uk.invalid > spoke thus:
(all-bits 0 as NULL post)

If you like programming-by-casino, go ahead. :-)


So the house wins on NULL and double NULL? ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
dt*******@yahoo .com (vp) writes:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"
No. The former statement sets all bits of `p' to zero, which is not
necessarily a representation of the null pointer.
The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions ;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like

memset( garMF, 0, sizeof(ModuleFu nctions)*N );


If `garMF' has static storage duration, it is automatically initialized to
zero if you don't initialize it explicitly. "Initialize d to zero" means
that each non-compound object is initialized as if you assigned `0' to it
(i.e. pointers are initialized to null pointers). For compound objects,
its members or elements are recursively initialized in this way.

Otherwise (if `garMF' doesn't have static storage duration) you'll have
to loop.

Martin
Nov 14 '05 #4
Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in
news:3f******@n ews2.power.net. uk:
No. NULL is not necessarily all-bits-zero.

Your technique will, however, work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL. If you like
programming-by-casino, go ahead. :-)


So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */
{
free(pVar);
}

return EXIT_SUCCESS;
}

--
- Mark ->
--
Nov 14 '05 #5
Martin Dickopp wrote:

dt*******@yahoo .com (vp) writes:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"
No. The former statement sets all bits of `p' to zero, which is not
necessarily a representation of the null pointer.
The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions ;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction
for each member of array, is it correct to do something like

memset( garMF, 0, sizeof(ModuleFu nctions)*N );

Otherwise (if `garMF' doesn't have static storage duration)
you'll have to loop.


I disagree about the necessity of a loop.

ModuleFunction garMF[N] = {NULL};

--
pete
Nov 14 '05 #6
Mark A. Odell wrote:

Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in
news:3f******@n ews2.power.net. uk:
No. NULL is not necessarily all-bits-zero.

Your technique will, however,
work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL.
If you like programming-by-casino, go ahead. :-)


So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */


Your code does not check to see if all bits are zero.

Your question and corresponding code example
are not relevant to what Richard Heathfield said.

--
pete
Nov 14 '05 #7
"Mark A. Odell" <no****@embedde dfw.com> writes:
Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in
news:3f******@n ews2.power.net. uk:
No. NULL is not necessarily all-bits-zero.

Your technique will, however, work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL. If you like
programming-by-casino, go ahead. :-)


So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */
{
free(pVar);
}

return EXIT_SUCCESS;
}


The program is correct (i.e. strictly conforming). The `if' statement
doesn't look at the representation (i.e. the bits) of `pVar', it simply
compares `pVar' to `0'. A comparison between an expression of pointer
type and `0', which is a null pointer constant in such a comparison, is
valid.

Martin
Nov 14 '05 #8
pete wrote:

Mark A. Odell wrote:

Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in
news:3f******@n ews2.power.net. uk:
No. NULL is not necessarily all-bits-zero.

Your technique will, however,
work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL.
If you like programming-by-casino, go ahead. :-)


So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */


Your code does not check to see if all bits are zero.

Your question and corresponding code example
are not relevant to what Richard Heathfield said.


The question is relevant, the answer is "no".

--
pete
Nov 14 '05 #9
vp wrote:

If I have a pointer char * p, is it correct to assign NULL to
this pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"


NO.

While it will often "work" on a particular system, it is not
portable and can lead to very mysterious bugs.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

102
6070
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
29
3771
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
64
3956
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
69
5604
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 assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
20
3804
by: Quantum | last post by:
Hi, Are these equivalent: char text; if(text==NULL){} if(text==0){} Thank you,
46
3694
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
15
3775
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer bit-pattern. My question is if a program refers to that specific value which is used by the machine to refer to null pointer, how compiler treats that?.
20
3235
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
17
4458
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized to 0. ANSI C guarantees that. Technically speaking, static variables go into the .bss ELF segment, while "static int foo=0" goes into .data. Because .bss is zero filled by the OS, it does not need to be in the actual binary. So it is in fact...
28
1878
by: rahul | last post by:
#include <stdio.h> int main (void) { char *p = NULL; printf ("%c\n", *p); return 0; } This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.