Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why can't I initialize a global variable to NULL?

Reply
Thread Tools

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

 
 
moxm
Guest
Posts: n/a
 
      08-17-2005
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.

 
Reply With Quote
 
 
 
 
akarl
Guest
Posts: n/a
 
      08-17-2005
moxm wrote:
> 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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-17-2005
akarl wrote:
>
> moxm wrote:
> > I have a statement declares a globle variable like this :
> >
> > char *pname = NULL;


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


--
pete
 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      08-17-2005
pete wrote:
> akarl wrote:
>
>>moxm wrote:
>>
>>>I have a statement declares a globle variable like this :
>>>
>>>char *pname = NULL;

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


Sigh...
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      08-17-2005

"moxm" <(E-Mail Removed)> wrote
>
>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.


 
Reply With Quote
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      08-17-2005

akarl wrote:
> moxm wrote:
> > 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).
>


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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>
> akarl wrote:
>>
>> 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".


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
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      08-17-2005
(E-Mail Removed) wrote:
> akarl wrote:
>
>>moxm wrote:
>>
>>>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).

>
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      08-17-2005
Malcolm wrote:
> "moxm" <(E-Mail Removed)> wrote
>
>>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.


<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.
 
Reply With Quote
 
moxm
Guest
Posts: n/a
 
      08-17-2005
/*@null@*/
char *pname = NULL;

then splint will shut up it's mouth.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is it not possible to assign a global variable to anotherglobal variable..? Jase Schick C Programming 11 08-05-2010 05:16 AM
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How to initialize a global variable prior to another? mars C++ 2 03-02-2006 06:07 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM



Advertisments