jeudi 14 mai 2020

Why a & b have the same address?

#include <stdio.h>
#include <array>
#include <vector>

std::vector<int> foo() {
 int i;
 std::vector<int> a(100);
 printf("%p, %p, %p\n", &i, &a, &(a[0]));
 return a;
}

int main() {
 int i;
 std::vector<int> b = foo();
 printf("%p, %p, %p\n", &i, &b, &(b[0]));
}

I don't understand why a & b have the same address? How this is achieved? Some kind of "cross-stack-frame" optimization? I even used -O0 option, the result is the same.

Aucun commentaire:

Enregistrer un commentaire