From 548799acae4e7ed35850357c4ba27c542b912256 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 2 Mar 2015 12:19:11 +0100 Subject: [PATCH] ex4 done --- .../exercise1.c | 6 --- .../exercise1.c | 1 + .../exercise2.c | 28 +++++++++++++ .../exercise2c | 1 + .../exercise2e | 3 ++ .../exercise3.c | 42 +++++++++++++++++++ .../exercise3b | 2 + 7 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2.c create mode 100644 ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2c create mode 100644 ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2e create mode 100644 ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3.c create mode 100644 ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3b diff --git a/ass3/mart/sws1-assignment3-s4109503-s4202015/exercise1.c b/ass3/mart/sws1-assignment3-s4109503-s4202015/exercise1.c index 28d6a2c..2baefbc 100644 --- a/ass3/mart/sws1-assignment3-s4109503-s4202015/exercise1.c +++ b/ass3/mart/sws1-assignment3-s4109503-s4202015/exercise1.c @@ -1,14 +1,11 @@ #include #include #include "magicfunction.h" - #define MAX 4194304/sizeof(uint64_t) - uint64_t determine_start() { return 0x567856785678; } - int main(void) { uint64_t filler = 0x123412341234; @@ -18,19 +15,16 @@ int main(void) /*Fill the stack with a filler value*/ for(p = &stack -1; p >= &stack - 1 - MAX; p--) *p = filler; - /*Find the beginning of the function stack with the function*/ determine_start(); for(q = &stack -1; q>=&stack-1-MAX; q--) if(*q == 0x567856785678) break; - /*Run the function and find the last occurance of non stack filler */ magic_function(); for(p = &stack - 1 - MAX; p <= q; p++) if(*p != filler) break; - printf("%td byte stack size used\n", (char*)p-(char*)q); return 0; } diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise1.c b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise1.c index b361d09..2615c75 100644 --- a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise1.c +++ b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise1.c @@ -8,4 +8,5 @@ int main(void) 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); + return 0; } diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2.c b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2.c new file mode 100644 index 0000000..4343e61 --- /dev/null +++ b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2.c @@ -0,0 +1,28 @@ +#include +#include +#include + +void heap_attack(void) +{ + char *b = malloc(1); + while(memcmp(--b, "s4109503", 8) != 0); + b += 8; + while(memcmp(b, "s4202015", 8) != 0) + *b++ = ' '; +} + +int main(void) +{ + char *s1 = malloc(8); + if(s1 == NULL) + return -1; + char *s2 = malloc(8); + if(s2 == NULL) + return -1; + strcpy(s1, "s4109503"); + strcpy(s2, "s4202015"); + heap_attack(); + printf("student 1: \"%s\"\n", s1); + printf("student 2: \"%s\"\n", s2); + return 0; +} diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2c b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2c new file mode 100644 index 0000000..39e2602 --- /dev/null +++ b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2c @@ -0,0 +1 @@ +Strings are null terminated, therefore the 8th byte is \0 diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2e b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2e new file mode 100644 index 0000000..b0e6e7a --- /dev/null +++ b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise2e @@ -0,0 +1,3 @@ +It will deallocate the space and the next allocation could be not in the +neighbourhood of the first(that is free'd). It might get overwritten too, and +therefore you might not be able to find it anymore. diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3.c b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3.c new file mode 100644 index 0000000..f97359c --- /dev/null +++ b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3.c @@ -0,0 +1,42 @@ +#include +#include + +int main(void) /* https://www.kernel.org/doc/Documentation/CodingStyle */ +{ + int i; + int j; + unsigned long long **m; + unsigned long long **mt; + + while(1){ + m = malloc(1000*sizeof(unsigned long long*)); + if(m == NULL) + return -1; + for(i = 0; i<1000; i++){ + m[i] = malloc(1000*sizeof(unsigned long long)); + if(m[i] == NULL) + return -1; + } + + mt = malloc(1000*sizeof(unsigned long long*)); + if(mt == NULL) + return -1; + for(i = 0; i<1000; i++){ + mt[i] = malloc(1000*sizeof(unsigned long long)); + if(mt[i] == NULL) + return -1; + } + + for(i = 0; i < 1000; i++) + for(j = 0; j < 1000; j++) + mt[i][j] = m[j][i]; + + for(i = 0; i<1000; i++){ + free(m[i]); + free(mt[i]); + } + free(m); + free(mt); + } + return 0; +} diff --git a/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3b b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3b new file mode 100644 index 0000000..4f230d2 --- /dev/null +++ b/ass4/mart/sws1-assignment4-s4109503-s4202015/exercise3b @@ -0,0 +1,2 @@ +We run out of space. The matrices are deallocated but the content isn't. It +leaks memory. -- 2.20.1