bounds.c
changeset 3 2fdec3c1da6a
parent 2 35178e8b7de3
child 4 4dcc249cbe07
equal deleted inserted replaced
2:35178e8b7de3 3:2fdec3c1da6a
     1 #include <stdio.h>
     1 #include <stdio.h>
       
     2 #include <string.h>
     2 #include <stdlib.h>
     3 #include <stdlib.h>
     3 #include <ctype.h>
     4 #include <ctype.h>
       
     5 #include <signal.h>
       
     6 
       
     7 // defined inline to avoid using the stack, but probably
       
     8 // the stack is used anyway, since we call other functions
       
     9 #define OUTPUT(step) \
       
    10 	    { if (*step == '\0') continue; \
       
    11 	    printf("%p: ", step); \
       
    12 	    if (isalnum(*step)) putchar(*step); \
       
    13 	    else printf("<%02x>", *step); \
       
    14 	    putchar('\n'); }
     4 
    15 
     5 extern char  etext, edata, end;
    16 extern char  etext, edata, end;
     6 
    17 
     7 int main(int argc, char **argv)
    18 int main(int argc, char **argv)
     8 {
    19 {
     9     char *step = NULL;
    20     char *step = NULL;
    10 
    21 
    11     char *value_string = "Hallo";
    22     /* Dieser String (nicht die Variable) liegt im Datensegment */
    12     int   value_int    = 0x7777;
    23     char *local_hallo = "Hallo";
       
    24 
       
    25     /* Diese Variable liegt komplett im Datensegement */
       
    26     static char static_hallo[] = "HALLO";
       
    27 
       
    28     /* Und diese Variable liegt auch auf dem Stack und hier sollten
       
    29      * wir unseren eventuell uebergebenen Commandline-Parameter finden.
       
    30      */
       
    31 
       
    32     char argument[1024];
       
    33     if (argc > 1) {
       
    34 	if (strlen(argv[1]) > sizeof(argument) - 1) {
       
    35 	    fprintf(stderr, "arg1 is too long, maximum is %d bytes\n", sizeof(argument));
       
    36 	    exit(EXIT_FAILURE);
       
    37 	}
       
    38 	strcpy(argument, argv[1]);
       
    39     }
       
    40     else {
       
    41 	fputs("need some argument on command line\n", stderr);
       
    42 	exit(EXIT_FAILURE);
       
    43     }
    13 
    44 
    14     printf("First address beyond:\n");
    45     printf("First address beyond:\n");
    15     printf("    program text segment(etext)      %10p\n", &etext);
    46     printf("    program text segment(etext)      %10p\n", &etext);
    16     printf("    initialized data segment(edata)  %10p\n", &edata);
    47     printf("    initialized data segment(edata)  %10p\n", &edata);
    17     printf("    uninitialized data segment (end) %10p\n", &end);
    48     printf("    uninitialized data segment (end) %10p\n", &end);
    18     printf("    size: %d\n", &end - &etext);
    49     printf("    size: %d\n", &end - &etext);
    19     printf("-----------------------------------------------\n\n");
    50     printf("-----------------------------------------------\n\n");
    20 
    51 
    21     // text bis end ausgeben
    52     // text bis end ausgeben
    22     for (step = &etext; step < &end; step++) {
    53     puts("- START OF INITIALIZED DATA SEGMENT -------");
    23 	printf("%p: ", step);
    54     for (step = &etext; step < &end; step++) 
    24 	if (isalnum(*step)) putchar(*step);
    55 	OUTPUT(step);
    25 	else printf("<%02x>", *step);
    56     puts("- END OF INITIALIZED DATA SEGMENT -------");
    26 	putchar('\n');
    57 
       
    58     char *p = malloc(10000);
       
    59     strcpy(p, argv[1]);
       
    60 
       
    61     printf("end:    %10p\n", &end);
       
    62     printf("malloc: %10p\n", p);
       
    63     printf("local:  %10p\n", &step);
       
    64 
       
    65     // alles nach end ausgeben:
       
    66     for (step = &end; 1; step++) {
       
    67 	OUTPUT(step);
    27     }
    68     }
    28 
    69 
    29     //alles nach end ausgeben:
       
    30     step = &end;
       
    31     while (1) {
       
    32 	printf("%p: ", step);
       
    33 	if (isalnum(*step)) putchar(*step);
       
    34 	else printf("<%02x>", *step);
       
    35 	putchar('\n');
       
    36 	step++;
       
    37     }
       
    38 
    70 
    39     //damit der compiler das nicht weg optimiert
    71     // avoid the warning about unused …
    40     printf("%s%d", value_string, value_int);
    72     local_hallo = static_hallo;
    41 
    73 
    42     return EXIT_SUCCESS;
    74     return EXIT_SUCCESS;
    43 }
    75 }