473,326 Members | 2,133 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,326 software developers and data experts.

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

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.

Nov 15 '05 #1
19 11913
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
Nov 15 '05 #2
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
Nov 15 '05 #3
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...
Nov 15 '05 #4

"moxm" <mo********@yahoo.com> 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.
Nov 15 '05 #5

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

Nov 15 '05 #6
ju**********@yahoo.co.in 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
Nov 15 '05 #7
ju**********@yahoo.co.in 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.
Nov 15 '05 #8
Malcolm wrote:
"moxm" <mo********@yahoo.com> 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.
Nov 15 '05 #9
/*@null@*/
char *pname = NULL;

then splint will shut up it's mouth.

Nov 15 '05 #10
ju**********@yahoo.co.in wrote:

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

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
Nov 15 '05 #11
moxm wrote:
/*@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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #12
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
Nov 15 '05 #13

Michael Mair wrote:
moxm wrote:
/*@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.


Thank you! I will take your advice.

Nov 15 '05 #14
akarl wrote:
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


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.
Nov 15 '05 #15
Peter Ammon wrote:

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


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.

--
pete
Nov 15 '05 #16
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:
Peter Ammon wrote:

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


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.

--
pete


Nov 15 '05 #17
"PRadyut" <pr******@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #18
pete wrote:
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.

Nov 15 '05 #19
Old Wolf wrote:
pete wrote:
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.


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.
Nov 15 '05 #20

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

Similar topics

4
by: SamMan | last post by:
I have a .js file that receives a value from an html page. I'd like this value to be a global variable that all functions in the .js file can use. I have tried just declaring a var at the top of my...
5
by: vsnadagouda | last post by:
Hello All, I am looking for some way to delay the global variable initilization. To make myself more clear, here is a simple example: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++...
3
by: freddy | last post by:
I know in vb.net you can declare a global variable, but how do you declare one in c#. I want to make a connection class with the path and have it accessed by anther class or method. Thank you
3
by: Nathan | last post by:
Somebody help please, I am desperate for help as I've battled this problem for 3 days now! :( I have an application (NT Service) that uses 2 threads... one worker-thread (i.e. always running)...
4
by: Boni | last post by:
Dear all, how can I initialize shared variable? i.e class A private shared B as boolean end class in C++ I would write in the global scope A::B=true What is the proper way in vb
1
by: amit | last post by:
Hello Group, Does anybody know how I can have a global variable in an HTML file? for instance, I have a fuction (called aFunction() here) and during a mousedown or up event the function is going...
4
by: jubelbrus | last post by:
I need to initiate a global variable to a class before the initialization of any other global variable. The problem is that when I link the application with a dll, some or all global variables in...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.