2

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)?

6
  • 3
    the size of x is always the same, it is a pointer Commented Feb 26, 2019 at 8:59
  • 3
    You want to use std::vector. Commented Feb 26, 2019 at 8:59
  • 2
    Whenever you think "dynamic array" your next thought should almost always be std::vector. Commented Feb 26, 2019 at 9:00
  • 1
    @someprogrammerdude humans are like vectors. We sometimes expand but shrinking is difficult. Commented Feb 26, 2019 at 9:00
  • 3
    vector is not just a fancy dynamic array. Note that your struct Y is broken in several ways. It can be copied, but bad things happen if you do. It can be used with automatic storage, but when it goes out of scope it leaks memory. It takes a lot more to get dynamic memory allocations right, something you dont want to rewrite each time, because someone else already did it for you: std::vector Commented Feb 26, 2019 at 9:13

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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.

Comments

0

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

7 Comments

you dont need to mention C. C++ is not a better C. C is a different language for different problems with different solutions
@user463035818 I never said C++ is better than C, where do you read that ? I just say the OP uses a way similar to what we do in C, and it is more practical to use vector existing in C++ but not existing in C. You wrongly interpret my sentence ;-)
I didnt quote you literally, but the fact that C has no vector is irrelevant, the only message it conveys is that C++ would be better because it has vector. Yes thats just how I think your sentence can be interpreted. Btw no reason to feel offended, I just thought it would be nice to tell you why I didnt upvote ;)
@user463035818 "C has no vector is irrelevant" : again you wrongly interpret, I never said "C has no vector", I said "C++ has vector". So strange ...
you can insist that my interpretation is wrong, or you could consider my point of view. Not really strange, but a little bit sad...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.