diff --git a/week07/ex1.txt b/week07/ex1.txt new file mode 100644 index 0000000..5f3e579 --- /dev/null +++ b/week07/ex1.txt @@ -0,0 +1,2 @@ + text data bss dec hex filename + 53740 7528 1568 62836 f574 dwm diff --git a/week07/ex2.c b/week07/ex2.c new file mode 100644 index 0000000..31e5f37 --- /dev/null +++ b/week07/ex2.c @@ -0,0 +1,14 @@ +#include +#include + +int main() { + int n; + scanf("%d", &n); + int *arr = malloc(sizeof(int) * n); + for (int i = 0; i < n; ++i) { + arr[i] = i; + printf("%d ", arr[i]); + } + printf("\n"); + free(arr); +} diff --git a/week07/ex2.out b/week07/ex2.out new file mode 100755 index 0000000..10ab6b9 Binary files /dev/null and b/week07/ex2.out differ diff --git a/week07/ex2.sh b/week07/ex2.sh new file mode 100755 index 0000000..ba7d01d --- /dev/null +++ b/week07/ex2.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex2.c -o ex2.out +./ex2.out diff --git a/week07/ex3.c b/week07/ex3.c new file mode 100644 index 0000000..30db038 --- /dev/null +++ b/week07/ex3.c @@ -0,0 +1,50 @@ +#include +#include +#include + +int main(){ + //Allows you to generate random number + srand(time(NULL)); + + // Allows user to specify the original array size, stored in variable n1. + printf("Enter original array size:"); + int n1=0; + scanf("%d",&n1); + + //Create a new array of n1 ints + int* a1 = malloc(sizeof(int) * n1); + int i; + for(i=0; i +#include +#include + +// Assuming that previous size is provided, since there is no standard way to know the size of the allocated block +void* myrealloc(void* ptr, size_t prev_size, size_t new_size) { + if (ptr == NULL) prev_size = 0; + + char *res = malloc(new_size); + size_t copy_size = (prev_size < new_size ? prev_size : new_size); + + for (size_t i = 0; i < copy_size; ++i) { + res[i] = ((char*) ptr)[i]; + } + + free(ptr); + + return res; +} + +// test myrealloc using ex3 +int main() { + //Allows you to generate random number + srand(time(NULL)); + + // Allows user to specify the original array size, stored in variable n1. + printf("Enter original array size:"); + int n1=0; + scanf("%d",&n1); + + //Create a new array of n1 ints + int* a1 = malloc(sizeof(int) * n1); + int i; + for(i=0; i + +int main() { + char *s; + char foo[] = "Hello World"; + s = foo; + printf("s is %s\n", s); + printf("s[0] is %c\n", s[0]); + return (0); +} diff --git a/week07/ex5.out b/week07/ex5.out new file mode 100755 index 0000000..ef8dc0c Binary files /dev/null and b/week07/ex5.out differ diff --git a/week07/ex5.sh b/week07/ex5.sh new file mode 100755 index 0000000..9f7ffdd --- /dev/null +++ b/week07/ex5.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex5.c -o ex5.out +./ex5.out