• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/97

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

97 Cards in this Set

  • Front
  • Back
address.
Every little part of the computer’s memory is associated with a number that represents its location

-When your
program starts, the computer sets aside a large chunk of memory and then
works closely with the microprocessor itself to assign a bunch of numbers
to the memory. Your program’s variables and your program’s code goes
in this memory. And consider this: If your program sits in memory, each
function sits in a particular place in memory, a place with a number or
address associated with it. In other words, each function has an address.
The two types of Memory used by C++
Heaping and Stacking
Heap
common area of memory that your program allocates for the different functions in your program to use
Stack
Whenever your program calls a function, however, the function gets its own
little private area of memory storage in an area of memory known as a stack.
The reason that this is called a stack is because it’s treated like a stack of papers: You can put something on the stack, and you can take something off, but you can’t put anything in the middle or take anything from the middle.
The computer uses this stack to keep track of all your function calls.
Further into stacks
Let's suppose there was nothing on the stack to begin with. One of the functions (either the calling function or the called function) would start a stack by placing a return address - and some extra 'plates' might be added, representing any arguments that get passed to the called function (the called function's parameters).

- By the time the called function reaches its return statement it has a big stack of plates. Just before it returns, it removes the plates (one by one) from the top downward so that it can read the return address, written on that very bottom plate. The program then jumps back to that original return address.
Further in he Heap
Heap memory is most often used for 'large' things (e.g. the contents of a file) - or for things that need to 'stay around' in memory so that different functions can share the data (in other words, for NON-local variables). It's also used very frequently to allocate memory for things whose size is not known until run time.
hexadecimal
-way of notating numbers on the computer

-starts with the characters 0x for C++ notation

-0, 1, 2, 3, 4, 5, 6, 7, 8, and 9,
a hex number consists of these digits plus six
more: A, B, C, D, E, and F.

-16 digits

-adding 1 to 0000009 produces 000000A

a 16 digit counting system
even futher into the stack
What the computer considers the top of the stack
is really the next position in memory. And the way the computer puts a
function on the stack is by putting on the stack the address of where the
computer left off in the preceding function.

-When the computer calls one of your functions, not only does it save the
address of the return location on the stack, it also reserves some space on
the stack for your local variables.
Heap
The heap is a common area in memory where you can store
global variables.
Stack
The stack is the area where the computer stores both the information
about the functions being called and the local variables for those
functions.
data structures
-arrangements of data

ex== queue and stack
queue
-you put data in one end and take it out the other
0x0000001F decimal eqivalent
10 = 16 F= 15 decimal equivalent 31

0x10 makes it understood that hex form is being implemented
Getting a variable’s address
If you want to take the address of (which is computerspeak for find the
address of) the variable NumberOfPotholes, you simply throw an
ampersand, &, in front of it

every variable lives in memory every variable has an address

.
code for printing address
#include <iostream>
using namespace std;
int main()
{
int NumberOfPotholes = 532587;
cout << &NumberOfPotholes << endl;
return 0;
}
What does the & + variable give you?
the hexadecimal number address if where that variable is stored in memory

contents of this call is not teh variable itself but its location expressed in hexadecimal form
A pointer example
string buttah = "we stack various types of cheses"

string is stored in memory

&buttah = hexadecimal location of the string in memory

ptr = &buttah;

ptr points to a location in memory that contains the string "we stack various types of cheses"
what is the relevance of having the hexadecimal location of a variable?
you can use it to modify the
variable itself by using what are called pointer variables
Ponter Variables
just like any other variable except that it stores the address of another
variable.
Using pointer variables
1) specify the type of variable it will point to and then you precede the variable’s name with an asterisk

ex

int *ptr;

Declares a variable points to an integer

contain the address of an integer variable

(pointers are type sensitive as in order to contain the address of a variable it needs to declared under the same type)
the implication of pointers
being able to acess the address of a variable can help you as to the variable

but what else?

i can also acess the information that it points too
Changing a variable by using a pointer
-place an '*' in front of pointer variable name to change the value of what is pointer points too
Example of Changing a variable by Using its pointer
#include <iostream>
using namespace std;
int main()
{
int NumberOfPotholes;
int *ptr;
ptr = &NumberOfPotholes;
*ptr = 6087;
cout << NumberOfPotholes << endl;
return 0;
}
What is it called when an aterisk is placed in front of the ponter?
This process of sticking
the asterisk before a pointer variable is called:

Dereferencing the Pointer
pointer and variables
variables can assigned values through their pointers
example:

