Do vectors and structures always have continuous addresses?

Asked

Viewed 61 times

3

Why vectors and structures are continuous in memory? I believe that it is not just coincidence.

#include <stdio.h>
int main(void){
    char s[10];
    for (int i=0; i<10; i++)
        printf("%d=%p.\n", i, (void*)&s[i]);
    struct {
        double r;
        int x;
        char str[4];
        int t;
    } E;
    printf("---------------\n");
    printf("& r     =%p.\n", & E.r);
    printf("& x     =%p.\n", & E.x);
    printf("& str[%d]=%p.\n",0, & E.str[0]);
    printf("& str[%d]=%p.\n",1, & E.str[1]);
    printf("& t     =%p.\n", & E.t);
    printf("---------------\n");
    printf("sizeof(double)=%d.\n", (int) sizeof(double));
    printf("sizeof(int)=%d.\n", (int) sizeof(int));
    printf("sizeof(char)=%d.\n", (int) sizeof(char));
}

Exit:

0=0x7ffe7531a1f2.
1=0x7ffe7531a1f3.
2=0x7ffe7531a1f4.
3=0x7ffe7531a1f5.
4=0x7ffe7531a1f6.
5=0x7ffe7531a1f7.
6=0x7ffe7531a1f8.
7=0x7ffe7531a1f9.
8=0x7ffe7531a1fa.
9=0x7ffe7531a1fb.
---------------
& r     =0x7ffe7531a1d0.
& x     =0x7ffe7531a1d8.
& str[0]=0x7ffe7531a1dc.
& str[1]=0x7ffe7531a1dd.
& t     =0x7ffe7531a1e0.
---------------
sizeof(double)=8.
sizeof(int)=4.
sizeof(char)=1.
  • Why do you think it could or should be different?

  • 1

    I don’t think so. But I avoid using pointers because I’m not sure it’s continuous. In the case of vector characters I even use, but I’m not sure whether integers or other types (including those defined by typedef) work the same way. And I don’t like programming "thinking".

  • In the case of structs the elements that compose it may not be allocated in contiguous positions due to the alignment of the memory positions that the compiler uses according to the type of data, there may be memory positions not used to maintain such alignment.

  • In your example change char str[4]; to char str[3]; and note the address where int t will be allocated.

  • {dc=0, dd=1, de=2, df=3}=str[4], I don’t understand where the bug is (and I don’t remember changing anything other than the formatting in Ctrl+c - Ctrl+v

1 answer

5


struct is something for you to group heterogeneous data that would be isolated to be treated as one thing, is to give cohesion to a type of object that you are creating, so what is the sense that they are separate? What gain do you expect to have from this? They are continuous because it is precisely what you want, that all that is something together.

The same goes for a array, the objective of it is to have a homogeneous set of data that would be isolated being one thing and that can be accessed with constant complexity (O(1)), which requires that the exact position is achieved through a simple arithmetic, which prevents each element from being spread across memory, but this is even more a consequence of the previous reason.

This arithmetic is the memory address of the initial element plus the simple position of the element (index) times the size of each individual object (so the object needs to be the same size).

In both of you you want it to be one object, so it would be hard not to be continuous. You cannot have a single plot with others in the middle, if you have holes you have several plots and not one. The two forms are ways to have something together.

This has important consequences for what you’re going to do next because of this organization. Be assured that these forms exist precisely to have everything in a continuous way. I don’t have an easy spec, but I can assure you there’s something in it that says this is guaranteed for these two cases.

How this data is always accessed by a pointer, even if you don’t see this (the member of the structure not exactly a dynamic pointer, but analogously that is already solved by the compiler) can trust that it will access properly, although for the case of the struct memory arithmetic is done in such a way that you don’t even have control over the continuity.

Of course access outside the object region no longer works right.

This can help.

A continuous object is different from continuity between objects

If you’re talking about one object being allocated after another, that’s normal in the stack since the objects are stacked continuously, again to have a very good performance (read more). If you were allocating heap would almost be coincidence. It is true that most cases will be followed, after all in a truck when you load goes putting one sludge box after the other or above the others in an organized way, in memory is the same. Of course you can have compartments and some things get a little out of order, so never consider what allocation in heap have two distinct objects soon afterwards (not to be confused with internal members or elements of this object, these somehow become continuous, at least in the most abstract view).

Note that there is no guarantee that stack one object will be allocated continuously to another as well, it is much more common, but there are some cases that this may not be true in some implementations (I don’t know one that is so), so just rely on the internal continuity of the object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.