##
Memory Allocation
### Pointers and memory allocation - How does C request dynamic memory when you don't know at compile-time exactly what you will need? - How does C allocate memory? - Automatic: compile arranges for memory to be allocated and initialized for local variables when it is in scope. - Static: memory for static variables are allocated once when program starts. - Dynamic: memory is allocated on the fly as needed.
### Dynamic Memory Allocation in C - Conceptually, it is similar to Java - But you have to do everything!
### malloc, free https://linux.die.net/man/3/malloc
### Hands-on - Start virtualbox - Start the *csc-331* VM
### Setup directory - If you shutdown your VM properly, you should still have the `intro-c` directory. - Confirm that you are in your `home` directory by typing `pwd` and confirm that you are in `/home/student`. - Confirm that you still have `intro-c` using `ls`. - Change into `intro-c`. - If you don't have `intro-c`, create one (take a look at Tuesday's lecture for instruction.) ``` $ ls $ cd intro-c ```
### What points to where!!! - `void *p = malloc(100);` - Allocate and assign the address of 100 bytes of unknown type to pointer variable `p`. - `int *ip = (int *)p;` - Assign the address of the 100 bytes above to pointer variable `ip`, and cast `ip` to type int. - Now you can use `*ip` to manipulate these 100 bytes.
### Compile and Run: Simple Compilation - Make sure that you are still inside `/home/student/intro-c` using `pwd`. ``` $ pwd $ gcc malloc.c $ ./a.out ```
![malloc](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/malloc.PNG)
### So wasteful!!! - An `int` is only 4 to 8 bytes. - How do we know how much to ask for?
![malloc-2](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/malloc-2.PNG)
##
Dynamic Array
### Array size determination - In Java, you can allocate storage for an array at any time in the program, not just when a variable is first initialized. - In C, you need to specify the array size upfront when using the standard [] notation (*rare exception* with C99 standards). - What to do?
### What is an array in C? - In Java, you can allocate storage for an array at any time in the program, not just when a variable is first initialized. - In C, you need to specify the array size upfront when using the standard [] notation (*rare exception* with C99 standards). - What to do?
![array](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/array.PNG)
### Exercise - Create a copy of `array.c` called `array-2.c` - Change type of `numbers` to `double`. - What is the address step now?
![array-2](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/array-2.PNG)
### An array variable ... - ... is in fact pointing to an address containing a value. - ... without the bracket notation and with an index points to the corresponding address of the value at the index.
### An array variable ... - ... is quite similar to a pointer.
![array-3](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/array-3.PNG)
### Array of Strings - String is an array of characters. - What then is an array of strings?
### Draw It!!!
![array-4](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/array-4.PNG)
![array-4-sol](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/array-4-solution.PNG)
##
Structured Types
### Object in C - C has no classes or objects
### Object in C - C has **struct** type (think ancestor of object)
![struct](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/struct.PNG)
### Composite struct Just like Java ``` struct line { struct point start; struct point end; }; .... struct line line1; line1.start.x = 1; line1.start.y = 2; line1.end.x = 3; line1.end.y = 4; ```
##
Functions
### Almost the same as methods in Java, except for one tiny difference ...
![function](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/function.PNG)
![function-3](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/malloc_struct_function/function-3.PNG)
##
Everything is a pointer!!!
##
How do you replicate the fundamental behavior of object and methods in Java with struct, function, and pointer in C?
##
Assignment !!!