Solved

Sizeof on Structure With Arrays

Posted on 2010-09-15
3
474 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

806 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