aps105 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
APS105 PowerPoint Presentation
Download Presentation
APS105

Loading in 2 Seconds...

play fullscreen
1 / 28

APS105 - PowerPoint PPT Presentation


  • 149 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'APS105' - candy


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
aps105

APS105

Malloc and 2D Arrays

(text Ch6.4, Ch10.2)

datatype size
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
Arrays and Pointers
  • Consider:

int x[] = {9,7,8};

.

Memory

pointer arithmetic1
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
Pointer Adding/Subtracting
  • Result is scaled by the type size

.

  • Why is this useful?
example of addition to pointer
Example of Addition to Pointer

.

Memory

x

p

x[0]

x[1]

x[2]

subtracting pointers
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
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
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 allocation1
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
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
2D Arrays
  • We know how to create this:

intmyarray[6];

  • How can we create this?

.

initializing 2d arrays
Initializing 2D Arrays

How can we initialize an array like this?

.

initializing 2d arrays via loops
Initializing 2D Arrays via Loops

How else can we initialize an array like this?

.

multidimensional arrays1
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
MultiDimensional Array as Parameter
  • Must specify all dimensions except first
    • Examples:

.

dynamic allocation of 2d array
Dynamic Allocation of 2D Array
  • Dynamically allocate a ROWS x COLS Matrix
    • first: lets visualize it:
dynamic allocation of 2d array1
Dynamic Allocation of 2D Array
  • Dynamically allocate a ROWS x COLS Matrix

.

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

.