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

Discussion in 'C Programming' started by moxm, Aug 17, 2005.

  1. moxm

    moxm Guest

    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.
    moxm, Aug 17, 2005
    #1
    1. Advertising

  2. moxm

    akarl Guest

    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
    akarl, Aug 17, 2005
    #2
    1. Advertising

  3. moxm

    pete Guest

    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
    pete, Aug 17, 2005
    #3
  4. moxm

    akarl Guest

    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...
    akarl, Aug 17, 2005
    #4
  5. moxm

    Malcolm Guest

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

    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.
    Malcolm, Aug 17, 2005
    #5
  6. moxm

    Guest

    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".
    , Aug 17, 2005
    #6
  7. 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
    Richard Heathfield, Aug 17, 2005
    #7
  8. moxm

    Michael Mair Guest

    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.
    Michael Mair, Aug 17, 2005
    #8
  9. moxm

    Michael Mair Guest

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

    >
    > 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.
    Michael Mair, Aug 17, 2005
    #9
  10. moxm

    moxm Guest

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

    then splint will shut up it's mouth.
    moxm, Aug 17, 2005
    #10
  11. moxm

    Default User Guest

    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
    Default User, Aug 17, 2005
    #11
  12. moxm

    Michael Mair Guest

    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.
    Michael Mair, Aug 17, 2005
    #12
  13. moxm

    akarl Guest

    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
    akarl, Aug 17, 2005
    #13
  14. moxm

    moxm Guest

    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.
    moxm, Aug 18, 2005
    #14
  15. moxm

    Peter Ammon Guest

    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.
    Peter Ammon, Aug 18, 2005
    #15
  16. moxm

    pete Guest

    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
    pete, Aug 18, 2005
    #16
  17. moxm

    PRadyut Guest

    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
    PRadyut, Aug 18, 2005
    #17
  18. "PRadyut" <> 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) <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.
    Keith Thompson, Aug 18, 2005
    #18
  19. moxm

    Old Wolf Guest

    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.
    Old Wolf, Aug 18, 2005
    #19
  20. moxm

    Flash Gordon Guest

    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.
    Flash Gordon, Aug 19, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,519
    Mike Schilling
    Mar 29, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  4. mars
    Replies:
    2
    Views:
    736
    Axter
    Mar 2, 2006
  5. jubelbrus
    Replies:
    5
    Views:
    602
    JohnQ
    Jul 20, 2007
Loading...

Share This Page