Are variables contiguous on the stack?
Asked Answered
T

2

7

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.

Treiber answered 22/1, 2014 at 16:6 Comment(4)
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.Impignorate
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.Jillianjillie
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.Atalie
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.)Yehudit
G
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.

Germinant answered 22/1, 2014 at 16:10 Comment(3)
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?Treiber
@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.Germinant
@Treiber You can view a working draft of the standard on this site.Methinks
J
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].

Jaine answered 22/1, 2014 at 16:16 Comment(3)
Although woe betide the code in main if it uses sizeof(a) and you make this change...Atalie
@SteveJessop - I'd hope that if someone was doing this sort of thing then they'd know the risks involvedJaine
use int *const a and int *const b if you can, as it will allow for more optimizations.Prohibit

© 2022 - 2024 — McMap. All rights reserved.