bounds.c
changeset 3 2fdec3c1da6a
parent 2 35178e8b7de3
child 4 4dcc249cbe07
--- a/bounds.c	Wed Jan 12 22:07:08 2011 +0100
+++ b/bounds.c	Wed Jan 12 23:09:48 2011 +0100
@@ -1,6 +1,17 @@
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <signal.h>
+
+// defined inline to avoid using the stack, but probably
+// the stack is used anyway, since we call other functions
+#define OUTPUT(step) \
+	    { if (*step == '\0') continue; \
+	    printf("%p: ", step); \
+	    if (isalnum(*step)) putchar(*step); \
+	    else printf("<%02x>", *step); \
+	    putchar('\n'); }
 
 extern char  etext, edata, end;
 
@@ -8,8 +19,28 @@
 {
     char *step = NULL;
 
-    char *value_string = "Hallo";
-    int   value_int    = 0x7777;
+    /* Dieser String (nicht die Variable) liegt im Datensegment */
+    char *local_hallo = "Hallo";
+
+    /* Diese Variable liegt komplett im Datensegement */
+    static char static_hallo[] = "HALLO";
+
+    /* Und diese Variable liegt auch auf dem Stack und hier sollten
+     * wir unseren eventuell uebergebenen Commandline-Parameter finden.
+     */
+
+    char argument[1024];
+    if (argc > 1) {
+	if (strlen(argv[1]) > sizeof(argument) - 1) {
+	    fprintf(stderr, "arg1 is too long, maximum is %d bytes\n", sizeof(argument));
+	    exit(EXIT_FAILURE);
+	}
+	strcpy(argument, argv[1]);
+    }
+    else {
+	fputs("need some argument on command line\n", stderr);
+	exit(EXIT_FAILURE);
+    }
 
     printf("First address beyond:\n");
     printf("    program text segment(etext)      %10p\n", &etext);
@@ -19,25 +50,26 @@
     printf("-----------------------------------------------\n\n");
 
     // text bis end ausgeben
-    for (step = &etext; step < &end; step++) {
-	printf("%p: ", step);
-	if (isalnum(*step)) putchar(*step);
-	else printf("<%02x>", *step);
-	putchar('\n');
+    puts("- START OF INITIALIZED DATA SEGMENT -------");
+    for (step = &etext; step < &end; step++) 
+	OUTPUT(step);
+    puts("- END OF INITIALIZED DATA SEGMENT -------");
+
+    char *p = malloc(10000);
+    strcpy(p, argv[1]);
+
+    printf("end:    %10p\n", &end);
+    printf("malloc: %10p\n", p);
+    printf("local:  %10p\n", &step);
+
+    // alles nach end ausgeben:
+    for (step = &end; 1; step++) {
+	OUTPUT(step);
     }
 
-    //alles nach end ausgeben:
-    step = &end;
-    while (1) {
-	printf("%p: ", step);
-	if (isalnum(*step)) putchar(*step);
-	else printf("<%02x>", *step);
-	putchar('\n');
-	step++;
-    }
 
-    //damit der compiler das nicht weg optimiert
-    printf("%s%d", value_string, value_int);
+    // avoid the warning about unused …
+    local_hallo = static_hallo;
 
     return EXIT_SUCCESS;
 }