SlideShare a Scribd company logo
Pointers and Arrays
until you want to vomit
Memory
Is just a long long string of bytes
When you create a variable and give it a name:
that's just an artifact of the compiler talking to YOU
when it talks to the computer, the only thing it talks about are
addresses.
Be an Address Dealer
Your program can also deal in addresses, if it is appropriate
The whole language of Java thinks it's not.
Other languages, (C, C++) think it is.
That means, there are good reasons on both sides.
Syntax Review: Abstract Example
(type *) myPtr;
This declares an empty variable, of type pointer to type.
When populated, it will contain an address:
myPtr = &SomeTypeOfThing;
myPtr will contain, FFF000, for example.
at FFF000, there will be an object some "type".
Syntax Review: Doubles and Chars
double myDouble, anotherDouble;
myDouble = 9.9;
(double *) myDoublePtr;
myDoublePtr = &myDouble;
read the contents of myDouble via the pointer:
anotherDouble = *myDoublePtr;
anotherDouble = 4.4;
write the contents of myDouble via the pointer:
*myDoublePtr = anotherDouble;
Syntax Review: You can't do that
(double *) myDoublePtr;
double myDouble;
so far, so good ...
myDoublePtr = 8.8; //NO
myDouble = myDoublePtr; // also no
myDouble = 22; // so far, so good
*myDoublePtr = 22; // ?? Could be trouble!!!! Why? (Hard ?)
Pointers & Addresses: Why bother?
Because of the way the operating system has decided to divvy
up memory.
Four types of memory:
code
static
related to functions (The Stack)
related to free memory (The Heap)
Compiler: Calculates how much space
you need.
How much space does the code itself occupy? Code
How about the variables outside of your main, or outside of
any function at all? Global
How big is the stack frame for each of your functions?
And that's all the compiler cares about. All the rest, the news
and the push_backs etc:
All the rest happens at RUN TIME
Functions, yet Again
Remember that your functions, when the are running, are
represented by a stack frame.
The active stack frames pile up,
one on top of the other,
in reverse order of invocation.
The latest is the greatest!
When it's done, it POPS off the stack.
It takes its variables with it.
Function Parameters
When you pass parameters to a function, those are local to the
function.
They are copies (By Default) of the originals in the calling
functions.
That's why changes aren't permanent.
When the stack frame POPS all those changes go away with
the stack frame.
Call By Reference:
Passing around Notes
When you call by reference
(not the default, you need to declare the function with the
& parameters)
Then you are passing an address of a parameter, not a copy of
the thing itself.
It's like passing notes in grade school
"pssst go look in the closet"
Where the answer to the test will be on a piece of paper. etc.
Compiler Space Small, Real Space Big
Imagine a program that is short,
and performs some simple calculation
on lots and lots and lots of records:
Big Huge Investment Bank BHB, inc.
wants to keep track of all the ticker symbols in the US plus all
the ones in Euro countries, plus Japan, plus Canada, plus
Australia etc etc
It will just update stock prices as they come in.
If they stocks are in memory, they are easy to access quickly
but the calculation itself is simple: the heap.
New: Just like in your current HW
new will work on any valid type
new will return a pointer to that type:
catch it in a point to type variable
new will get space on the heap big enough to store 1 or more
of your preferred type.
If that type has a constructor, then new will execute the
constructor for you.
Constructor vs. no Constructor
string * somePtr = new string("yaketyyak");
double * someOtherPtr = new double[4];
Is the string initialized?
Are the doubles initialized?
Constructor vs. no Constructor
string * somePtr = new string("yaketyyak");
double * someOtherPtr = new double[4];
Is the string initialized?
Are the doubles initialized?
How about this familiar one:
char * inputLine = new char[256];
Allocating whole arrays at a time
Question for the class:
What are the differences between
char * line = new char[256];
char line2[256];
Allocating Whole Arrays at a Time
char line2[256];
this will come off the stack or out of static (global)
char * line = new char[256];
it's on the heap - not allocated at compile time
Arrays
The whole array is referred to by a common name
like line on the previous slide
The memory is allocated in one long block
the lowest address is the first element
the highest address is the last element
it's divided up into multiples of whatever the sizeof(type) is
Counting from 0
The lowest address is the start of the array
it's chopped up into chunks the sizeof the type
therefore:
the address of an element at some index equals is calculated
by this formula:
address = start address + ( index multiplied by sizeof(type))
That's why we always count from 0
CUZ: it always comes back to an address in memory
Two Dimensional Arrays
Credit where credit is due
Toothpaste for Dinner
http://toothpastefordinner.com/misc.php
Syntax
For a multi dimensional array
char matrix[3][3];
like on the back ground of this slide!
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
a 2 X 3 matrix
A B C
D E F
matrix[0][0] contains A
A B C
D E F
matrix[1][1] contains E
A B C
D E F
What is the matrix short hand for the
cell containing D?
A B C
D E F
Array Syntax
one more piece of syntax:
int digits[4] = { 34, 543, 654, 4354};

