Thread: Why does this little code consume extra bytes of memory?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    43

    Why does this little code consume extra bytes of memory?

    In the following program, the structure object nd[0] node consumed 8 bytes of memory. Ideally, nd[0] should consume 5 bytes of memory in total. I mean, 4 bytes for Int type + 1 byte for the char type.
    Code:
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    
    struct Node
    {
        int distance;
        char charName;
    } nd[] = {5,'b'};
    
    
    int main()
    {
        cout << sizeof(nd[0]) << "   " << sizeof(nd[0].charName) << "    " << sizeof(nd[0].distance) << endl;
        return 0;
    }
    Output : 8 1 4

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    First of all, you cannot assume the order of the members of a struct, in memory, despite the order you specify.

    If the char is first in memory, then the int will by default, align on the next int boundary, with 3 bytes of padding in between.

    Your compiler will probably have options concerning the alignment of members of a struct. Unless you have limited memory, best to leave the alignment/padding up to the compiler.

    In addition to all of this, although your question concerns both C & C++, this forum is for discussions of C, NOT C++

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest reading Eric Raymond's article on The Lost Art of Structure Packing. The section on "Structure alignment and padding" has a similiar example with explanation why there might be trailing padding by default.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    Thanks Rstanley.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    Thanks Laserlight.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > First of all, you cannot assume the order of the members of a struct, in memory, despite the order you specify.
    Erm, yes you can.
    Within a structure object, the non-bit-field members and the units in which bit-fields
    reside have addresses that increase in the order in which they are declared. A pointer to a
    structure object, suitably converted, points to its initial member (or if that member is a
    bit-field, then to the unit in which it resides), and vice versa. There may be unnamed
    padding within a structure object, but not at its beginning.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rstanley
    First of all, you cannot assume the order of the members of a struct, in memory, despite the order you specify.
    You are mistaken:
    Quote Originally Posted by C11 (draft because I am on my phone) Clause 6.7.2.1 Paragraphs 13, 15
    Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

    There may be unnamed padding at the end of a structure or union.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    I may have read some incorrect documentation many years ago.

    I stand corrected.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Shafiul View Post
    In the following program, the structure object nd[0] node consumed 8 bytes of memory. Ideally, nd[0] should consume 5 bytes of memory in total. I mean, 4 bytes for Int type + 1 byte for the char type.
    Code:
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    
    struct Node
    {
        int distance;
        char charName;
    } nd[] = {5,'b'};
    
    
    int main()
    {
        cout << sizeof(nd[0]) << "   " << sizeof(nd[0].charName) << "    " << sizeof(nd[0].distance) << endl;
        return 0;
    }
    Output : 8 1 4

    To explain what is going onn here abit more, you are guaranteed
    that the first member will have the same address as the structure itself. You also guarantted that when you declare an array of structures, the size of the array is N * the size of the structure.

    However most platforms have alignment requirements for multi-byte types like int. So it follows that to fulfil these demands, you need to insert three padding bytes, probably at the end of the struct through they could be after the fiirst member.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 04-21-2015, 10:43 AM
  2. fputc inserting extra bytes
    By dylan.scott in forum C Programming
    Replies: 2
    Last Post: 09-05-2009, 04:52 PM
  3. memory allocation in bytes
    By sarathius in forum C Programming
    Replies: 17
    Last Post: 03-17-2008, 09:17 AM
  4. Allocating extra memory for a dialog
    By btq in forum Windows Programming
    Replies: 4
    Last Post: 03-04-2003, 01:04 PM
  5. From where these two extra bytes came?
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-24-2002, 05:32 PM

Tags for this Thread