# Week 1 - Memory ## Team Team name: Date: 16-2-2022 Members | Role | Name | |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------| | **Facilitator** keeps track of time, assigns tasks and makes sure all the group members are heard and that decisions are agreed upon. |Max Huizing | | **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. |Tim Eulink| | **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. |Daan Roes| | **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. |Viggo te Woerd| ## Activities Make sure to have the activities signed off regularly to ensure progress is tracked. Set up a project in CLion to write the small programs needed in some of the activities. ### Activity 1: Memory usage - the *sizeof* operator ``` Data type Size in bytes: char 1 short int 2 int 4 long int 4 long long int 8 float 4 double 8 long double 16 Pointer data type Size in bytes char* 8 int* 8 float* 8 double* 8 void* 8 ``` ### Activity 2: Array and structure sizes This is how you include code listings in your markdown document: ```C typedef struct { char name[80]; int age; } person_t; void size_array(int array[]) { printf("Size of array parameter: %lu\n", sizeof(array)); } void size_struct(person_t p) { printf("Size of p parameter: %lu\n", sizeof(p)); } int main() { int array[10]; person_t bob = {.name = "Bob", .age = 22}; printf("Size of int array: %lu\n", sizeof(array)); printf("Size of person_t structure: %lu\n", sizeof(bob)); size_array(array); size_struct(bob); } ``` Record your answer here ``` Size of int array: 40 Size of person_t structure: 84 Size of array parameter: 8 Size of p parameter: 84 ``` ### Activity 3: Memory addresses ```C int main( void ) { int a = 0xA0B0C0D0; short int b = 0x7856; const char * s = "Hello!"; char buf[] = "Pointer"; short int c = 0x3412; printf("int a: %p\n", (void*) &a); printf("short int b: %p\n", (void*) &b); printf("const char *s: %p\n", (void*) &s); printf("char buf[]: %p\n", (void*) buf); printf("short int c: %p\n", (void*) &c); } ``` Record your answer here 48 65 6c 6c 6f 21 00 ### Activity 4: Observing automatic lifetime ``` Ja het geheugen wordt hergebruikt door de locale variabele. ```` ### Activity 5: Observing the stack ``` Ja, het geheugen wordt hergebruikt in de add_poly functie. ``` ### Activity 6: Leaking local addresses ``` De lokale stack van de functie wordt bewaardt todat iemand de ruimte nodig heeft uit de stack. ``` ### Activity 7: Memory addresses of local variables ``` Nee dit werkt niet omdat de functie een adres van een lokale variable returnt. ``` ### Activity 8: Using malloc ``` int* allocate_memory(int count){ int *ptr = (int*) malloc(sizeof(int)*count); } int main(){ int *longPtr; int *floatPtr; longPtr = (int*) malloc(sizeof(long)); floatPtr = (int*) malloc(sizeof(float)*256); allocate_memory(20); return 0; } ``` ### Activity 9: Using allocated memory as an array - - Het programma kan dan een andere waarde pakken van een variabele. - #include <stdio.h> ``` #include <malloc.h> int main(void) { const int capacity = 20; int *ptr = (int *) malloc(capacity*sizeof (int)); for (int i = 0; i < capacity; i++) { printf("ptr[%a] %d\n", i, ptr[i]); } } Er kunnen 20 ints worden opgeslagen ``` ### Activity 10: Infinite memory? ``` Het programma crasht niet omdat de pointer na 122 keren null wordt. ``` ### Activity 11: Fixing a memory leak Record your answer here ### Activity 12: Dangerous `free`s ```De free-functie zorgt ervoor dat de ruimte waarnaar de pointer verwijst wordt gedeloceerd, dit zorgt voor een beschikbaarheid voor een andere toewijzing. Als een pointer een NULL pointer is, gebeurd er niks.``` ### Activity 13: Using realloc ``` Tien keer aangezien deze forloop tien keer runt. ``` ### Activity 14: Using a dynamically sized buffer Record your answer here ## Looking back ### What we've learnt Dat je ruimte kunt reserveren en verwijderen. ### What were the surprises Dat pointers in functies aan het eind van de fucntie worden weggegooid ### What problems we've encountered We begrepen niet helemaal hoe malloc werkt. ### What was or still is unclear Viggo zijn geaardheid ### How did the group perform? Goed teamwork.