More Related Content

PPTX
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
PDF
Return Oriented Programming
DOCX
Functions in c++
DOC
DOC
PDF
DEFUN 2008 - Real World Haskell
PDF
Real World Haskell: Lecture 7
PPTX
Introduction to Python Part-1
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
Return Oriented Programming
Functions in c++
DEFUN 2008 - Real World Haskell
Real World Haskell: Lecture 7
Introduction to Python Part-1

What's hot (18)

PPT
BayFP: Concurrent and Multicore Haskell
PPT
C language
PPTX
Computer Science Assignment Help
PDF
Hex file and regex cheat sheet
PDF
Bliss: A New Read Overlap Detection Algorithm
PPT
Phyton Learning extracts
PPTX
Pointer in c
PPTX
Demo learn python
PPTX
Pointers in c v5 12102017 1
PPTX
Pointers in C Language
DOC
Arrays In General
PPTX
Recursion
PPT
Chapter Eight(1)
PPTX
Python for loop
PPTX
Fundamentals of Pointers in C
PPTX
C Programming Homework Help
BayFP: Concurrent and Multicore Haskell
C language
Computer Science Assignment Help
Hex file and regex cheat sheet
Bliss: A New Read Overlap Detection Algorithm
Phyton Learning extracts
Pointer in c
Demo learn python
Pointers in c v5 12102017 1
Pointers in C Language
Arrays In General
Recursion
Chapter Eight(1)
Python for loop
Fundamentals of Pointers in C
C Programming Homework Help
Ad

Viewers also liked (7)

PPTX
C programming - Pointers
PPT
Pointers in c
PPTX
2CPP06 - Arrays and Pointers
PDF
Pointers
PPT
Pointers in C
PPT
02 c++ Array Pointer
PDF
C Pointers
C programming - Pointers
Pointers in c
2CPP06 - Arrays and Pointers
Pointers
Pointers in C
02 c++ Array Pointer
C Pointers
Ad

Similar to More Pointers and Arrays (20)

PPT
General Talk on Pointers
PPT
Stack and heap allocation
PDF
Chapter16 pointer
PPT
DOCX
Arrry structure Stacks in data structure
PPT
Pointer
PPT
ch08.ppt
PPTX
20.C++Pointer.pptx
PPT
FP 201 - Unit 6
PPT
Pointers definition syntax structure use
PPT
Lecture2.ppt
PPT
Chapter09-10 Pointers and operations .PPT
PPT
Chapter09-10.PPT
PPT
Link list
PPTX
C++ Pointer | Introduction to programming
PDF
C++ Course - Lesson 3
PPTX
Mca ii dfs u-2 array records and pointer
PPT
Pointers (Pp Tminimizer)
PDF
VIT351 Software Development VI Unit3
PDF
220 runtime environments
General Talk on Pointers
Stack and heap allocation
Chapter16 pointer
Arrry structure Stacks in data structure
Pointer
ch08.ppt
20.C++Pointer.pptx
FP 201 - Unit 6
Pointers definition syntax structure use
Lecture2.ppt
Chapter09-10 Pointers and operations .PPT
Chapter09-10.PPT
Link list
C++ Pointer | Introduction to programming
C++ Course - Lesson 3
Mca ii dfs u-2 array records and pointer
Pointers (Pp Tminimizer)
VIT351 Software Development VI Unit3
220 runtime environments

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Empathic Computing: Creating Shared Understanding
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
cuic standard and advanced reporting.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25 Week I
Empathic Computing: Creating Shared Understanding
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
cuic standard and advanced reporting.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”

