CSCD-255
2015-02-20


Exploring the Contents of Computer Memory

Computer memory is composed of a large number of binary numeric fields.  It can be thought of as a huge array of 8-bit bytes, and the memory address acts like an array subscript.

In response to the user's program, C allocates those bytes for use as variables, assigning the correct number of bytes for each data type, as seen in the 9 Jan web page

Data type name

   Size in bytes

   Conversion character

char 1 %c
short int 2 %hd
long int 4 %ld
int 4 %d
float 4 %f
double 8 %lf

We have also seen the use of character arrays to contain character string information.  In addition, we have just finished covering one-dimensional arrays of the other data types.

When you reference an array just by its name, you are actually using it as a pointer, referencing where in memory that block of memory begins.  In the lecture covered by the 3 Feb web page, there is an example of setting up character arrays to examine gets, strcpy, and strcat.

The specimen program was intentionally broken, reducing the sizes of the arrays to generate buffer overflows.  The explicit memory addresses are shown at the beginning.
broken.txt   broken.c   broken.exe

int main()
{
   char buffer[40];          // Line being built
   char front[10];           // First name, middle initial
   char back[10];            // Last name
   char year[10];            // Freshman, sophomore, etc.

   printf("Four array addresses:  %d %d %d and %d\n",
          buffer, front, back, year);
   printf("Four array addresses:  %#x %#x %#x and %#x\n\n",
          buffer, front, back, year);
When executed, this generates the following initial output:
Four array addresses: 2686712 2686702 2686692 and 2686682
Four array addresses: 0x28fef8 0x28feee 0x28fee4 and 0x28feda

The decimal output makes it really easy to see the allocation of blocks of 10 bytes:
2686682 is the beginning of the array year
2686692 is the beginning of the array back
2686702 is the beginning of the array front, and
2686712 is the beginning of the array buffer, which extends for 40 bytes.

Similarly, we can declare some int arrays and examine the addresses:
intArrays.txt   intArrays.c   intArrays.exe

Specimen Run:
Four addresses in decimal:
k0: 2686748
k1: 2686708
k2: 2686668
k3: 2686628

40 bytes are allocated to array k1, each cell has 4 bytes

Link to page on pointers