Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sizeof on Structure With Arrays

Posted on 2010-09-15
3
Medium Priority
?
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 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