Hilfe für das Speicherlayout
12. November 2012 03:56

Dieses kleine Programm zeigt eindrucksvoll das Speicherlayout unter Linux.

                    /*
 	-Useful program to show the address layout for beginners-

 	tuxwave.net 2010
	Have phun.
 */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

void reversefunc(int blah);

int main(int argc, char *argv[], char *envp[])
{
	int i = 0;
	static int foo1, foo2 = 1000;
	char * chunk1 = (char *)malloc(sizeof(char)*1024);
	char * chunk2 = (char *)malloc(sizeof(char)*4096);

	printf("Equiv: long long int -> quadword || long int -> doubleword || short int -> word || char -> byte\n");
	printf("Size of: long long int: %d long int: %d short int: %d char: %d\n\n", sizeof(long long int), sizeof(long int), sizeof(short int), sizeof(char));

	printf("Size of: float: %d\n", sizeof(float));
	printf("Size of: long double: %d double: %d\n\n", sizeof(long double), sizeof(double));

	printf("Adress of main() is: %#x\n", &main);
	printf("Adress of automatic i (integer) in main() is: %#x\n", &i);
	reversefunc(0);
	printf("Adress of static uninitialized foo1 (integer) in main() is: %#x\n", &foo1);
	printf("Adress of static initialized foo2 (integer) in main() is: %#x\n", &foo2);
	printf("Adress of malloc chunk1 (char *) in main() is: %#x\n", chunk1);
	printf("Adress of malloc chunk2 (char *) in main() is: %#x\n", chunk2);
	printf("Adress of glibc function strcpy() is: %#x\n", &strcpy);
	printf("Adress of systemcall wrapper write() is: %#x\n", &write);

	printf("\n\n"
	".text: code segment (functions)\n"
	".data: initialized data (initialized global variables, initialized static variables)\n"
	".bss: uninitialized data (static variables and global variables)\n"
	"heap: dynamic extension of bss contents (malloc, sbrk)\n"
	"stack: automatic local variables, function parameters, register contents of cpu\n"
	);
}

void reversefunc(int blah)
{
	if(blah != 5)
	{
		printf("Address of int argument blah in reversefunc() is %#x\n", &blah);
		blah++;
		reversefunc(blah);
	}
	return;
}

                    
        
Download