string vulnerable;
string *ptr ;
ptr= &vulnerable;
*ptr = "we are alone";
cout << vulnerable<<endl;
string vulnerable;
string *ptr;
*ptr= vulnerable;
*ptr = "we are alone";

what is wrong with this code?
1st creates empty variable
2nd creates pointer to a string
3rd included * is incorrect and pointers need to point to addresses in memory not variables in code

needs to be changed to

ptr = &vulnerable

&vulnerable relates to the variables position in memory vs just the variable in code

4th line correctly states address of where vulnerable sits in memory will be change to accomodate new string
Saving values with pointer variables example
#include <iostream>
using namespace std;
int main()
{
int NumberOfPotholes;
int *ptr = &NumberOfPotholes;
int SaveForLater;
*ptr = 6087;
SaveForLater = *ptr;
cout << SaveForLater << endl;
*ptr = 7000;
cout << *ptr << endl;
cout << SaveForLater << endl;
return 0;
}
Explain

#include <iostream>
using namespace std;
int main()
{
int NumberOfPotholes;
int *ptr = &NumberOfPotholes;
int SaveForLater;
*ptr = 6087;
SaveForLater = *ptr;
cout << SaveForLater << endl;
*ptr = 7000;
cout << *ptr << endl;
cout << SaveForLater << endl;
return 0;
}
int variable is declared

int pointer is declared and assigned to first int variable

2nd int variable declared

1st int variable is assigned through pointer that valued is then assigned to the 2nd variable aslo through the pointer

pointer reassgns first variable

first value of first variable has been saved in second variable and a new value replaces that value in first variable
What types of varibles can pointer variables point to?
they can point to any type
create a pointer variable named pointme and point it to a string called gotit and assign it a string value
string gotIT;
string *pointMe = &gotIT;
*pointMe = "whatever it takes"
#include <iostream>
using namespace std;
int main()
{
string GoodMovie;
string *ptrToString;
GoodMovie = “Best in Show”;
ptrToString = &GoodMovie;
cout << *ptrToString << endl;
return 0;
}
-what the pointer is associated with prints to console window
dereferencing the pointer
-accomplished by outting an ' * ' in front of it

- a dereferenced pointer sent to cout will print the value it refers back to not the address
The Pointer to Access the Individual Parts of the String
#include <iostream>
using namespace std;
int main()
{
string HorribleMovie;
string *ptrToString;
HorribleMovie = “L.A. Confidential”;
ptrToString = &HorribleMovie;
for (unsigned i = 0; i < HorribleMovie.length(); i++)
{
cout << (*ptrToString)[i] << “ “;
}
cout << endl;
return 0;
}
explain the code --

#include <iostream>
using namespace std;
int main()
{
string HorribleMovie;
string *ptrToString;
HorribleMovie = “L.A. Confidential”;
ptrToString = &HorribleMovie;
for (unsigned i = 0; i < HorribleMovie.length(); i++)
{
cout << (*ptrToString)[i] << “ “;
}
cout << endl;
return 0;
}
1) string and pointer to string are initialized

2) for loop

- counter variable: unsigned i = 0

- condition is that i cannot be equal to or exceed length of string HorribleMovie

- i++ denotes that after action inside the curly braces are completed the loop continues but adds one 1 to the counter variable

-in consquence the loop resets as many times untill it fails the condition


[i] = indexes based on the i varaible

() around pointer necessary of else compiler will do brackets before applying the aterisk to variable name

use unsigned and not int
changing individual characters example
(*ptrToString)[5] = ‘X’;.
can pointers change what they point to?
yes example:::

