Why can't I initialize a global variable to NULL?

M

moxm

I have a statement declares a globle variable like this :

char *pname = NULL;

Then I used splint to check the code, I got errors:

err.c:8:15: Global pname initialized to null value: pname = NULL
A reference with no null annotation is assigned or initialized to
NULL. Use
/*@null@*/ to declare the reference as a possibly null pointer. (Use
-nullassign to inhibit warning)
err.c:8:15: Global pname initialized to null value: char * pname = NULL
= NULL

What's that mean?

Any help is appreciated, thanks.
 
A

akarl

moxm said:
I have a statement declares a globle variable like this :

char *pname = NULL;

Then I used splint to check the code, I got errors:

err.c:8:15: Global pname initialized to null value: pname = NULL
A reference with no null annotation is assigned or initialized to
NULL. Use
/*@null@*/ to declare the reference as a possibly null pointer. (Use
-nullassign to inhibit warning)
err.c:8:15: Global pname initialized to null value: char * pname = NULL
= NULL

What's that mean?

Any help is appreciated, thanks.

In C all global pointer variables are guaranteed to be initialized to
NULL, so setting pname to NULL is overkill. That probably is what Splint
is trying to say (but in a strange way).

August
 
P

pete

That's a declaration and a definition,
but not a statement.
In C all global pointer variables are guaranteed to be initialized to
NULL,

.... unless the code initializes them to something else,
like an address constant.

/* BEGIN new.c */

int global;

int *pointer = &global;

int main(void)
{
return *pointer;
}

/* END new.c */
 
A

akarl

pete said:
That's a declaration and a definition,
but not a statement.




... unless the code initializes them to something else,
like an address constant.

Sigh...
 
M

Malcolm

moxm said:
I have a statement declares a globle variable like this :

char *pname = NULL;

Then I used splint to check the code, I got errors:

err.c:8:15: Global pname initialized to null value: pname = NULL
A reference with no null annotation is assigned or initialized to
NULL. Use
/*@null@*/ to declare the reference as a possibly null pointer. (Use
-nullassign to inhibit warning)
err.c:8:15: Global pname initialized to null value: char * pname = NULL
= NULL

What's that mean?

Any help is appreciated, thanks.
splint isn't a very good program for beginners.
You code is perfectly unexceptional and idiomatic C, but splint doesn't seem
to like it, probably on the basis that a global pointer with the value NULL
might cause problems later down the line if passed to printf() or similar.

The fact is that very often you do need pointer to point to NULL before you
have allocated a string to them. splint only makes an intelligent guess as
to where problems may be, it cannot see your program with a human eye to
tell whether you ahve made a mistake or not.
 
J

junky_fellow

akarl said:
In C all global pointer variables are guaranteed to be initialized to
NULL, so setting pname to NULL is overkill. That probably is what Splint
is trying to say (but in a strange way).

I have a doubt if all global unitialized pointer variables are
initialized with NULL. I think they are initialized with
"all bits zeros".
I have seen an unix implementation, where while
loading a page that belongs to .bss (gobal unitialized data) segment,
the page is initialized with 0 (all bits 0). May be on that
implementation NULL is "all bits zeros".
 
R

Richard Heathfield

I have a doubt if all global unitialized pointer variables are
initialized with NULL. I think they are initialized with
"all bits zeros".

You're incorrect. They are initialised with the default static initialiser
value for their type, which is 0, 0.0, 0.0f, NULL, or whatever. Not
all-bits-zero (although of course it may turn out that way anyway, but only
because all-bits-zero happens to be one very sensible way to implement a
zero value).
 
M

Michael Mair

I have a doubt if all global unitialized pointer variables are
initialized with NULL. I think they are initialized with
"all bits zeros".
I have seen an unix implementation, where while
loading a page that belongs to .bss (gobal unitialized data) segment,
the page is initialized with 0 (all bits 0). May be on that
implementation NULL is "all bits zeros".

The standard says that they are initialized as if initialized with
a null pointer constant (No, that is not the exact wording).
Get N869 and look it up yourself instead of telling us implementation
details.


-Michael
 
M

Michael Mair

Malcolm said:
splint isn't a very good program for beginners.
You code is perfectly unexceptional and idiomatic C, but splint doesn't seem
to like it, probably on the basis that a global pointer with the value NULL
might cause problems later down the line if passed to printf() or similar.

The fact is that very often you do need pointer to point to NULL before you
have allocated a string to them. splint only makes an intelligent guess as
to where problems may be, it cannot see your program with a human eye to
tell whether you ahve made a mistake or not.

