Solved

Sizeof on Structure With Arrays

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

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

752 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