05. Addressing
(Do in on sugon server)
do not forget to use compile script
…or static linking: cc -O0 -static -mno-abicalls -mno-gpopt your_file.c -o your_program_name
printf("%p\n", &variable) — print pointer to variable, which in C strictly means address
- write a program that prints addresses of a pair of local and a pair of global variables
- compare the addresses by sight
interpret this
unlike .data segment in MARS, real world .rdata segment is not at 0x1001000, but just after .text segment ends!)
int *P; — «pointer to integer» variable type (i. e. address of memory part that keeps an integer value)
- Array name is array address, it equals to the array zero element address
i. e. A == &A[0]
- Address arithmetic. Write a program, that
prints character (char) array zero and fifths element addresses
prints integer (int) array zero and fifths element addresses
interpret this
Compare two global arrays A and B addresses by just subtracting them!
Why B-A = 5?
How to calculate real distance between A and B? Try it.
with sizeof() ?
with (int) cast ?
Unexpected: remove both «={…}» initializers and compile code dynamically (cc prog.c -o prog)
There's a chance that A address became larger than B! This is because the order of uninitialized elements in memory is not guaranteed
- Alignment. Compile this:
1 #include <stdio.h> 2 struct STRU { 3 int a; 4 int b; 5 double f; 6 }; 7 8 struct STRU Massive[10]; 9 void main(void) { 10 int i=3, j=4; 11 printf("Size of STRU: %d, STRU[10]: %d\n", sizeof(struct STRU), sizeof(Massive)); 12 printf("%d, %lf\n", Massive[i].a, Massive[j].f); 13 printf("%p, %p, %p\n", &Massive[i].a, &Massive[i].b, &Massive[i].f); 14 15 }
- Rewrite the structure as this:
- compile and run
interptet changes!
(this is because alignment, see .align instructions in assrmber code)
H/W
If you have non-MIPS environment, try these programs there