user3862410
user3862410

Reputation: 171

Dynamically allocated structure array with an dynamically allocated structure array element

Please consider the following code:

struct X
{
   int x1;
   int x2;
};

struct Y
{
   int y1;
   struct X *x;
};

Now I am allocating memory dynamically as follows:

struct Y *y = new Y[N];

And for each element of this structure array, I am also allocating memory for y[i].x as follows-

y[i].x = new X[M];

In such case, how does the system will allocate memory for y before knowing the actual memory size of y[i].x.

I am getting a segmentation fault in this case. Is there any good way to allocate memory dynamically? What if I need to reallocate memory (to grow the array size dynamically)?

Upvotes: 2

Views: 125

Answers (4)

freakish
freakish

Reputation: 56467

In such case, how does the system will allocate memory for y before knowing the actual memory size of y[i].x.

Oh, but it does know the size of y[i].x. That member's type is struct X* which is a pointer. The size of a pointer varies between architectures but it typically is 32 of 64 bits. Regardless of what X is (there are few edge cases like function pointers).

In other words X is not a piece of Y. Actually Y has a pointer that points to a chunk of memory occupied by (possibly multiple) X.

It's like having an address. You can write it down on a small piece of paper an keep it. Everyone knows the size of the paper. Regardless of how many (and how big) houses occupy the actual place.

Your segmentation fault has nothing to do with all of that. Most likely you've crossed some boundary. But it's hard to tell without the actual code.

Upvotes: 4

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

In such case, how does the system will allocate memory for y before knowing the actual memory size of y[i].x.

The compile knowsr the size of a pointer, as x currently is declared.
The segmentation fault most probably occurs when you try to dereference y[i].x since you never correctly allocated the memory it should point to.

Is there any good way to allocate memory dynamically?

Yes there is. You should use std::vector to let it handle all the obstacles and pitfalls doing the manual memory management for you:

struct X
{
   int x1;
   int x2;
};

struct Y
{
   int y1;
   std::vector<X> x;
};

std::vector<Y> y(N);
y[i].x.resize(M);

What if I need to reallocate memory (to grow the array size dynamically)?

That will be also managed by the std::vector class.

Upvotes: 3

Chris623
Chris623

Reputation: 2532

y[i].x is always a pointer which has a fixed size on every system. (Mostly it is 32/64 bit). That is why the system know how much memory to allocate for each instance of Y.

Upvotes: 2

bruno
bruno

Reputation: 32586

In such case, how does the system will allocate memory for y before knowing the actual memory size of y[i].x

In Y the type of x is a pointer to X, so the size is the size of a pointer, the size of the pointed memory block is not relevant when allocating Y

Is there any good way to allocate memory dynamically? What if I need to reallocate memory (to grow the array size dynamically)?

Visibly you need a std::vector<X> rather than a pointer to X, you are in C++, not in C

Upvotes: 0

Related Questions