<OT>
The splint manual sheds light on some of the reasons for some of
the messages; however, it is not intended for beginners, either.
If you intend to rely on splint, then you should consider feeding
splint the additional information it needs to be more effective:
If you mark the pointer as /*@null@*/, splint knows that it is
perfectly acceptable that this pointer holds a null pointer
constant.
</OT>

Cheers
Michael
 
M

Michael Mair

moxm said:
/*@null@*/
char *pname = NULL;

then splint will shut up it's mouth.

Please quote some context so that people who get your answer before
my message know what you are responding to -- especially since splint
is off-topic and my advice (and your above statement) may be wrong.

Cheers
Michael
 
A

akarl

moxm said:
/*@null@*/
char *pname = NULL;

then splint will shut up it's mouth.

....or if you (as mentioned) rely on the default initialization;

char *pname;


August
 
M

moxm

Michael said:
Please quote some context so that people who get your answer before
my message know what you are responding to -- especially since splint
is off-topic and my advice (and your above statement) may be wrong.

Thank you! I will take your advice.
 
P

Peter Ammon

akarl said:
...or if you (as mentioned) rely on the default initialization;

char *pname;


August

This would seem like a bug in splint. If a pointer that is not marked
as NULL-permitting is initialized to NULL, why should splint care if
it's explicit or implicit?
 
P

pete

Peter said:
This would seem like a bug in splint. If a pointer that is not marked
as NULL-permitting is initialized to NULL, why should splint care if
it's explicit or implicit?

One time I saw somebody initialize a global pointer to NULL,
and I told him that he didn't have to do that.
 
P

PRadyut

In my borland compiler 5.5.1 it shows 0 if the global variable is
undefined

For example the code below
/*-----new.c---------*/
#include <stdio.h>

int global;


int *pointer = &global;

int getData(void);
int main(void)
{
int test = NULL;

printf("%d, %d, %d", getData(), global, test);
return 0;
}

int getData(void)
{
return *pointer;
}

/*---EOF-------------*/
the o/p is 0, 0, 0
It shows(outputs) null if command line arguments are empty but not in
this case

Please can somebody please clarify

Thanks

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India
 
K

Keith Thompson

PRadyut said:
In my borland compiler 5.5.1 it shows 0 if the global variable is
undefined

For example the code below
/*-----new.c---------*/
#include <stdio.h>

int global;


int *pointer = &global;

int getData(void);
int main(void)
{
int test = NULL;

printf("%d, %d, %d", getData(), global, test);
return 0;
}

int getData(void)
{
return *pointer;
}

/*---EOF-------------*/
the o/p is 0, 0, 0
It shows(outputs) null if command line arguments are empty but not in
this case

Please can somebody please clarify

Please don't top-post. Your response goes below, or interspersed
with, any quoted material, which should be reduced to what's actually
necessary. The idea is to allow each article to be read from top to
bottom. See most of the other articles in this newsgroup for example.

The declaration
int test = NULL;
is questionable. NULL is (a macro that expands to) a null pointer
context; it should only be used as a pointer value. (If NULL happens
to be defined as 0, it can also be used as an integer value.) Also,
you should end your output with a newline; change the "%d, %d, %d"
to "%d, %d, %d\n".

Apart from that, your program seems to behave as it should. The
getData() function returns the value of the variable to which it
points, which happens to be "global"; since global variables are
implicitly initialized to zero, that's the value that's printed.
Printing the value of "global" directly gives you the same result.
And the variable "test" also has the value 0, even though you chose an
odd (and non-portable) way to initialize it.

I don't understand what you're saying about command line arguments.
The program doesn't look at its command line arguments, and in my own
testing it isn't affected by them.

Please clarify what you're asking.
 
O

Old Wolf

pete said:
One time I saw somebody initialize a global pointer to NULL,
and I told him that he didn't have to do that.

In practice, I've encountered a surprisingly high number
of non-conforming embedded systems, which will not
zero-initialize global variables. The linker actually
puts the variable in a different section if you add "= 0" to
the definition.
 
F

Flash Gordon

Old said:
In practice, I've encountered a surprisingly high number
of non-conforming embedded systems, which will not
zero-initialize global variables. The linker actually
puts the variable in a different section if you add "= 0" to
the definition.

My experience is similar. Although in my case the global variable was
put in the same place but it was added to a list of variables to be
initialised on startup.

My solution was to zero all RAM during a RAM test that I wrote to be run
before the C code was started.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top