Chapter 1 Arrays, Pointers, and Structures - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Chapter 1 Arrays, Pointers, and Structures

Description:

Aggregate: collection of objects stored ... Dangling/stale pointers. string *s = new string('hello' ... delete t; // s become invalid //= stale/dangling pointer ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 19
Provided by: Ken695
Category:

less

Transcript and Presenter's Notes

Title: Chapter 1 Arrays, Pointers, and Structures


1
Chapter 1Arrays, Pointers, and Structures
  • Ken Nguyen
  • Spring 2002

2
1.1 What are arrays
  • Aggregate collection of objects stored in one
    unit
  • Arrays indexed collections of identical-type
    objects.
  • Pointers objects are used to access other
    objects.
  • Structures collections of objects that need not
    be of the same type.

3
1.2 Arrays and Strings
  • Arrays can be used in two different ways
    primitive arrays and vectors.
  • String in C is implemented in both structures.
  • First class object(vectors) vs. second class
    object (arrays)

4
Vectors
  • include ltvectorgt
  • Declaration vectorlttypegt identifiers(size)
  • Example vectorltintgt a(3)
  • Vector can be indexed as an array, using
  • Vector member functions size, resize, push_back,
    pop_back, . . .

5
Function calls
  • Call by value
  • int findMax(vectorltintgt a)
  • Call be reference
  • int findMax(vectorltintgt a)
  • int findMax(const vectorltintgt a)

6
Arrays
  • int a2 0,1
  • // multidimensional arrays
  • int b22 1,1, 2,2
  • matrixltintgt x(2,3)

7
Strings
  • include ltstringgt
  • string s hello world!
  • int size s.length()
  • s.c_str() returns a \0 terminated primitive
    character array.

8
1.3 Pointers syntax in C
  • unary operator that returns the address of
    the object.
  • Type identifier // pointer declaration
  • int a 3
  • int iPtr
  • iPtr a // points iPtr to the memory
    address of variable a.
  • iPtr 5 // what is the value of a ???(5)

9
Pointers Cont
  • Precedence rules applied on pointer arithmetic
  • int a 10
  • int ptr a
  • ptr 1 // ptr
  • ptr // returns 11, and //adv. ptr to
    the next //memory slot

a
10
ptr
11
ptr
ptr
11
10
1.4 Dynamic memory management
  • new operator allocates memory and returns a
    pointer to the newly created object.
  • When an object that is allocated by new is no
    longer referenced, applied delete operator on
    this object, through its pointer, to avoid
    memory leakage

11
Figure1.10
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main()
  • string strPtr
  • strPtr new string(hello)
  • coutltlt The string is ltlt strPtrltltendl
  • delete strPtr
  • return 0

12
Dangling/stale pointers
  • string s new string(hello)
  • string t s // t s point to the same block
  • delete t // s become invalid //gtstale/dang
    ling pointer
  • delete s // another problemgtdouble
    //deletion
  • string oop() string s oop return s
  • cout ltltoop()ltltendl // run time error

13
1.5 Reference variables
  • int long_name 0
  • int cnt long_name //cnt is an alias
  • cnt 3 // long_name 0

14
Differences between ref. and pointers
  • void swapPtr(int a, int b)
  • int tmp a
  • a b
  • b tmp
  • int x 3, y 5
  • swapPtr(x, y) // call-by pointer

15
  • void swapRef(int a, int b)
  • int tmp a
  • a b
  • b tmp
  • int x 3, y 5
  • swapRef(x, y) // call-by reference
  • References are implicitly dereferenced

16
Structures
  • Struct Student
  • string firstName
  • string lastName
  • Student a, sPtr // declaring Student
  • a.firstName Mary//accessing member data
  • sPtr a
  • (sPtr).lastName Smith
  • //sPtr-gtlastName Smith // same as above

17
Copying objects
  • Shallow copy a copy of pointers rather than data
    being pointed at
  • Deep copy a copy of data being pointed at rather
    than the pointers.

18
Summary
  • Vectors vs. arrays
  • First class objects vs. second class objects
  • Ways to pass data to functions
  • Pointers
  • Dynamic memory allocation deallocation
  • and . Operators -gt
Write a Comment
User Comments (0)
About PowerShow.com