Doubt about size of structure with inner structure

Hi

I create a structure with another one nested inside it. The doubt is regarding the size of structure if I create a pointer object to the inner structure.

Listing 1: With inner struct object as a pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

struct a
{
   struct b
   {
      int c;
      int d;
   } *var;

   int e;
}aobj;

int main()
{
   cout<<"Size of a = "<<sizeof(a)<<endl;
   cout<<"Size of a::b = "<<sizeof(a::b)<<endl;
   return 0;
}


Output:
Size of a = 8
Size of a::b = 8


Listing 2: With normal inner struct object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

struct a
{
   struct b
   {
      int c;
      int d;
   } var;

   int e;
}aobj;

int main()
{
   cout<<"Size of a = "<<sizeof(a)<<endl;
   cout<<"Size of a::b = "<<sizeof(a::b)<<endl;
   return 0;
}


Output:
Size of a = 12
Size of a::b = 8


What is the reason for the difference in size of the outer structure?
Last edited on
On your machine, sizeof(a::b*) appears to equal sizeof(int).

So for the first example, a has a int and an a::b*, which is 4+4 = 8.

For the second example, you have an int (4) and a b struct (2 ints so 4+4 = 8) so a is 4+8 = 12.
Yes thats what I also figure it out to be, but what would cause sizeof(a::b*) to be equal to sizeof(int).
The next problem that I face with this is while overloading the operator new.

I am using gcc 3.4.6 on linux
No particular reason. It just so happens that they are equal.
If I use a double and an int in the inner structure in Listing 1, the size of the outer structure does not change.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

struct a
{
   struct b
   {
      int c;
      double d;
   } *var;

   int e;
}aobj;

int main()
{
   cout<<"Size of a = "<<sizeof(a)<<endl;
   cout<<"Size of a::b = "<<sizeof(a::b)<<endl;
   return 0;
}


Output:
Size of a = 8
Size of a::b = 12


Whay does the inner structure's size does not alter the size of the outer structure if it is a pointer?
Last edited on
Because a pointer is not the actual structure. It is a separate piece of data containing the memory address of the data it is pointing at.
And it is implemented as an integer within the compiler, so the outer structure's size remains the same whatever be contained in the inner structure.

Now, does overloading the new operator become necessary here because there is no memory allocation for the inner structure's variables and I get a memory fault upon trying to access aobj.var->c
Last edited on
No, you just need to actually get an object of a::b to point to:

1
2
3
4
a something;
a::b something_else;
a.var = &something_else;
a.var->c = 25;
But is that a good coding practice?
The kind of structure is part of a library to be used from the main application.
It sounds like good coding practice, in this case, would be to just have a member variable rather than using a pointer.
@mgupta:

yeah, its not a good coding practice to make an obj if inner member.Its would be better if you write a member function to initialise this pointer by allocating storage for the inner structure.Otherwise there would be no way to utilise this inner structure without accessing it from outside by a::b.
Thanks all.
Topic archived. No new replies allowed.