html5
1 / 28

APS105

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.

candy
Télécharger la présentation

APS105

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • intsizeof(<type>): • a builtin function that returns size of a type .

  3. Arrays and Pointers • Consider: int x[] = {9,7,8}; . Memory

  4. Pointer Arithmetic

  5. 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

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

  7. Example of Addition to Pointer . Memory x p x[0] x[1] x[2]

  8. Example of Subtraction from Pointer . Memory x p x[0] x[1] x[2]

  9. 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]

  10. 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]

  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. 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

  17. 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”

  18. Marks Example w Malloc/Free .

  19. Multidimensional Arrays

  20. 2D Arrays • We know how to create this: intmyarray[6]; • How can we create this? .

  21. Initializing 2D Arrays How can we initialize an array like this? .

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

  23. Arrays Stored in “Row Major” Order Memory .

  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 .

More Related