Connecting Tech Pros Worldwide Forums | Help | Site Map

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

moxm
Guest
 
Posts: n/a
#1: Nov 15 '05
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.


akarl
Guest
 
Posts: n/a
#2: Nov 15 '05

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


moxm wrote:[color=blue]
> 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.[/color]

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
pete
Guest
 
Posts: n/a
#3: Nov 15 '05

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


akarl wrote:[color=blue]
>
> moxm wrote:[color=green]
> > I have a statement declares a globle variable like this :
> >
> > char *pname = NULL;[/color][/color]

That's a declaration and a definition,
but not a statement.
[color=blue]
> In C all global pointer variables are guaranteed to be initialized to
> NULL,[/color]

.... 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 */


--
pete
akarl
Guest
 
Posts: n/a
#4: Nov 15 '05

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


pete wrote:[color=blue]
> akarl wrote:
>[color=green]
>>moxm wrote:
>>[color=darkred]
>>>I have a statement declares a globle variable like this :
>>>
>>>char *pname = NULL;[/color][/color]
>
>
> That's a declaration and a definition,
> but not a statement.
>
>[color=green]
>>In C all global pointer variables are guaranteed to be initialized to
>>NULL,[/color]
>
>
> ... unless the code initializes them to something else,
> like an address constant.[/color]

Sigh...
Malcolm
Guest
 
Posts: n/a
#5: Nov 15 '05

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



"moxm" <moxm_black@yahoo.com> wrote[color=blue]
>
>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.
>[/color]
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.


junky_fellow@yahoo.co.in
Guest
 
Posts: n/a
#6: Nov 15 '05

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



akarl wrote:[color=blue]
> moxm wrote:[color=green]
> > 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.[/color]
>
> 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).
>[/color]

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".

Richard Heathfield
Guest
 
Posts: n/a
#7: Nov 15 '05

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


junky_fellow@yahoo.co.in wrote:
[color=blue]
>
> akarl wrote:[color=green]
>>
>> 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).
>>[/color]
>
> I have a doubt if all global unitialized pointer variables are
> initialized with NULL. I think they are initialized with
> "all bits zeros".[/color]

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).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Michael Mair
Guest
 
Posts: n/a
#8: Nov 15 '05

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


junky_fellow@yahoo.co.in wrote:[color=blue]
> akarl wrote:
>[color=green]
>>moxm wrote:
>>[color=darkred]
>>>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.[/color]
>>
>>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).[/color]
>
> 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".[/color]

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Michael Mair
Guest
 
Posts: n/a
#9: Nov 15 '05

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


Malcolm wrote:[color=blue]
> "moxm" <moxm_black@yahoo.com> wrote
>[color=green]
>>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.
>>[/color]
>
> 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.[/color]

<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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
moxm
Guest
 
Posts: n/a
#10: Nov 15 '05

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


/*@null@*/
char *pname = NULL;

then splint will shut up it's mouth.

Default User
Guest
 
Posts: n/a
#11: Nov 15 '05

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


junky_fellow@yahoo.co.in wrote:

[color=blue]
> I have a doubt if all global unitialized pointer variables are
> initialized with NULL. I think they are initialized with
> "all bits zeros".[/color]


Is there some reason you fail to read the FAQ list and the standard
before making these sorts of statements?

http://www.eskimo.com/~scs/C-faq/q1.30.html




Brian
Michael Mair
Guest
 
Posts: n/a
#12: Nov 15 '05

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


moxm wrote:[color=blue]
> /*@null@*/
> char *pname = NULL;
>
> then splint will shut up it's mouth.[/color]

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
akarl
Guest
 
Posts: n/a
#13: Nov 15 '05

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


moxm wrote:[color=blue]
> /*@null@*/
> char *pname = NULL;
>
> then splint will shut up it's mouth.[/color]

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

char *pname;


August
moxm
Guest
 
Posts: n/a
#14: Nov 15 '05

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



Michael Mair wrote:[color=blue]
> moxm wrote:[color=green]
> > /*@null@*/
> > char *pname = NULL;
> >
> > then splint will shut up it's mouth.[/color]
>
> 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.[/color]

Thank you! I will take your advice.

Peter Ammon
Guest
 
Posts: n/a
#15: Nov 15 '05

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


akarl wrote:[color=blue]
> moxm wrote:
>[color=green]
>> /*@null@*/
>> char *pname = NULL;
>>
>> then splint will shut up it's mouth.[/color]
>
>
> ...or if you (as mentioned) rely on the default initialization;
>
> char *pname;
>
>
> August[/color]

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?

--
Pull out a splinter to reply.
pete
Guest
 
Posts: n/a
#16: Nov 15 '05

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


Peter Ammon wrote:[color=blue]
>
> akarl wrote:[color=green]
> > moxm wrote:
> >[color=darkred]
> >> /*@null@*/
> >> char *pname = NULL;
> >>
> >> then splint will shut up it's mouth.[/color]
> >
> >
> > ...or if you (as mentioned) rely on the default initialization;
> >
> > char *pname;
> >
> >
> > August[/color]
>
> 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?[/color]

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

--
pete
PRadyut
Guest
 
Posts: n/a
#17: Nov 15 '05

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


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

pete wrote:[color=blue]
> Peter Ammon wrote:[color=green]
> >
> > akarl wrote:[color=darkred]
> > > moxm wrote:
> > >
> > >> /*@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[/color]
> >
> > 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?[/color]
>
> One time I saw somebody initialize a global pointer to NULL,
> and I told him that he didn't have to do that.
>
> --
> pete[/color]

Keith Thompson
Guest
 
Posts: n/a
#18: Nov 15 '05

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


"PRadyut" <pradyutb@gmail.com> writes:[color=blue]
> 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[/color]

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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Old Wolf
Guest
 
Posts: n/a
#19: Nov 15 '05

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


pete wrote:
[color=blue]
> One time I saw somebody initialize a global pointer to NULL,
> and I told him that he didn't have to do that.[/color]

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.

Flash Gordon
Guest
 
Posts: n/a
#20: Nov 15 '05

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


Old Wolf wrote:[color=blue]
> pete wrote:
>[color=green]
>>One time I saw somebody initialize a global pointer to NULL,
>>and I told him that he didn't have to do that.[/color]
>
> 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.[/color]

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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Closed Thread