Thread: data structure alignment

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Question data structure alignment

    I am reading this and got curious!
    Data structure alignment - Wikipedia, the free encyclopedia

    do you think if the struct has +1 variable, the struct will ALWAYS allocate 2 more bytes + of course bytes of other data types that the structure contains???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    It depends on your structure. The padding can be zero and upwards.
    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.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by c_lady View Post
    do you think if the struct has +1 variable, the struct will ALWAYS allocate 2 more bytes + of course bytes of other data types that the structure contains???
    It depends on the data type of the variable.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    ok if i have this structure

    Code:
    struct person{
         char name[9];
    };
    means 3 bytes will be allocated? right...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    You could just do sizeof(struct person) and find out for yourself how many bytes it needs.

    And no, 3 isn't the answer.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    sorry I meant 12

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM