Two questions

This is a discussion on Two questions within the C Programming forums, part of the General Programming Boards category; 1. Why the values of static and global variables are initialized to zero by default? 2. How can we write ...

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    277

    Two questions

    1. Why the values of static and global variables are initialized to zero by default?
    2. How can we write our own sizeof() operator?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by anirban View Post
    1. Why the values of static and global variables are initialized to zero by default?
    Here's a graph of the options:

    1. The values start out uninitialized.
    2. The values start out initialized
    .... a. The values start out at zero
    .... b. The values start out at some value other than zero

    Case 1 is obviously not preferred.

    Case 2b is strange. If it's not zero, what value would it be? What justification would there be for that particular value?

    Case 2a makes the most sense.

    How can we write our own sizeof() operator?
    It involves using an array. That's probably enough of a hint.

  3. #3
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    277
    Yes that is my query, why Case 1 is not preferred?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by anirban View Post
    Yes that is my query, why Case 1 is not preferred?
    Because in the case of a global variable, there isn't even such a thing as "uninitialized." It exists as a physical object within the program image. Whereas local variables are created dynamically on the stack when a function is executed.

    Why would a programmer prefer to have their global variables start out with random values, when it costs absolutely nothing to initialize them directly in the program image?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    And a corollary is that you don't generally prefer that automatic variables, those with local scope, undergo run-time hits to initialize them. But those variables with program lifetime, whose initialization takes place before main, it is preferred.

    With regard to sizeof, it involves knowing the size of the object to be represented, which can be calculated knowing (the composite of) the elementary components composing the object and any alignment issues.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Dave_Sinkula View Post
    With regard to sizeof, it involves knowing the size of the object to be represented, which can be calculated knowing (the composite of) the elementary components composing the object and any alignment issues.
    I'd post the exact method of determining sizeof() without actually using it, but I'm suspicious that this is a homework problem.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by brewbuck View Post
    I'd post the exact method of determining sizeof() without actually using it, but I'm suspicious that this is a homework problem.
    I too am familiar with the one you mean, it has even been posted on this board several times. I just thought the question was worded differently (and I too suspect that this is homework).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    277
    Please say me how to do it! I cannot do it myself.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by anirban View Post
    Please say me how to do it! I cannot do it myself.
    I'm perfectly willing to enlighten you, but if this IS a homework assignment, I think you would benefit from doing it yourself.

    Some facts to consider: sizeof(char) == 1, always. And the data members of arrays are always packed as closely as possible. The solution is going to involve some type-casting.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    look in limits.h for a start

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21