Dynamic 2D Array - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Dynamic 2D Array

Description:

Static 2D array: array of same size of arrays. Dynamic 2D array: ... A static 2D array is guaranteed to be in a single block, e.g. int e[2][3]; Instead you could ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 10
Provided by: cpeg
Category:
Tags: array | astatic | dynamic

less

Transcript and Presenter's Notes

Title: Dynamic 2D Array


1
Dynamic 2D Array
  • COMP103 Lab 5

2
Dynamic 2D Array
  • Not the same as static 2D array!
  • Static 2D array array of same size of arrays
  • Dynamic 2D array array of various size of arrays

Static
Dynamic
3
Dynamic 2D Array
  • A static 2D array is guaranteed to be in a single
    block, e.g. int e23
  • Instead you could
  • have int k6
  • exy will be the same
  • as kx 3 y

k5
e12
k4
e11
k3
e10
k2
e02
k1
e01
k0
e00
4
Dynamic 2D Array
  • Each dynamic memory allocation can only allocate
    memory for a single element or an array of
    elements
  • How to construct a dynamic 2D int array?
  • Create an array of pointer to int array (int)
  • Create arrays of int one-by-one

5
Dynamic 2D Array
  • Example (array of 2 x 3)
  • int c new int2
  • c0 new int3
  • c1 new int3

c10 to c12
1060-106B
c00 to c02
1050-105B
1060
1044
c1
1050
1040
c0
Heap memory
6
Dynamic 2D Array
  • Note memory allocation in dynamic 2D array are
    not continuous
  • Example (dynamic 2 x 3 array c and static 2 x 3
    array a)
  • Access to c03 in dynamic 2D array may be
    failed
  • Access to a03 in static 2D array will
    actually accessing a10

7
Dynamic 2D Array
  • Memory deallocation (e.g. int array of 2 x 3)
  • Similar to allocation
  • Delete all arrays of int
  • delete c0
  • delete c1
  • Delete array of pointer to int array
  • delete c
  • Note reverse order of memory allocation!

8
Dynamic Multi-dimensional Array
  • Same principle as dynamic 2D array
  • Order of memory allocation
  • , int, int, int
  • Order of memory deallocation
  • int, int, int,

9
String Comparison
  • String library provided strcmp(), strncmp() for
    char string
  • If you want to develop you own, make sure you
    understand the string concept completely!!
  • String is terminated by null character (\0)
  • Comparison should stop after null character
  • Dont use gt, lt, etc!
Write a Comment
User Comments (0)
About PowerShow.com