nog korter ex2
authorMart Lubbers <mart@martlubbers.net>
Tue, 3 Mar 2015 12:16:14 +0000 (13:16 +0100)
committerMart Lubbers <mart@martlubbers.net>
Tue, 3 Mar 2015 12:16:14 +0000 (13:16 +0100)
ass4/mart/sws1-assignment4-s4109503-s4202015/ex2 [new file with mode: 0755]
ass4/mart/sws1-assignment4-s4109503-s4202015/exercise1.c
ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2.c

diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/ex2 b/ass4/mart/sws1-assignment4-s4109503-s4202015/ex2
new file mode 100755 (executable)
index 0000000..829243c
Binary files /dev/null and b/ass4/mart/sws1-assignment4-s4109503-s4202015/ex2 differ
index 2615c75..3444eea 100644 (file)
@@ -3,10 +3,10 @@
 
 int main(void)
 {
-       size_t block = 0;
-       char *b, d = 1;
-       for(size_t i = 1 << 31; i >= 1; i /= 2, free(b))
-               d = (b = malloc(block += d * i)) == NULL ? -1 : 1;
-       printf("One malloc can allocate at most %zu bytes\n", block);
+       size_t s = 0;
+       char *buf, dir = 1;
+       for(size_t i = 1 << 31; i >= 1; i /= 2, free(buf))
+               dir = (buf = malloc(s += dir * i)) == NULL ? -1 : 1;
+       printf("One malloc can allocate at most %zu bytes\n", s);
        return 0;
 }
index 4343e61..5a3f570 100644 (file)
@@ -5,9 +5,9 @@
 void heap_attack(void)
 {
        char *b = malloc(1);
-       while(memcmp(--b, "s4109503", 8) != 0);
-       b += 8;
-       while(memcmp(b, "s4202015", 8) != 0)
+       while(memcmp(--b, "4109503", 8) != 0);
+       b += 7;
+       while(memcmp(b, "4202015", 8) != 0)
                *b++ = ' ';
 }
 
@@ -19,8 +19,8 @@ int  main(void)
        char *s2 = malloc(8);
        if(s2 == NULL)
                return -1;
-       strcpy(s1, "s4109503");
-       strcpy(s2, "s4202015");
+       strcpy(s1, "4109503");
+       strcpy(s2, "4202015");
        heap_attack();
        printf("student 1: \"%s\"\n", s1);
        printf("student 2: \"%s\"\n", s2);