Un programa que muestra donde el S.O. pone cada cosa.

Iniciado por MAFUS, 3 Diciembre 2015, 21:44 PM

0 Miembros y 1 Visitante están viendo este tema.

MAFUS

No es mucha cosa, solo un divertimento; pero sirve para ver en qué parte de la memoria pone el S.O. las funciones, las variables locales, las globales y las dinámicas.
Así se puede ver como el sistema separa la memoria de código, la memoria de pila (stack) y la memoria del montón (heap).


#include <stdio.h>
#include <stdlib.h>

char gc;
int gi;

int main(int argc, char *argv[])
{
char c;
int i;
int *p = malloc(sizeof(int));
printf("printf\t= %p\n", &printf);
printf("malloc\t= %p\n", &malloc);
printf("main\t= %p\n", &main);
printf("\n");
printf("gi\t= %p\n", &gi);
printf("gc\t= %p\n", &gc);
printf("\n");
printf("argv\t= %p\n", &argv);
printf("argc\t= %p\n", &argc);
printf("\n");
printf("c\t= %p\n", &c);
printf("i\t= %p\n", &i);
printf("p\t= %p\n", &p);
printf("\n");
for(i = 0; i < argc; ++i)
printf("argv[%i]\t= %p -- %s$\n", i, argv[i], argv[i]);
printf("\n");
printf("p (->)\t= %p\n", p);
free(p);
return 0;
}


P. ej.:

$ ./c 1 12 123 1234 12345
printf   = 0x4004f0
malloc   = 0x400520
main   = 0x40061d

gi   = 0x60105c
gc   = 0x601060

argv   = 0x7ffd71212400
argc   = 0x7ffd7121240c

c   = 0x7ffd71212413
i   = 0x7ffd71212414
p   = 0x7ffd71212418

argv[0]   = 0x7ffd71214551 -- ./c$
argv[1]   = 0x7ffd71214555 -- 1$
argv[2]   = 0x7ffd71214557 -- 12$
argv[3]   = 0x7ffd7121455a -- 123$
argv[4]   = 0x7ffd7121455e -- 1234$
argv[5]   = 0x7ffd71214563 -- 12345$

p (->)   = 0x1e36010