Do structres get zeroed at declaration?

This is a discussion on Do structres get zeroed at declaration? within the C Programming forums, part of the General Programming Boards category; That's the question, it came because you can't have default initialization values for the structs, so maybe that's the reason....

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    58

    Do structres get zeroed at declaration?

    That's the question, it came because you can't have default initialization values for the structs, so maybe that's the reason.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    No. But if you use calloc to assign space, that should initialize it.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It depends on the scope in which a variable of struct type is declared.
    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.*

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    58
    ok i'll use memset.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Govalant View Post
    it came because you can't have default initialization values for the structs
    You can initialize structs, default is zero for all elements in the proper type for variable of static storage duration. calloc may not be correct depending on the types members.
    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
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Structure will be initialized in following circumstances
    Code:
     ---------------------------------
    |   ON      |  Initialized value  |
     ---------------------------------
    | Global var|          0          |
    | Static    |          0          |
    | calloc    |          0          |
    | Local var |       garbage       |
    | malloc    |          0          |
     ---------------------------------
    Please note: These where the results which i got on my machine.

    ssharish2005

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    You can't rely on calloc / memset producing valid initialisation for pointers and floats.
    http://c-faq.com/malloc/calloc.html

    > | Local var | garbage |
    > | malloc | 0 |

    Visual studio in debug mode specifically fills local variables and malloc'ed memory with known bit-patterns to assist with debugging all those 'use before initialisation' problems.
    In general, you can't rely on either of them containing anything meaningful.

    If you want a zeroised struct with 0, 0.0, NULL as appropriate, then do
    Code:
    struct foo empty = { 0 };
    
    // Then later, you can do
    struct foo myvar = empty;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by ssharish2005 View Post
    | malloc | 0 |
    malloc isn't guaranteed to return initialized memory.

    http://www.cplusplus.com/reference/c...ib/malloc.html
    http://www.cplusplus.com/reference/c...ib/calloc.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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