Solved

Sizeof on Structure With Arrays

Posted on 2010-09-15
3
478 Views
Last Modified: 2012-05-10
Hey again,

Lotsa questions this week.  Can someone go into some detail and explain what happens here?

#include <iostream>
using namespace std;

struct SomeFakeData
{
    int intArray[10];
    char charArray[10];
};

struct Test
{
    SomeFakeData sfd[20];
    int xx;
    const char* yy;
};

int main()
{
    Test test;
    test.xx = 99;
    test.yy = "str99str";
    test.sfd[1].intArray[4] = 32;
    test.sfd[1].charArray[3] = 'H';

    cout<<sizeof(Test)<<endl;
    cout<<sizeof(test)<<endl;
}


Okay, so using this crappy example, both sizeof statements return the same result, even though the c-string held within the strucutre, test, has been given a value which should take more bytes than it previously did.  I get that this is because sizeof() probably took the size of the pointer rather than the sizeof what it pointed to.

My question is:

If I'm calculating the size of object test, can I assume that the size of the arrays is already incorporated to what gets output when I do sizeof(Test)?  Can someone give me a line of code that calculates the complete size of everything needed to hold the complete instance "test"?

Thanks in advance!

-w00te
0
Comment
Question by:w00te
  • 3
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 33686343
The values you use do not impact the size. sizeof is a compile time construct. It returns the size of the entire struct, and that size does not change while the program runs.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 33686366
The size of the struct will be :

        20 * sizeof(SomeFakeData) + sizeof(int) + sizeof(char*) + padding

and sizeof(SomeFakeData) will similarly be :

        10 * sizeof(int) + 10 * sizeof(char) + padding

Note that the padding is implementation dependent. There might or might not be padding between the struct fields and/or at the end of the struct.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33686381
>> Can someone give me a line of code that calculates the complete size of everything needed to hold the complete instance "test"?

I'm not sure what you mean by this, but sizeof gives you the complete size of the struct, including arrays.

Note though that for the const char* pointer, sizeof only includes the size of the pointer itself - not the data it points to !
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert money to nchar 4 69
What's the Difference Between a VI, the Command Prompt and a Shell 7 123
Create a path if not exists 7 92
Why isn't object file created? 6 69
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question