More Pointers and Arrays

  • 1. Pointers and Arrays until you want to vomit
  • 2. Memory Is just a long long string of bytes When you create a variable and give it a name: that's just an artifact of the compiler talking to YOU when it talks to the computer, the only thing it talks about are addresses.
  • 3. Be an Address Dealer Your program can also deal in addresses, if it is appropriate The whole language of Java thinks it's not. Other languages, (C, C++) think it is. That means, there are good reasons on both sides.
  • 4. Syntax Review: Abstract Example (type *) myPtr; This declares an empty variable, of type pointer to type. When populated, it will contain an address: myPtr = &SomeTypeOfThing; myPtr will contain, FFF000, for example. at FFF000, there will be an object some "type".
  • 5. Syntax Review: Doubles and Chars double myDouble, anotherDouble; myDouble = 9.9; (double *) myDoublePtr; myDoublePtr = &myDouble; read the contents of myDouble via the pointer: anotherDouble = *myDoublePtr; anotherDouble = 4.4; write the contents of myDouble via the pointer: *myDoublePtr = anotherDouble;
  • 6. Syntax Review: You can't do that (double *) myDoublePtr; double myDouble; so far, so good ... myDoublePtr = 8.8; //NO myDouble = myDoublePtr; // also no myDouble = 22; // so far, so good *myDoublePtr = 22; // ?? Could be trouble!!!! Why? (Hard ?)
  • 7. Pointers & Addresses: Why bother? Because of the way the operating system has decided to divvy up memory. Four types of memory: code static related to functions (The Stack) related to free memory (The Heap)
  • 8. Compiler: Calculates how much space you need. How much space does the code itself occupy? Code How about the variables outside of your main, or outside of any function at all? Global How big is the stack frame for each of your functions? And that's all the compiler cares about. All the rest, the news and the push_backs etc: All the rest happens at RUN TIME
  • 9. Functions, yet Again Remember that your functions, when the are running, are represented by a stack frame. The active stack frames pile up, one on top of the other, in reverse order of invocation. The latest is the greatest! When it's done, it POPS off the stack. It takes its variables with it.
  • 10. Function Parameters When you pass parameters to a function, those are local to the function. They are copies (By Default) of the originals in the calling functions. That's why changes aren't permanent. When the stack frame POPS all those changes go away with the stack frame.
  • 11. Call By Reference: Passing around Notes When you call by reference (not the default, you need to declare the function with the & parameters) Then you are passing an address of a parameter, not a copy of the thing itself. It's like passing notes in grade school "pssst go look in the closet" Where the answer to the test will be on a piece of paper. etc.
  • 12. Compiler Space Small, Real Space Big Imagine a program that is short, and performs some simple calculation on lots and lots and lots of records: Big Huge Investment Bank BHB, inc. wants to keep track of all the ticker symbols in the US plus all the ones in Euro countries, plus Japan, plus Canada, plus Australia etc etc It will just update stock prices as they come in. If they stocks are in memory, they are easy to access quickly but the calculation itself is simple: the heap.
  • 13. New: Just like in your current HW new will work on any valid type new will return a pointer to that type: catch it in a point to type variable new will get space on the heap big enough to store 1 or more of your preferred type. If that type has a constructor, then new will execute the constructor for you.
  • 14. Constructor vs. no Constructor string * somePtr = new string("yaketyyak"); double * someOtherPtr = new double[4]; Is the string initialized? Are the doubles initialized?
  • 15. Constructor vs. no Constructor string * somePtr = new string("yaketyyak"); double * someOtherPtr = new double[4]; Is the string initialized? Are the doubles initialized? How about this familiar one: char * inputLine = new char[256];
  • 16. Allocating whole arrays at a time Question for the class: What are the differences between char * line = new char[256]; char line2[256];
  • 17. Allocating Whole Arrays at a Time char line2[256]; this will come off the stack or out of static (global) char * line = new char[256]; it's on the heap - not allocated at compile time
  • 18. Arrays The whole array is referred to by a common name like line on the previous slide The memory is allocated in one long block the lowest address is the first element the highest address is the last element it's divided up into multiples of whatever the sizeof(type) is
  • 19. Counting from 0 The lowest address is the start of the array it's chopped up into chunks the sizeof the type therefore: the address of an element at some index equals is calculated by this formula: address = start address + ( index multiplied by sizeof(type)) That's why we always count from 0 CUZ: it always comes back to an address in memory
  • 21. Credit where credit is due Toothpaste for Dinner http://toothpastefordinner.com/misc.php
  • 22. Syntax For a multi dimensional array char matrix[3][3]; like on the back ground of this slide!
  • 23. [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]
  • 24. a 2 X 3 matrix A B C D E F
  • 27. What is the matrix short hand for the cell containing D? A B C D E F
  • 28. Array Syntax one more piece of syntax: int digits[4] = { 34, 543, 654, 4354};