Presentation is loading. Please wait.

Presentation is loading. Please wait.

APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).

Similar presentations


Presentation on theme: "APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2)."— Presentation transcript:

1 APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2)

2 Datatype Size Datatypes have varying size: –char: 1B –int: 4B –double: 8B int sizeof( ): –a builtin function that returns size of a type.

3 Arrays and Pointers Consider: int x[] = {9,7,8};. AddrValue 0 4 8 12 16 20 24 Memory

4 Pointer Arithmetic

5 Some operations allowed on pointers – + // result is a pointer – - // result is a pointer – - // result is an int

6 Pointer Adding/Subtracting Result is scaled by the type size. Why is this useful?

7 Example of Addition to Pointer. AddrValue 016 4 8 12 169 207 248 Memory x x[0] x[1] x[2] p

8 Example of Subtraction from Pointer. AddrValue 016 4 8 12 169 207 248 Memory x x[0] x[1] x[2] p

9 Subtracting Pointers Recall: - // 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( ) Example:. AddrValue 016 4 824 12 169 207 248 Memory x x[0] x[1] x[2] p q

10 Comparing Pointers i.e, : // 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:. AddrValue 016 420 824 12 169 207 248 Memory x x[0] x[1] x[2] p q

11 Pointers and Integers Cannot assign an integer value to a pointer –exception: 0 (zero) –example:. NULL –a predefined constant with the value zero –example:.

12 Array/Pointer Parameters

13 Array vs Pointer Parameters.

14 Swap using Pointers.

15 Dynamic Allocation

16 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

17 Malloc and Free #include 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”

18 Marks Example w Malloc/Free.

19 Multidimensional Arrays

20 2D Arrays We know how to create this: int myarray[6]; How can we create this?. 5 0123 0 1 2 3 4

21 Initializing 2D Arrays How can we initialize an array like this?. 0123 08792 19384 25136

22 Initializing 2D Arrays via Loops How else can we initialize an array like this?. 0123 00000 10000 20000

23 Arrays Stored in “Row Major” Order. AddrRowColVal Memory 0123 08792 19384 25136

24 Multidimensional Arrays 2D: think of a table. 3D: think of a book with pages of tables. 4D: think of a set of books with....

25 MultiDimensional Array as Parameter Must specify all dimensions except first –Examples:.

26 Dynamic Allocation of 2D Array Dynamically allocate a ROWS x COLS Matrix –first: lets visualize it:

27 Dynamic Allocation of 2D Array Dynamically allocate a ROWS x COLS Matrix.

28 Dyn. Malloc’d 2D Array as Parameter write a func f that sums all values of a 2D array.


Download ppt "APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2)."

Similar presentations


Ads by Google