1

If I declare a variable as

 int a[100]

it is said that an array with 100 elements created on stack, and can be a bad idea depending on size etc.

Consider I define a structure

 struct abc
 {
    int a[100];
 };

and somewhere in code I utilize this structure as

 abc P; //line 1
 abc *p = new abc(); //line 2

Now the array is inside these two objects( one on stack(line 1) and one on heap (line 2) ). Where does the internal array reside?

Thanks

5
  • What does it matter? Instead of raw arrays, one should use std::vector or std::array. Also preferring automatic storage over dynamic storage. Commented Aug 12, 2016 at 13:57
  • 1
    @crashmstr some C++ local company guidelines for embedded C++, in particular, disallows use of many std constructs as well as C++1x concepts, in which case the above could be relevant. Commented Aug 12, 2016 at 13:58
  • @dfri true, but the OP mentions nothing of those kind of restrictions and embedded and other specific platform needs are generally outside "general recommendations" for idiomatic C++. Commented Aug 12, 2016 at 14:00
  • @crashmstr also true, but in the general sense of learning basic concepts of C++ (as I assume OP is doing), the above also could be relevant. But most importantly, tying back to the latter: there must exist some thread that this one can appropriately be marked as a duplicate of. Commented Aug 12, 2016 at 14:04
  • If the array is inside something (and it is), then surely it must be in the same general place as the thing it's inside. (When you're inside a car, and the car is in a city, you're in the same city as the car.) Commented Aug 12, 2016 at 14:16

3 Answers 3

4

The location for a data member depends on the location of the object contains it. When the struct is on the stack, all its members are on the stack. When the struct is on the heap, so are the members.

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

Comments

0

In line 1 the array is on the stack.

In line 2 the array is on the heap.

The struct is seen as one big variable containing all the internal arrays (and perhaps some more memory for padding and aligning) and the entire thing resides where you out it - stack or heap.

This is why you can assign struct to struct, like

s1 = s2;

and all the arrays get copied - it is handled as one big chunk of data (although it's a shallow copy, the arrays occupy real memory).

Comments

0

It's in the same place as the object, cause when the object is on the heap and the inside array would be on the stack the array would be deleted and you end up with an empty object.

2 Comments

Hi theStroyer, and welcome to Stack Overflow. Could you expand your answer a bit? It's quite difficult to understand, because your explanation is very terse.
I guess I don't have to expand it anymore, because someone else already posted a good answer. I will keep in mind that I have to make my answers as clear as possible in the feature.

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.