Hi all,
I know it's been a while since I posted anything, but I wanted to let you know that
I have started creating videos on C++. I use these in conjunction with my sessions with students.
Check them out on youtube:
https://www.youtube.com/watch?v=sfTW0cNBaTs
Keep in mind that I am using a separate username to upload videos: concord.spark.tutor
In the future, I plan to move over to this email address, completely.
If you are interested in learning C++ with me, email me at: sparkprogrammer@gmail.com
See you all,
Joe
Spark's "Grape" Ideas
Wednesday, 6 August 2014
Tuesday, 20 May 2014
C++ - Selection Sort
I was showing someone how to do the selection sort in C++ a few weeks ago. I thought I'd also post the explanation for everyone else to see.
This shows the process for sorting an array of five elements using the selection sort method:
The array of numbers are:
A selection sort behaves in the following way:
Pass One:
During pass one, we are going to find the LOWEST number and have it switch places with the first element in the array. Now, here's how we start looking:
The green arrow is pointing at the second number in the list. For now, we're going to assume that the first element is the lowest in the list.
This shows the process for sorting an array of five elements using the selection sort method:
The array of numbers are:
A selection sort behaves in the following way:
Pass One:
During pass one, we are going to find the LOWEST number and have it switch places with the first element in the array. Now, here's how we start looking:
The green arrow is pointing at the second number in the list. For now, we're going to assume that the first element is the lowest in the list.
Now, 5 is less than 21, so we make the black arrow point to the number 5 - to keep track of the lowest number we have so far.
The green arrow, now points at 2. 2 is less than 5, so again we move the black pointer and have it point at 2 to keep track of it as the lowest number.
Now, 16 is being compared with the number 2. 16 is not less than 2, so the black arrow doesn't change positions, only the green one does. Keep in mind that the green arrow is the number we are comparing with to see if it is lower than the one pointed to by the black arrow.
The green arrow now points at 8, which is still not less than 2. Now, we have determined that the number 2 is the lowest number in the list - which the black arrow is pointing at. What we do is have the number 2 swap places with the value of the first element in the list - 21, like so:
![]() |
| The value of the third element swaps places with the value of the first element. |
This completes pass one.
Pass Two:
On pass two, we have already found the smallest number in the list and we've moved it to the beginning of the list. Since we now know that 2 is at the start of the list, we can begin checking the elements starting from the second element in the list; we make the black arrow point to the second element in the list.
Notice that on Pass Two, we start from the second element in the list. On pass one, we were finding out what the lowest value in the list was. On this second pass, we are going to find out the second-lowest value in the list. For now, we have the black arrow pointing at the number 5, and we are going to compare it with all the other numbers in the list. As you know by now the green arrow will point to each number in turn to see which is less than 5. Since none of the numbers are less than 5, no swap is needed - nothing happens.
This ends Pass Two.
Pass Three:
![]() |
| On the third pass, we start at the third element. |
For now, we again assume that the number in the third position (21) is the lowest value we have so far. The green arrow points to the next value, 16, and we compare it with 21. Now, 16 is lower so we have the black arrow point at it, to "keep track" of the lowest value we've found so far.
This brings us to:
Now, we compare 8 with 16. Of course, 8 is lesser, so we have the black arrow pointing at the lowest element we've found.
At the end of this THIRD pass, we will put the number 8, which is the third-lowest value into the third element of the array:
![]() |
| Pass Three: Third Lowest Element in the entire list is found. |
This ends Pass Three.
Pass Four:
Now, we compare 21 with 16. No swap is needed. The array is now sorted. This ends pass four and also, the entire sorting process. Keep in mind that even though we had 5 elements to sort, we only needed 4 passes. This holds true for ANY array size. If you have 100 elements to sort, you will only need 99 passes to fully sort the array.
The C++ code is shown below - the variable names are blackArrow and greenArrow, so that it will be easier to follow the code using the explanation I've written above.
int array[] = { 21, 5, 2, 16, 8 };
int blackArrow = 0; //Will hold the index of the lowest value in the list each pass
int greenArrow = 0; //Will hold the index of the element we are comparing with the black arrow
for (int pass = 0; pass < 4; pass++)
{
blackArrow = pass;
for (int greenArrow = blackArrow + 1; greenArrow < 5; greenArrow++)
{
if (array[greenArrow] < array[blackArrow])
{
blackArrow = greenArrow;
}
}
//If pass is 0, then the lowest value found will be swapped with the value in array[0].
//If pass is 1, then the second-lowest value found will be swapped with the value in array[1].
//If pass is 2, then the third-lowest value found will be swapped with the value in the third position - array[2].
//If pass is 3, then the fourth-lowest value found will switch places with the value in the array's fourth position.
int temp = array[pass];
array[pass] = array[blackArrow];
array[blackArrow] = temp;
}
int blackArrow = 0; //Will hold the index of the lowest value in the list each pass
int greenArrow = 0; //Will hold the index of the element we are comparing with the black arrow
for (int pass = 0; pass < 4; pass++)
{
blackArrow = pass;
for (int greenArrow = blackArrow + 1; greenArrow < 5; greenArrow++)
{
if (array[greenArrow] < array[blackArrow])
{
blackArrow = greenArrow;
}
}
//If pass is 0, then the lowest value found will be swapped with the value in array[0].
//If pass is 1, then the second-lowest value found will be swapped with the value in array[1].
//If pass is 2, then the third-lowest value found will be swapped with the value in the third position - array[2].
//If pass is 3, then the fourth-lowest value found will switch places with the value in the array's fourth position.
int temp = array[pass];
array[pass] = array[blackArrow];
array[blackArrow] = temp;
}
That's it, folks! I hope that helps the beginner understand the selection sort - even just a little bit.
If you have any questions, email me at: sparkprogrammer@gmail.com
-Joe
-Joe
Thursday, 15 May 2014
New Announcement - Java Tutoring
Hi all,
I recently started tutoring people in Java. Although, I admit, I'm much more comfortable with C++, I also know quite a bit of Java and have begun tutoring people in it. Sooner or later, I'll start posting Java posts here as well. If you need help understanding Java, let me know and I'll give it a shot!
Joe
I recently started tutoring people in Java. Although, I admit, I'm much more comfortable with C++, I also know quite a bit of Java and have begun tutoring people in it. Sooner or later, I'll start posting Java posts here as well. If you need help understanding Java, let me know and I'll give it a shot!
Joe
Sunday, 4 May 2014
Operator Overloading
First off: What is operator overloading? You are simply providing a version of an operator (+, -, ++, etc..) that will handle YOUR object types.
An operator is just a function that you can call with operator syntax. So, remember that to call a method, you would go:
object.Plus(1);
With operators, you can use operator syntax:
object + 1;
Now, what your version of the + operator does is completely up to you. (I don't recommend this, but if you wanted to be sneaky, you could overload the + operator to do subtraction and the - operator to do addition!)
So, when overloading operators, we can do it two ways:
1. - Have them as MEMBERS of a class - methods
2. - Just have them as regular operator functions that belong to no class and can be called by anyone.
There are differences, between the two, but we'll take a look at each one.
First off, let's have a look at operator overloading where you do NOT make it a class method.
An example would be:
(Line numbers are for reference only.)
Now, what does this do? It overloads the + operator to use it with variables (objects) of class Object. It performs addition, by adding the x and y data members of each operand together and stores the results into a new Object variable which it then returns.
So, later on in main() you could do this:
Object left;
Object right;
Object result = left + right;
Now, notice that the Object 'left' will go into leftOperand and 'right' will go into the rightOperand parameter. That's how it is with operators that are NOT methods of a class.
If you decided to overload the + operator for a class, then it would look something like this:
Now, note that we only need a parameter for the right operand. Where's the left operand? The left operand is the object used to call the + operator.
So, if we do this:
objThree = objOne + objTwo;
You can imagine the statement as looking like this:
objThree = objOne.+(objTwo);
objTwo is passed in as the rightOperand and the + operator is using objOne's x and y data members and adding them to rightOperand's corresponding members.
The result is then stored in Object temp, which is then returned.
(I'll probably continue this in another blog post.)
Joe
An operator is just a function that you can call with operator syntax. So, remember that to call a method, you would go:
object.Plus(1);
With operators, you can use operator syntax:
object + 1;
Now, what your version of the + operator does is completely up to you. (I don't recommend this, but if you wanted to be sneaky, you could overload the + operator to do subtraction and the - operator to do addition!)
So, when overloading operators, we can do it two ways:
1. - Have them as MEMBERS of a class - methods
2. - Just have them as regular operator functions that belong to no class and can be called by anyone.
There are differences, between the two, but we'll take a look at each one.
First off, let's have a look at operator overloading where you do NOT make it a class method.
An example would be:
- Object operator + (const Object &leftOperand, const Object &rightOperand) const
- {
- //Suppose that Class Object has two data members: x and y
- Object temp;
- temp.x = leftOperand.x + rightOperand.x;
- temp.y = leftOperand.y + rightOperand.y;
- return temp;
- }
(Line numbers are for reference only.)
Now, what does this do? It overloads the + operator to use it with variables (objects) of class Object. It performs addition, by adding the x and y data members of each operand together and stores the results into a new Object variable which it then returns.
So, later on in main() you could do this:
Object left;
Object right;
Object result = left + right;
Now, notice that the Object 'left' will go into leftOperand and 'right' will go into the rightOperand parameter. That's how it is with operators that are NOT methods of a class.
If you decided to overload the + operator for a class, then it would look something like this:
- class Object
- {
- public:
- Object operator + (const Object &rightOperand) const;
- //More code here
- };
- Object Object::operator + (const Object &rightOperand) const
- {
- Object temp;
- temp.x = x + rightOperand.x;
- temp.y = y + rightOperand.y;
- return temp;
- }
Now, note that we only need a parameter for the right operand. Where's the left operand? The left operand is the object used to call the + operator.
So, if we do this:
objThree = objOne + objTwo;
You can imagine the statement as looking like this:
objThree = objOne.+(objTwo);
objTwo is passed in as the rightOperand and the + operator is using objOne's x and y data members and adding them to rightOperand's corresponding members.
The result is then stored in Object temp, which is then returned.
(I'll probably continue this in another blog post.)
Joe
Tuesday, 29 April 2014
Preprocessor Directives
Hi all, many new programmers don't really know that #include does, or that a preprocessor even exists, so I decided to post a short section talking about it. Hope it helps.
A preprocessor directive is any line that begins with a hash: # symbol.
The preprocessor is a component that runs BEFORE the compiler and looks for preprocessor directives.
Examples of preprocessor directives include: #include, #define, #endif, etc....
Let's start with #include:
Say, your source file, looks like this:
#include <iostream>
int main()
{
}
That's it. Now, when you Build your project, what happens is that before the compiler runs, the preprocessor starts first. Its job is to look for any lines that begin with #. In this case, we only have one preprocessor directive - #include <iostream>
When the preprocessor sees this, it REPLACES this #include directive with the contents of the iostream header file! Now, your original .cpp file does NOT get modified; the preprocessor will build a temporary copy of your source file with all of the #include lines replaced and THAT is what the compiler actually compiles.
Now, what if <iostream> also has #include lines in it? Then those lines would also be replaced as well with the contents of the files they include (remember, nothing is changed, a new source file is generated). As you can imagine, our above .cpp file of only 4 lines can quickly transform into hundreds of thousands of lines!
Now, let's take a look at another kind of preprocessor directive: #define
A common use of #define is to create constants called Macros.
#define PI 3.14159
Then, let's say later on in main(), we do this:
cout<<PI<<endl;
What happens is the preprocessor will replace the word PI with 3.14159 everytime. Note also that this change will appear only in the temporary file generated by the preprocessor; your original source code .cpp file remains the same.
Here's another example:
#define ARRAY_SIZE 20
int array[ARRAY_SIZE];
In the temporary file that the preprocessor generates, the line:
#define ARRAY_SIZE 20
is gone.
However, the line:
int array[ARRAY_SIZE];
has now been changed to this:
int array[20];
A preprocessor only performs substitution.
We don't use macros much anymore, if we even do. The reason they existed was during the C programming era, programmers did not have the const keyword to create a const variable like: const int PI = 3.14159;
All they had were macros which allowed the preprocessor to replace a symbol with a value. Nowadays, programmers encourage the use of 'const' and knowing about #define is mostly used for backwards compatibility (compatibility with source code written a while back, either in C or in the early days of C++).
The last thing I want to explain about preprocessor directives is the existence of inclusion guards.
First off, why do we need them?
Say that a header file, Object.h, contains the following lines:
class Object
{
int x;
int y;
}
That's it. No include guards, nothing. When you try to include this line later on into main.cpp, the preprocessor will happily oblige you and insert the contents of the file into the temp file. Now, what if you accidentally #include "Object.h" twice? Will the preprocessor insert the code twice? Yes, it will, so the temp file will end up looking like this:
//Contents of temporary main.cpp
class Object
{
int x;
int y;
}
class Object
{
int x;
int y;
}
int main()
{
return 0;
}
Now, when the compiler starts compiling this file, it will generate an error because it will see that you defined the Object class, twice! How can we prevent an error like this?
We can "wrap" the class definition in Object.h inside include guards, like:
#ifndef OBJECT_H
#define OBJECT_H
class Object
{
int x;
int y;
}
#endif
The reason I marked a section green, is because that is the only code the preprocessor inserts. The other lines #ifndef, #define and #endif are only preprocessor directives and do not end up in the temp file (the same goes for any preprocessor directive).
Now, the FIRST time you try to include Object.h, the preprocessor does the following:
- Sees the line: #ifndef OBJECT_H which means "if OBJECT_H is not defined", then:
- #define OBJECT_H which makes the preprocessor "remember" that a symbol called OBJECT_H has been defined. It also records which .cpp file defined the OBJECT_H symbol, in this case: main.cpp
- It includes all the lines highlighted above, until it hits the #endif and then it stops.
The SECOND time, you try to include "Object.h" into the SAME source file: main.cpp, the preprocessor does the following:
- #ifndef OBJECT_H makes it check: "Is OBJECT_H defined by main.cpp?"
- Yes! So the preprocessor skips all the highlighted lines and jumps right down to the #endif. There is nothing else to do, so it goes and does some other task.
Keep in mind that the preprocessor takes note of WHICH source file defined the OBJECT_H symbol.
This is important, because if another file, say: another.cpp tried to #include "Object.h", the preprocessor WOULD include the file, even if you already #include "Object.h" in main.cpp.
An inclusion guard ONLY prevents the SAME source file from #including a header MORE than once, it doesn't matter if two .cpp files included the same source file.
So, now: main.cpp and another.cpp have the class Object in their files. What happens, then? The linker, at the end, will remove any duplicates, so you don't need to worry about that. :)
Joe - sparkprogrammer@gmail.com
A preprocessor directive is any line that begins with a hash: # symbol.
The preprocessor is a component that runs BEFORE the compiler and looks for preprocessor directives.
Examples of preprocessor directives include: #include, #define, #endif, etc....
Let's start with #include:
Say, your source file, looks like this:
#include <iostream>
int main()
{
}
That's it. Now, when you Build your project, what happens is that before the compiler runs, the preprocessor starts first. Its job is to look for any lines that begin with #. In this case, we only have one preprocessor directive - #include <iostream>
When the preprocessor sees this, it REPLACES this #include directive with the contents of the iostream header file! Now, your original .cpp file does NOT get modified; the preprocessor will build a temporary copy of your source file with all of the #include lines replaced and THAT is what the compiler actually compiles.
Now, what if <iostream> also has #include lines in it? Then those lines would also be replaced as well with the contents of the files they include (remember, nothing is changed, a new source file is generated). As you can imagine, our above .cpp file of only 4 lines can quickly transform into hundreds of thousands of lines!
Now, let's take a look at another kind of preprocessor directive: #define
A common use of #define is to create constants called Macros.
#define PI 3.14159
Then, let's say later on in main(), we do this:
cout<<PI<<endl;
What happens is the preprocessor will replace the word PI with 3.14159 everytime. Note also that this change will appear only in the temporary file generated by the preprocessor; your original source code .cpp file remains the same.
Here's another example:
#define ARRAY_SIZE 20
int array[ARRAY_SIZE];
In the temporary file that the preprocessor generates, the line:
#define ARRAY_SIZE 20
is gone.
However, the line:
int array[ARRAY_SIZE];
has now been changed to this:
int array[20];
A preprocessor only performs substitution.
We don't use macros much anymore, if we even do. The reason they existed was during the C programming era, programmers did not have the const keyword to create a const variable like: const int PI = 3.14159;
All they had were macros which allowed the preprocessor to replace a symbol with a value. Nowadays, programmers encourage the use of 'const' and knowing about #define is mostly used for backwards compatibility (compatibility with source code written a while back, either in C or in the early days of C++).
The last thing I want to explain about preprocessor directives is the existence of inclusion guards.
First off, why do we need them?
Say that a header file, Object.h, contains the following lines:
class Object
{
int x;
int y;
}
That's it. No include guards, nothing. When you try to include this line later on into main.cpp, the preprocessor will happily oblige you and insert the contents of the file into the temp file. Now, what if you accidentally #include "Object.h" twice? Will the preprocessor insert the code twice? Yes, it will, so the temp file will end up looking like this:
//Contents of temporary main.cpp
class Object
{
int x;
int y;
}
class Object
{
int x;
int y;
}
int main()
{
return 0;
}
Now, when the compiler starts compiling this file, it will generate an error because it will see that you defined the Object class, twice! How can we prevent an error like this?
We can "wrap" the class definition in Object.h inside include guards, like:
#ifndef OBJECT_H
#define OBJECT_H
class Object
{
int x;
int y;
}
#endif
The reason I marked a section green, is because that is the only code the preprocessor inserts. The other lines #ifndef, #define and #endif are only preprocessor directives and do not end up in the temp file (the same goes for any preprocessor directive).
Now, the FIRST time you try to include Object.h, the preprocessor does the following:
- Sees the line: #ifndef OBJECT_H which means "if OBJECT_H is not defined", then:
- #define OBJECT_H which makes the preprocessor "remember" that a symbol called OBJECT_H has been defined. It also records which .cpp file defined the OBJECT_H symbol, in this case: main.cpp
- It includes all the lines highlighted above, until it hits the #endif and then it stops.
The SECOND time, you try to include "Object.h" into the SAME source file: main.cpp, the preprocessor does the following:
- #ifndef OBJECT_H makes it check: "Is OBJECT_H defined by main.cpp?"
- Yes! So the preprocessor skips all the highlighted lines and jumps right down to the #endif. There is nothing else to do, so it goes and does some other task.
Keep in mind that the preprocessor takes note of WHICH source file defined the OBJECT_H symbol.
This is important, because if another file, say: another.cpp tried to #include "Object.h", the preprocessor WOULD include the file, even if you already #include "Object.h" in main.cpp.
An inclusion guard ONLY prevents the SAME source file from #including a header MORE than once, it doesn't matter if two .cpp files included the same source file.
So, now: main.cpp and another.cpp have the class Object in their files. What happens, then? The linker, at the end, will remove any duplicates, so you don't need to worry about that. :)
Joe - sparkprogrammer@gmail.com
Monday, 31 March 2014
Clearing Confusion....
Hi all,
I recently had a few people point out that I was a bit unclear as to my C++ experience. I am NOT an industry working C++ programmer who has designed sophisticated programs. No, a lot of my C++ knowledge comes from my professors and researching on the internet. I am always accepting new information but please understand that once in a while, you may see a "bad design" or "outdated code usage" here and there. Let me know, and I will address the problem, immediately.
I do offer C++ tutoring services because I DO know the C++ language very well and more importantly, I know how a beginner programmer thinks and his/her problems and stumbling blocks. I offer these services, only because I want to help you learn. If for some reason, you think that I am not giving you the knowledge/experience that you need, I will always refund the money to you and do my best to help you find a replacement for me.
In the future, I will be posting more articles and tutorials and will still continue to accept people for tutoring. However, I want to be clear on one thing: my main goal is to help the beginner and somewhat intermediate C++ programmers; not the experts who want to learn sophisticated concepts/code.
I am sorry for the confusion if I have caused any and I promise that I still will do my best to help each and every one of you that comes to me.
Thank you,
Joe
I recently had a few people point out that I was a bit unclear as to my C++ experience. I am NOT an industry working C++ programmer who has designed sophisticated programs. No, a lot of my C++ knowledge comes from my professors and researching on the internet. I am always accepting new information but please understand that once in a while, you may see a "bad design" or "outdated code usage" here and there. Let me know, and I will address the problem, immediately.
I do offer C++ tutoring services because I DO know the C++ language very well and more importantly, I know how a beginner programmer thinks and his/her problems and stumbling blocks. I offer these services, only because I want to help you learn. If for some reason, you think that I am not giving you the knowledge/experience that you need, I will always refund the money to you and do my best to help you find a replacement for me.
In the future, I will be posting more articles and tutorials and will still continue to accept people for tutoring. However, I want to be clear on one thing: my main goal is to help the beginner and somewhat intermediate C++ programmers; not the experts who want to learn sophisticated concepts/code.
I am sorry for the confusion if I have caused any and I promise that I still will do my best to help each and every one of you that comes to me.
Thank you,
Joe
Friday, 28 March 2014
The "Stack"
Since a lot of beginning programmers rarely hear of the 'stack' (if they even hear about it), I thought I'd post a somewhat introductory description of what the stack is.
  What is the stack? The stack is an area of memory that the operating system gives to you when your program starts up. It is used to hold all of your function’s local variables and parameters. Why is it called a stack? In truth, the stack is not “a stack” but just a chunk of memory we use. However, we do ‘conceptually’ think of it as a stack to aid in understanding it. More specifically, we ‘imagine’ the stack as looking like this:
Now, what’s the green arrow? That is the “stack pointer.” Our program uses it to keep track of where the top of the stack is. Anything below the stack pointer is memory that is in use (either for variables, parameters or something); the memory above the stack pointer is completely free. Everytime we add something to the stack, the pointer goes up until it’s right above the top of the newly added data. This way, our programs can keep track of what’s on the stack and where it can start adding new data onto the stack. Also, you’ll hear these words popping up in your programming studies, so here they are:
“Push” – Means to add something onto the top of the stack.
“Pop” – Means to remove something from the top of the stack.
There is no going “in-between” or “from the bottom.” A stack is a stack. You can only add something to the top or remove something from the top.
Here’s a sample program that should help you understand:
int add(int x, int y)
{
return x + y;
}
int main()
{
  int x;
  int y;
  add(x, y);
  return 0;
}
This is what the stack looks like:
Now when the line: int x; gets executed, the computer makes room on the stack by simply moving the stack pointer UP like:
As you can see, the stack pointer is now on top of the local variable ‘x’.
When int y; executes, ‘y’ will get “pushed” onto the stack making the stack look like:

Now, the function call to add() gets executed: add(x, y);
And we pass in x and y BY VALUE. When we say BY VALUE, a copy of x and y will be put onto the stack like this:
As you can see, add()’s parameters: x and y, ARE variables, BUT they are separate variables with their own copies of the data you pass in. When the add() function’s return statement executes, then the stack pointer “goes back down” indicating that the memory above it is now available. Some people also say that the parameters are “popped” off the stack. So, now the stack looks like this:
As you can see, the memory originally used for add()’s parameters are now “deallocated” – made available for use again. What is the stack pointer? It is merely a variable that contains the starting “address” of the available memory on the stack. Remember that each “byte” of memory has an address (just a number) associated with it. The stack pointer contains the address of the first byte of available free space on the stack. All the other bytes “on top” of this one byte of free space, is free too; all the other bytes “below” this byte is being used.
I hope this helps. Remember that this is just a basic introduction. There are lots of other information out there that you can research. If you need to ask a question, email me at:
sparkprogrammer@gmail.com
Joe – C++ tutor
  What is the stack? The stack is an area of memory that the operating system gives to you when your program starts up. It is used to hold all of your function’s local variables and parameters. Why is it called a stack? In truth, the stack is not “a stack” but just a chunk of memory we use. However, we do ‘conceptually’ think of it as a stack to aid in understanding it. More specifically, we ‘imagine’ the stack as looking like this:
Now, what’s the green arrow? That is the “stack pointer.” Our program uses it to keep track of where the top of the stack is. Anything below the stack pointer is memory that is in use (either for variables, parameters or something); the memory above the stack pointer is completely free. Everytime we add something to the stack, the pointer goes up until it’s right above the top of the newly added data. This way, our programs can keep track of what’s on the stack and where it can start adding new data onto the stack. Also, you’ll hear these words popping up in your programming studies, so here they are:
“Push” – Means to add something onto the top of the stack.
“Pop” – Means to remove something from the top of the stack.
There is no going “in-between” or “from the bottom.” A stack is a stack. You can only add something to the top or remove something from the top.
Here’s a sample program that should help you understand:
int add(int x, int y)
{
return x + y;
}
int main()
{
  int x;
  int y;
  add(x, y);
  return 0;
}
This is what the stack looks like:
Now when the line: int x; gets executed, the computer makes room on the stack by simply moving the stack pointer UP like:
As you can see, the stack pointer is now on top of the local variable ‘x’.
When int y; executes, ‘y’ will get “pushed” onto the stack making the stack look like:

Now, the function call to add() gets executed: add(x, y);
And we pass in x and y BY VALUE. When we say BY VALUE, a copy of x and y will be put onto the stack like this:
As you can see, add()’s parameters: x and y, ARE variables, BUT they are separate variables with their own copies of the data you pass in. When the add() function’s return statement executes, then the stack pointer “goes back down” indicating that the memory above it is now available. Some people also say that the parameters are “popped” off the stack. So, now the stack looks like this:
As you can see, the memory originally used for add()’s parameters are now “deallocated” – made available for use again. What is the stack pointer? It is merely a variable that contains the starting “address” of the available memory on the stack. Remember that each “byte” of memory has an address (just a number) associated with it. The stack pointer contains the address of the first byte of available free space on the stack. All the other bytes “on top” of this one byte of free space, is free too; all the other bytes “below” this byte is being used.
I hope this helps. Remember that this is just a basic introduction. There are lots of other information out there that you can research. If you need to ask a question, email me at:
sparkprogrammer@gmail.com
Joe – C++ tutor
Subscribe to:
Comments (Atom)













