290 likes | 480 Views
APS105. Malloc and 2D Arrays (text Ch6.4, Ch10.2). Datatype Size. Datatypes have varying size: char: 1B int : 4B double: 8B int sizeof (<type>): a builtin function that returns size of a type. Arrays and Pointers. Consider: int x[] = {9,7,8};. Memory. Pointer Arithmetic.
E N D
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2)
Datatype Size • Datatypes have varying size: • char: 1B • int: 4B • double: 8B • intsizeof(<type>): • a builtin function that returns size of a type .
Arrays and Pointers • Consider: int x[] = {9,7,8}; . Memory
Pointer Arithmetic • Some operations allowed on pointers • <pointer> + <int> // result is a pointer • <pointer> - <int> // result is a pointer • <pointer> - <pointer> // result is an int
Pointer Adding/Subtracting • Result is scaled by the type size . • Why is this useful?
Example of Addition to Pointer . Memory x p x[0] x[1] x[2]
Example of Subtraction from Pointer . Memory x p x[0] x[1] x[2]
Subtracting Pointers • Recall: <pointer2> - <pointer1> // result is an int • Only valid if: • both pointers point to parts of the same array • the result is positive • i.e., ptr2 points to a higher-addr location than ptr1 • Result is divided by sizeof(<type>) • Example: . Memory x p q x[0] x[1] x[2]
Comparing Pointers • i.e, : <pointer1> < <pointer2> // result is an int • Only valid if: • both pointers point to parts of the same array • the result is bool (true or false) • can use ==, !=, <, >, <=, >= • Example: . Memory x p q x[0] x[1] x[2]
Pointers and Integers • Cannot assign an integer value to a pointer • exception: 0 (zero) • example: . • NULL • a predefined constant with the value zero • example: .
Dynamic Allocation • We can create an array of a certain size . • But what if we don’t know #students? • could assume a maximum number of students . • what’s wrong with this approach? • Better solution: dynamic allocation
Malloc and Free • #include <stdlib.h> • the malloc function • means “memory allocation” • reserves space in memory for a number of bytes • you call it to allocate memory when you need it • hence “dynamically”, at run-time, on-demand • the free function • you call it to release or give-back memory • mem that you malloc should eventually be free’ed • What if you forget to free malloc’ed memory? • that’s called a “memory leak”
2D Arrays • We know how to create this: intmyarray[6]; • How can we create this? .
Initializing 2D Arrays How can we initialize an array like this? .
Initializing 2D Arrays via Loops How else can we initialize an array like this? .
Arrays Stored in “Row Major” Order Memory .
Multidimensional Arrays • 2D: think of a table . • 3D: think of a book with pages of tables . • 4D: think of a set of books with... .
MultiDimensional Array as Parameter • Must specify all dimensions except first • Examples: .
Dynamic Allocation of 2D Array • Dynamically allocate a ROWS x COLS Matrix • first: lets visualize it:
Dynamic Allocation of 2D Array • Dynamically allocate a ROWS x COLS Matrix .
Dyn. Malloc’d 2D Array as Parameter • write a func f that sums all values of a 2D array .