6

I'm wondering if the arrays a[] and b[] in the code below are contiguous in memory:

int main() {
    int a[3];
    int b[3];

    return 0;
}

a[0],a[1] and a[2] should be contiguous and the same for b, but are there any guarantees on where b will be allocated in relation to a?

If not, is there any way to force a and b to be contiguous with eachother? i.e. - so that they are allocated next to eachother in the stack.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Dievser
  • 85
  • 1
  • 6
  • 2
    Since there is no "stack", the question cannot be answered in terms of C++. A decent compiler wouldn't allocate anything in your case because the program does nothing. – Kerrek SB Jan 22 '14 at 16:08
  • 3
    No, there are no such guarantees. If you need `a` and `b` to be contiguous you could put them in a struct. Even then you will have to contend with details such as padding. – Michael Burr Jan 22 '14 at 16:09
  • It's pretty unlikely that a compiler will put padding between two arrays of the same type in a struct, right? It cannot be necessary in order to satisfy alignment requirements, so it would either be pure devilment, or else some need the compiler has for usable space in the struct between the members. Some kind of ABI-destroying instrumentation, maybe. So I think you could contend with the possibility of padding using an assertion that measures the size of the struct and ensures there's none. – Steve Jessop Jan 22 '14 at 17:30
  • I am unconvinced about the likelihood of the compiler necessarily _not_ putting padding between like array types. AFAIK it is an implementation-defined detail, is it not? (I mean, I agree it would be dumb for two char arrays, but I can imagine structures where it totally would, and I don’t like assuming anything if I plan to play games with UB array access and type puns and the like.) – Dúthomhas Dec 24 '22 at 19:05

2 Answers2

18

No, C++ makes no guarantee about the location of these variables in memory. One or both may not even be in memory (for example if they are optimised out)!

In order to obtain a relative ordering between the two, at least, you would have to encapsulate them into a struct or class and, even then, there would be padding/alignment to consider.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    Thanks! I often find myself asking these kinds of questions because I cant find the answers anywhere except from other people. Where did you get your information from, if you dont mind me asking? Is there some "c++ bible" somewhere which covers basic memory management and such? – Dievser Jan 22 '14 at 16:25
  • 1
    @Dievser: Experience. The standard simply doesn't say anything about this, and I remember that. :-) Also, I just double-checked by scanning through its text in the relevant sections. Though footnote 4 points out that the as-if rule permits an object not to be stored if the program will never find out, which sort of gets you half-way there. – Lightness Races in Orbit Jan 22 '14 at 16:32
  • @Dievser You can view a working draft of the standard [on this site.](http://isocpp.org/) – David G Jan 22 '14 at 16:33
4

There's no guarantee that individual variables will be next to each other on the stack.

In your example you could "cheat" and simulate what you're looking for by doing this:

int main() 
{
    int buffer[6]
    int *a = buffer;
    int *b = buffer+3;

    return 0;
}

Now, when you write to a[4] you'll actually write to b[0].

Sean
  • 60,939
  • 11
  • 97
  • 136