SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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};

Weitere Àhnliche Inhalte

Was ist angesagt?

Was ist angesagt? (18)

BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
 
C language
C languageC language
C language
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
Bliss: A New Read Overlap Detection Algorithm
Bliss: A New Read Overlap Detection AlgorithmBliss: A New Read Overlap Detection Algorithm
Bliss: A New Read Overlap Detection Algorithm
 
Ch8a
Ch8aCh8a
Ch8a
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
Pointer in c
Pointer in c Pointer in c
Pointer in c
 
Demo learn python
Demo learn pythonDemo learn python
Demo learn python
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Recursion
RecursionRecursion
Recursion
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 
Python for loop
Python for loopPython for loop
Python for loop
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 

Andere mochten auch

C programming - Pointers
C programming - PointersC programming - Pointers
C programming - PointersWingston
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
Pointers in C
Pointers in CPointers in C
Pointers in Cguestdc3f16
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array PointerTareq Hasan
 
C Pointers
C PointersC Pointers
C Pointersomukhtar
 

Andere mochten auch (7)

C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
Pointers
PointersPointers
Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
C Pointers
C PointersC Pointers
C Pointers
 

Ähnlich wie More Pointers and Arrays

C language introduction
C language introduction C language introduction
C language introduction musrath mohammad
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environmentsJ'tong Atong
 
C programming language
C programming languageC programming language
C programming languageMahmoud Eladawi
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As CompDavid Halliday
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As CompDavid Halliday
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointersemartinez.romero
 
C Interview Questions for Fresher
C Interview Questions for FresherC Interview Questions for Fresher
C Interview Questions for FresherJaved Ahmad
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationsonu sharma
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKgr Sushmitha
 
C interview Question and Answer
C interview Question and AnswerC interview Question and Answer
C interview Question and AnswerJagan Mohan Bishoyi
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxfredharris32
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptxaboma2hawi
 
Arrays and pointers
Arrays and pointersArrays and pointers
Arrays and pointersKevin Nguyen
 
Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience
Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience
Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience App Ttrainers .com
 
C language
C languageC language
C languagespatidar0
 

Ähnlich wie More Pointers and Arrays (20)

C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C language introduction
C language introduction C language introduction
C language introduction
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
C programming language
C programming languageC programming language
C programming language
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointers
 
C Interview Questions for Fresher
C Interview Questions for FresherC Interview Questions for Fresher
C Interview Questions for Fresher
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
C interview Question and Answer
C interview Question and AnswerC interview Question and Answer
C interview Question and Answer
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docx
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
C –FAQ:
C –FAQ:C –FAQ:
C –FAQ:
 
Exploitation Crash Course
Exploitation Crash CourseExploitation Crash Course
Exploitation Crash Course
 
Arrays and pointers
Arrays and pointersArrays and pointers
Arrays and pointers
 
C++ Boot Camp Part 2
C++ Boot Camp Part 2C++ Boot Camp Part 2
C++ Boot Camp Part 2
 
Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience
Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience
Űčلم Ű§Ù„ŰšÙŠŰ§Ù†Ű§ŰȘ - Data Sience
 
C language
C languageC language
C language
 

Mehr von emartinez.romero

Family Resource Center Upper Nyack 2008-2009
Family Resource Center Upper Nyack 2008-2009Family Resource Center Upper Nyack 2008-2009
Family Resource Center Upper Nyack 2008-2009emartinez.romero
 
Classes and Inheritance
Classes and InheritanceClasses and Inheritance
Classes and Inheritanceemartinez.romero
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointersemartinez.romero
 
First Look at Pointers
First Look at PointersFirst Look at Pointers
First Look at Pointersemartinez.romero
 

Mehr von emartinez.romero (6)

Family Resource Center Upper Nyack 2008-2009
Family Resource Center Upper Nyack 2008-2009Family Resource Center Upper Nyack 2008-2009
Family Resource Center Upper Nyack 2008-2009
 
Classes and Inheritance
Classes and InheritanceClasses and Inheritance
Classes and Inheritance
 
Arrays
ArraysArrays
Arrays
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointers
 
First Look at Pointers
First Look at PointersFirst Look at Pointers
First Look at Pointers
 
Conficker
ConfickerConficker
Conficker
 

KĂŒrzlich hochgeladen

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂșjo
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

KĂŒrzlich hochgeladen (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

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};