Solved

Sizeof on Structure With Arrays

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now