Course list http://www.c-jump.com/bcc/
Chances are you also enrolled in a Java course and ready to learn Java programming
CIS-123 covers basic understanding of
the CPU
program in memory
data in memory
data struct that organizes "objects" in memory
relationships between classes of objects via inheritance
interfaces to objects
Basic knowledge:
the CPU constantly runs a program some place in memory
the program is made up of single instructions and that is all the CPU knows
occasionally, the instructions direct the CPU to look at data in memory
put it all together, and you have the key elements of a Von-Neuman architecture machine.
In this course we learn how memory concepts can help us better understand and write computer programs.
We use examples from a mix of programming languages
I personally prefer examples in C++ because they tend to be smaller in size
Some samples will be using the C programming language.
Compare C, C++, and Java "Hello, World!" programs:
|
|
|
Do you notice any syntax commonality between these programs?
One important commonality is the pair of curly braces { }
|
|
Did you know that a pair of curly braces { }
manages program memory
creates a placeholder for local variables
groups together a set of executable statements
A running program means executing a series of CPU commands, often in a loop. For example, continuously reading mouse coordinates and repositioning the mouse cursor on computer screen represents a task that requires hunderds of CPU instructions to execute.
A "running program" also means that a program is loaded into computer memory. Consequently, executable instructions are stored in computer memory during program execution.
(Process Environment image from johnloomis.org )
The text memory segment has machine instructions that the CPU executes.
|
|
The uninitialized data segment contains uninitialized global variables.
The entire segment of memory is filled with zeroes by the program loader.
The stack memory segment contains automatic variables aka local variables.
Heap is where the dynamically allocated variables are stored in a C program.
C++ uses its own facility named free store to dynamically allocate memory.
For example,
#include <iostream>
int main()
{
std::cout << "how many numbers would you like to allocate? ";
int count;
std::cin >> count;
int* memory_block = new int[ count ];
std::cout << "your block of memory begins at address " << memory_block << '\n';
return 0;
}
Any memory declared inside a function is called a local variable::
void f()
{
int count = 0;
}
Let's try an example:
#include <cstdio>
int main()
{
int x = 4;
printf( "x is stored at %p\n", &x );
return 0;
}
#include <cstdio>
int main()
{
int x = 4;
printf( "x is stored at %p\n", &x );
return 0;
}
Q: Why does the %p format display the memory address in hex format?
A: It's the way engineers typically refer to memory addresses.
Okay... Let's try to elaborate a little more on this answer.
You've heard that computers use binary numbers.
The binary numbers have only two digits: 0 and 1.
Despite this simplicity, 0 and 1 can represent numerical values just as good as the decimal numbers:
decimal number: 0, 1, 2, 3, 4, 5, ...
binary number: 0, 1, 10, 11, 100, 101, ...
The internal use of binary numbers forces computers to manipulate types of data that have sizes like
1, 2, 4, 8, 16, 32, 64, and so on.
These numbers are known as powers of 2. For example,
64 = 2 * 2 * 2 * 2 * 2 * 2 = 26
64 equals 2 raised to the 6th power. The binary value of 64 is 1000000.
The binary numbers are extremely unfriendly to humans. For example, decimal 123456 is binary 11110001001000000. So we need some alternative to binaries.
Unfortunately, number 10, which is commonly used by humans, is not a power of two: the nearest powers of 2 are 8 and 16. Both of these are commonly used as numbering bases in programming, called octal numbers and hexadecimal numbers, respectively.
Octal numbers:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...decimal equivalents
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, ...
|
|__note that 8 is an illegal octal digit!
Hexadecimal numbers:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ...decimal equivalents
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F,
|
|__using letters to make up for more digits
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 2A, 2B, 2C, 2D, 2E, 2F, ...
|
|