Skip to main content
adicionou 103 caracteres ao conteúdo
Fonte Link
Maniero
  • 484,5mil
  • 94
  • 1,3mil
  • 2,2mil

Por quê vetores e estruturas são contínuas em memória? Creio que não seja somente coincidência.

#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));
}

Saída:


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.

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.

Por quê vetores e estruturas são contínuas em memória? Creio que não seja somente coincidência.

#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));
}

Saída:


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.

Por quê vetores e estruturas são contínuas em memória? Creio que não seja somente coincidência.

#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));
}

Saída:

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.
Tweeted twitter.com/StackOverflowPT/status/1145890077823946752
Fonte Link

Vetores e estruturas tem sempre endereços contínuos?

Por quê vetores e estruturas são contínuas em memória? Creio que não seja somente coincidência.

#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));
}

Saída:

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.