Initializing Global Variables

Discussion in 'C Programming' started by jyu.james@gmail.com, Oct 4, 2005.

  1. Guest

    Is there an easy way to specify that global variables be initialized to
    something else besides zero?

    Specifically, I have a project that has many global variables spread
    across many files. I want all these variables to be initialized to a
    pattern of 0xFB. For example, for a 2 byte int, I want the value to be
    0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.

    I suppose I could pack all the variables into a struct and simply
    memset it, but this would involve a humongous change in the
    organization of the code. Is there a clever way to do this easily
    without making such huge changes?
    , Oct 4, 2005
    #1
    1. Advertising

  2. Richard Bos Guest

    "" <> wrote:

    > Is there an easy way to specify that global variables be initialized to
    > something else besides zero?


    Separately, yes: initialise them. However...

    > Specifically, I have a project that has many global variables spread
    > across many files. I want all these variables to be initialized to a
    > pattern of 0xFB. For example, for a 2 byte int, I want the value to be
    > 0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.


    ....this is not possible. The Standard requires that all variables with
    static duration, including all globals, are implicitly initialised to
    zero unless explicitly given another value. There is no facility for
    changing this.

    Richard
    Richard Bos, Oct 4, 2005
    #2
    1. Advertising

  3. <> wrote:
    >Is there an easy way to specify that global variables be initialized to
    >something else besides zero?
    >Specifically, I have a project that has many global variables spread
    >across many files. I want all these variables to be initialized to a
    >pattern of 0xFB. For example, for a 2 byte int, I want the value to be
    >0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.
    > ...Is there a clever way to do this easily
    >without making such huge changes?


    There isn't in standard C. It may be possible in a particular
    implementation.
    In some embedded development systems, for example, you can specify the
    "fill pattern" that the linker will use in an otherwise uninitialized
    memory section.
    If your environment will allow you to retrieve in your main programs
    symbols identifying the memory section used by global variables, you
    could 'memset()' that to any value without using an enclosing
    structure as you mentioned.
    In both cases you may also set to something other than zero global
    variables used by the C standard library and run time system etc., so
    the answer is still no.

    Roberto Waltman

    [ Please reply to the group, ]
    [ return address is invalid. ]
    Roberto Waltman, Oct 4, 2005
    #3
  4. Eric Sosman Guest

    Roberto Waltman wrote On 10/04/05 14:10,:
    > <> wrote:
    >
    >>Is there an easy way to specify that global variables be initialized to
    >>something else besides zero?
    >>Specifically, I have a project that has many global variables spread
    >>across many files. I want all these variables to be initialized to a
    >>pattern of 0xFB. For example, for a 2 byte int, I want the value to be
    >>0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.
    >>...Is there a clever way to do this easily
    >>without making such huge changes?

    >
    >
    > There isn't in standard C. It may be possible in a particular
    > implementation.
    > In some embedded development systems, for example, you can specify the
    > "fill pattern" that the linker will use in an otherwise uninitialized
    > memory section.
    > If your environment will allow you to retrieve in your main programs
    > symbols identifying the memory section used by global variables, you
    > could 'memset()' that to any value without using an enclosing
    > structure as you mentioned.


    Keep in mind that the global variables may include
    things like stdout, goodies belonging to malloc(), and
    other things that might not work very well after being
    clobbered with 0xFB ...

    --
    Eric Sosman, Oct 4, 2005
    #4
  5. In article <>,
    <> wrote:
    >
    > I want all these variables to be initialized to a
    > pattern of 0xFB. ... Is there a clever way to do this easily
    > without making such huge changes?


    /*
    this is not portable.
    use only for amusement purposes.
    will break things that expect globals to be zero!
    */

    extern int _edata, _end;
    memset(&_edata, 0xFB, (char *)&_end - (char *)&_edata);
    Anonymous 7843, Oct 4, 2005
    #5
  6. <> wrote:
    >Roberto Waltman wrote On 10/04/05 14:10,:
    >> <> wrote:
    >>>Is there an easy way to specify that global variables be initialized to
    >>>something else besides zero?

    >> If your environment will allow you to retrieve in your main programs
    >> symbols identifying the memory section used by global variables, you
    >> could 'memset()' that to any value without using an enclosing
    >> structure as you mentioned.

    >
    > Keep in mind that the global variables may include
    >things like stdout, goodies belonging to malloc(), and
    >other things that might not work very well after being
    >clobbered with 0xFB ...


    That's exactly what I wrote in the following paragraph, that you did
    not quote...
    Roberto Waltman

    [ Please reply to the group, ]
    [ return address is invalid. ]
    Roberto Waltman, Oct 4, 2005
    #6
    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. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  2. jubelbrus
    Replies:
    5
    Views:
    601
    JohnQ
    Jul 20, 2007
  3. mark4asp
    Replies:
    1
    Views:
    203
  4. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    210
    Tony Archer
    Nov 25, 2003
  5. Stanimir Stamenkov

    Initializing a global namespace object

    Stanimir Stamenkov, Jul 26, 2010, in forum: Javascript
    Replies:
    39
    Views:
    353
    David Mark
    Jul 30, 2010
Loading...

Share This Page