Вопрос

During a lecture I learned about the importance of 8-byte alignment for x86 architectures. This was visualized using the following example:

// char 1 byte 
int main() 
{ 
 char a1; 
 char a2; 
 char b1[5]; 
 char b2[8]; 
 char b3[3]; 
 return 1; 
} 

..which during debugging would show the 8-byte-alignment for the array variables:
0xffbfe6e8, 0xffbfe6e0, 0xffbfe6d8

(gdb) print &a1 
$1 = 0xffbfe6f7 "" 
(gdb) print &a2 
$2 = 0xffbfe6f6 "" 
(gdb) print &b1 
$3 = (char (*)[5]) 0xffbfe6e8 
(gdb) print &b2 
$4 = (char (*)[8]) 0xffbfe6e0 
(gdb) print &b3 
$5 = (char (*)[3]) 0xffbfe6d8 

I am trying to reproduce this example, but I can't get to reveal the memory addresses.

  • g++ -g main.cpp (OK)
  • gdb a.out (OK)
  • (gdb) print &a1 ==> No symbol "a1" in current context.

Can somebody enlighten me to see what I am doing wrong?

Это было полезно?

Решение

Automatic variables like a1 will have an address only when the function containing them, main in this case, has begun executing. The first few instructions of the function, called its prologue, allocate this space by, for example, subtracting an appropriate amount from the stack pointer on systems where stacks grow from high addresses toward low addresses.

If you type b main and then run, this should run enough of main so that you can print the addresses of some or all of those variables. Note that, depending on optimizations done by the compiler, some variables might be placed in registers or might not get allocated at all, and will not have an address.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top