:
CSci 1113
C/C++ for Scientists and Engineers
Section 010 - evening class
Pointers, Dynamic Arrays, Linked Lists
Prof. Eric Van Wyk and Ted Kaminski
Spring 2010
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 1 / 27
MyString, cont’d
:
Recap
Introduced pointer type.
char *str;
Comparison with fixed-size arrays.
Lets us have dynamically-sized arrays.
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 2 / 27
MyString, cont’d
:
Memory diagram
void f()
{
int x;
int a[3];
int *b;
}
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 3 / 27
MyString, cont’d
:
Using dynamic arrays
int a[3];
int *b;
b = new int[3];
a[0] = 2;
b[0] = 2;
delete[] b;
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 4 / 27
MyString, cont’d
:
Stack memory management
C++ constructs all variables that come into scope
And destructs all variables going out of scope
Simple, predictable, safe.
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 5 / 27
MyString, cont’d
:
MyString
Implementation
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 6 / 27
This
preview
has intentionally blurred sections.
Sign up to view the full version.
Pointer Basics
:
We’ve seen some already.
Declaration.
Allocation/deletion of arrays.
Use of arrays.
Pointers are more than dynamic arrays.
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 7 / 27
Pointer Basics
:
&
“Address of” operator.
Obtain a pointer to something that already exists.
Unlike
new
where we create something.
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 8 / 27
Pointer Basics
:
*
“Dereference” operator.
Make use of what the pointer points to.
int *x = ...;
x
is a pointer
*x
is an integer
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 9 / 27
Pointer Basics
:
Pointer values
int *p = ...;
p = ...;
changes where
p
points.
*p = ...;
changes the memory where
p
points, which is
an integer.
c Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 10 / 27
Pointer Basics
:
new
&
gives us the address of something existing.

This is the end of the preview.
Sign up
to
access the rest of the document.
- Spring '08
- Staff
- Garbage collection, Van Wyk, evening class Spring
-
Click to edit the document details