#include <iostream>
using namespace std;
int main()
{
int ExpensiveComputer;
int CheapComputer;
int *ptrToComp;
ptrToComp = &ExpensiveComputer;
*ptrToComp = 2000;
cout << *ptrToComp << endl;
ptrToComp = &CheapComputer;
*ptrToComp = 500;
how do you declare two pointer variables?
You can declare two pointer variables of the same type by putting them
together in a single statement, as you can with regular variables. However,
you must precede each one with an asterisk, as in the following line.

example
int *ptrOne, *ptrTwo;

each pointer needs their own aterisks
dynamic allocation
write code that can cause the computer to allocate space only
after it’s running. The computer allocates this space on the heap. This
process is called dynamic allocation
what type of variable needs to be set for dynamic allocation
To declare a storage bin on the heap, first you need to set up a variable that
will help you keep track of the storage bin. This variable must be a pointer
variable.
Using New
To allocate memory on the heap, you need to do two things: First, declare
a pointer variable. Second, call a function called new. The new function is a
little different from other functions in that you don’t put parentheses around
its parameter. For this reason, it’s actually considered to be an operator.
Other operators are + and - for adding and subtracting integers. These
other operators behave similar to functions, but you don’t use parentheses.
How do you allocate memory on the heap?
declare a pointer variable and call the function new

... no parameters considered an operator life + or -
pointer variable purpose in allocating memory...
hold the address for the variable
fomat for allocating memory using pointers

example :: int type / call pointer later

explain the code.
int *later = new int;

a pointer variable called later is allocating memory for int type variable by scuring its address in memory
example Allocating Memory by Using new
#include <iostream>
using namespace std;
int main()
{
int *ptr = new int;
*ptr = 10;
cout << *ptr << endl;
return 0;
}

Prints 10
what does a pointer variable point to when allocating memory using new funciton?
It’s the memory that was allocated by the
new operator. their is no name for the variable and the only way to acess it is through the point variable
terminology for using new
allocating memory on the heap
Pointers meet Arrays
An array is simply a large storage bin that has multiple slots,each of which holds one item. And if you set up an array that holds pointers, you can store away all these pointers without having to name them individually. And these pointers can point to complex things, called object
Arrays saving memories
if you want to, for example, pass all these variables (which could be quite large, if they’re strings) to a function, you need to pass only the array, not the strings themselves. That step saves memory on the stack
A little over my head but has to do with pointers, pointer variables, funtions possible arrays
In addition to objects and arrays, you can also have a function create and
return a variable. Then, when you get the variable back from the function, you
can use it, and when you are finished with the variable, delete it. Finally, you
can pass a pointer into a function. When you do so, the function can actually
modify the pointer for you. See “Passing Pointer Variables to Functions” and
“Returning Pointer Variables from Functions,” later in this chapter.
Putting a Value in Parentheses to Initialize Memory That
You Allocate
#include <iostream>
using namespace std;
int main()
{
int *ptr = new int(10);
cout << *ptr << endl;
return 0;
}
allocate a space in memory and give it the strign literal "We make millions off dumb children" and print to console
#include <iostream>

using namesapce std;

int main()
{
string *pointMe = new string("We make millions off dumb children" );

cout << *pointMe << endl;

return 0;
}
int *ptr = new int(10);

write in long hand ::
int *ptr = new int;
*ptr = 10;
Techniacal phrase for what is happening when you initailize the value in the new operator....
invoking a constructor...

The reason is that the compiler adds a bunch of code to your program, code that operates behind the
scenes. This code is called the runtime library. The library includes a function that initializes an integer variable if you pass an initial value. The function that does this is known as a constructor. When you run it, you are invoking it.

//jesus
Thus, you are invoking the constructor.
cannot allocate viod type
cannot allocate void type!!!!
Using the new Operator with Strings
#include <iostream>
using namespace std;
int main()
{
string *Password = new string;
*Password = “The egg salad is not fresh.”;
cout << *Password << endl;
return 0;
}
pointer variables hold..
an address of allocated memory
shortcut to string function example find the length of

string *pointMe =string new ("for the money")
->
(*pontMe).legnh();

or *pointMe -> legnth();
what does it mean to free a pointer?
Free memory on top of the heap allocated for a pointer variable

call the delete function
free this pointer

string *mrPointer = new string("we like to party on Sundays");
delete mrPointer;

delete funtion is an operator like new and does not require a parameter
Example Code using pointers, new and delete operators and string funtions on poitners
#include <iostream>
using namespace std;
int main()
{
string *phrase = new string(“All presidents are cool!!!”);
cout << *phrase << endl;
(*phrase)[20] = ‘r’;
phrase->replace(22, 4, “oked”);
cout << *phrase << endl;
delete phrase;
return 0;
}

notice the no aterisk or paraentheses when call string funtions shorthand

also asterisks is used when calling the new and initializing it but the poitner name is all that is needed when deleting
#include <iostream>
using namespace std;
int main()
{
string *phrase = new string(“All presidents are cool!!!”);
cout << *phrase << endl;
(*phrase)[20] = ‘r’;
phrase->replace(22, 4, “oked”);
cout << *phrase << endl;
delete phrase;
return 0;
}
Break it Down

first allocated a new string and initialized it, saving its
address in the pointer variable called phrase

called string funtion onto the variable pointer

printed variable pointer

and the freed the memory
when you "free the pointer" or "delete the pointer" what is actually happening?
-you are freeing the memory it points
what does the pointer point to after it has been freed?
it still points to the same area in memory after its call to deletion

do not attempt to call the pointer unless it has been reset to another variable or recalled to new
Actions after Freeing the pointer
Whenever you free a pointer, a good habit is to set the pointer to the value 0.

I point to nothing at all.
delete ptrToSomething;
ptrToSomething = 0;

whats happening ...
The following code sample shows this. First, this code frees the pointer and
then clears it by setting it to 0:
When do you call delete?
Only call delete on memory that you allocated by using new.
How can funtions modify variables?
--when a pointer variable is passed through a funtion it modify the original variable

--A pointer contains a number, which represents the address of a variable. If
you pass this address into a function and the function stores that address
into one of its own variables, its own variable also points to the same variable
that the original pointer did. Make sense? The pointer variable in main and
the pointer variable in the function both point to the same variable because
both pointers hold the same address.
Example of passing a pointer to a funtion to modify the variable
#include <iostream>
using namespace std;
void ChangesAreGood(int *myparam)
{
(*myparam) += 10;
cout << “Inside the function:” << endl;
cout << (*myparam) << endl;
}
int main()
{
int mynumber = 30;
cout << “Before the function:” << endl;
cout << mynumber << endl;
ChangesAreGood(&mynumber);
cout << “After the function:” << endl;
cout << mynumber << endl;
return 0;
}
When you run this program, you see the following output:
Before the function:
30
Inside the function:
40
After the function:
40
what is another way annotating the address of a varaible without using a pointer
int variable;

changesAregood(&variable)
What must be in the paraentheses in order to modify the variable?
a pointer variable


example

void changesAregood(int *para);
why must a pointer variable be in the parameter when modifying a variable by passing it through the funtion
you need to a pointer to point to the address

when a regular variable is passed through the funtion the value is passed through however when an address is passed through via pointer it changes information at the point in memory
How can funtions modify variables?
--when a pointer variable is passed through a funtion it modify the original variable

--A pointer contains a number, which represents the address of a variable. If
you pass this address into a function and the function stores that address
into one of its own variables, its own variable also points to the same variable
that the original pointer did. Make sense? The pointer variable in main and
the pointer variable in the function both point to the same variable because
both pointers hold the same address.
Example of passing a pointer to a funtion to modify the variable
#include <iostream>
using namespace std;
void ChangesAreGood(int *myparam)
{
(*myparam) += 10;
cout << “Inside the function:” << endl;
cout << (*myparam) << endl;
}
int main()
{
int mynumber = 30;
cout << “Before the function:” << endl;
cout << mynumber << endl;
ChangesAreGood(&mynumber);
cout << “After the function:” << endl;
cout << mynumber << endl;
return 0;
}
When you run this program, you see the following output:
Before the function:
30
Inside the function:
40
After the function:
40
what is another way annotating the address of a varaible without using a pointer
int variable;

changesAregood(&variable)
What must be in the paraentheses in order to modify the variable?
a pointer variable


example

void changesAregood(int *para);
why must a pointer variable be in the parameter when modifying a variable by passing it through the funtion
you need to a pointer to point to the address

when a regular variable is passed through the funtion the value is passed through however when an address is passed through via pointer it changes information at the point in memory
when builing a funtion in order to change a variable you pass an:
address
the funtion header in a funtion that modifys a variable uses a :
pointer variable
you can make a pointer variable by adding what sympbol
an aterisks ' * '
Using a Function to Modify a String Passed into It by
Using Pointers
#include <iostream>
using namespace std;
void Paranoid(string *realmessage)
{
(*realmessage)[6] = ‘i’;
realmessage->replace(9, 1, “”);
realmessage->insert(18, “ad”);
realmessage->replace(15, 2, “in”);
realmessage->replace(23, 7, “!”);
realmessage->replace(4, 3, “ali”);
}
int main()
{
string message = “The friends are having dinner”;
cout << message << endl;
Paranoid(&message);
cout << message << endl;
return 0;
}
Returning a Pointer from a String Involves Using an
Asterisk in the Return Type
#include <iostream>
#include <sstream>
using namespace std;
string *GetSecretCode()
{
string *code = new string;
code->append(“CR”);
int randomnumber = rand();
ostringstream converter;
converter << randomnumber;
code->append(converter.str());
code->append(“NQ”);
return code;
}
int main()
{
string *newcode;
int index;
for (index = 0; index < 10; index++)
{
newcode = GetSecretCode();
cout << *newcode << endl;
}
return 0;
}
In this code,
Returning a Pointer from a String Involves Using an
Asterisk in the Return Type
specify the type followed by an asterisk at the beginning of
the function header.

main function, we created a pointer to a string
My function is returning a pointer to a string, and we needed the pointer and
the string to match. When we used the string, we had to dereference it.
Just as the parameters to a function are normally values, a function normally
returns a value. In the case of returning a pointer, the function is still returning
just a value — it is returning the value of the pointer, which is a number
representing an address.
Just as the parameters to a function are normally values, a function normally
returns a value. In the case of returning a pointer, the function is still returning
just a value — it is returning the value of the pointer, which is a number
representing an address.
Never return from a function the address of a local variable in the function.
The local variables live in the stack space allocated for the function, not in
the heap. When the function is finished, the computer frees the stack space
used for the function, making room for the next function call. If you try this,
the variables will be okay for a while, but after enough function calls that
follow, the variable’s data will get overwritten. Wiped out. Gone to the great
variable home in the sky.
Never return from a function the address of a local variable in the function.
The local variables live in the stack space allocated for the function, not in
the heap. When the function is finished, the computer frees the stack space
used for the function, making room for the next function call. If you try this,
the variables will be okay for a while, but after enough function calls that
follow, the variable’s data will get overwritten. Wiped out. Gone to the great
variable home in the sky.
Random numbers and strings
#include <sstream>

int randomnumber = rand();
ostringstream converter;
converter << randomnumber;
int randomnumber = rand();
The first of these creates a random number
by calling a function called rand. You get
back from this function an integer, which is
random.
ostringstream converter;
converter << randomnumber;
The next one creates a variable of
a type called ostringstream, which is a
type that’s handy for converting numbers to
strings. A variable of this type has features
similar to that of a console. You can use the
insertion operator, <<, except instead of
going to the console, anything you write goes
into the string itself. But this isn’t just any old
string; it’s a special string of type ostringstream
(which comes from the words output,
string, and stream; usually things that allow the
insertion operator << or the extraction operator
>> to perform input and output are called
streams).
code->append(converter.str());
After we do this, we can add the
resulting string onto our string variable called
code. To do that, we use the line
code->append(converter.str());
code->append(converter.str());
The part inside parentheses, converter.
str(), returns an actual string version
of the converter variable. And that we can
easily append to our code variable by using
the append function. It’s kind of tricky, but it
works quite nicely.
sending an address to a funtion with a variable pointer in the para will potentially
change the value of the variable
int main()
{
string *newcode;
int index;
for (index = 0; index < 10; index++)
{
newcode = GetSecretCode();
cout << *newcode << endl;
}
return 0;
}

what is going on,,,
a a string pointer variable declared

the pointer variable is then set equal to the funtion GetSecretCode

getSecretCode has to return a series of pointers
Dereferencing Your Return Value Immediately So You
Don’t Need to Use It as a Pointer
#include <iostream>
using namespace std;
string *GetNotSoSecretCode()
{
string *code = new string(“ABCDEF”);
return code;
}
int main()
{
string newcode;
int index;
for (index = 0; index < 10; index++)
{
newcode = *GetNotSoSecretCode();
cout << newcode << endl;
}
return 0;
}
#include <iostream>
using namespace std;
string *GetNotSoSecretCode()
{
string *code = new string(“ABCDEF”);
return code;
}
int main()
{
string newcode;
int index;
for (index = 0; index < 10; index++)
{
newcode = *GetNotSoSecretCode();
cout << newcode << endl;
}
return 0;
}
by placing the call to the funtion with an aterisk which derefernces the pointer when we want to use the value
With References, You Don’t Need Pointers!
#include <iostream>
using namespace std;
void MessMeUp(int &myparam)
{
myparam = myparam * 2 + 10;
}
int main()
{
int mynumber = 30;
MessMeUp(mynumber);
cout << mynumber << endl;
return 0;
}
& in param modify the variable by passing it through the function
A reference
another way of specifying a parameter in a function whereby the function
can modify the original variable. Instead of following the parameter type
with an asterisk, *, to denote a pointer, you follow it with an ampersand, &.
Then, throughout your function, you can use the parameter just as you
normally would, not as a pointer.
what would you have to do modify the variable...
send a pointer of the variable to a funtion (either &varaible name or send pointer variable with out * to indicate an address ) with a pointer in the param
example of using a reference to change the variable
#include <iostream>
using namespace std;
void MessMeUp(int &myparam)
{
myparam = myparam * 2 + 10;
}
int main()
{
int mynumber = 30;
MessMeUp(mynumber);
cout << mynumber << endl;
return 0;
}