CIS2500 Lab Three

The purpose of today’s lab is to review key concepts related to dynamic memory allocation in C.

Overview

Everything you write in C lives in memory. This includes data types, functions, and the very programs themselves. In order for your program to run it must be compiled, assembled, and linked with other source code in such a way that CPU instructions and memory locations are known at at runtime. C, as with all programming languages to various extents, provides mechanisms to abstract this process and a tool-chain to facilitate it.

Pass By Value & Pass By Reference

Library Analogy

fill

Suppose that you went to the library with the intent of checking out some books. You walk up to the help desk with a list of 10 entries. You say to the librarian, “Hey book lady, give me these books”. Based on how the librarian choses to interpret this, one of three things happens:

1: She gives you 10 books

Now you have 10 books, how are you supposed to carry these? They probably wont fit in your bag, and even if they could, would you really want to be holding that many books around?

2: She gives you index cards with references to the 10 books

Okay, well these are definitely a lot easier on your back, so thats a nice. But wait, you can’t exactly do your book report on a bunch of Dewey Decimal numbers scribbled on some paper.

3: You Get Something In Between…

What you probably expected was a mix between the two. Maybe to get the few that where available and references to those which were checked out for later? Maybe to get 2 or 3 at a time? Whatever the case may be, this is largely left up to chance or the library’s policy, both of which you cannot control and may not be aware or ahead of time.

Making the Connection

Value

Reference

Referencing/ Dereferencing

Task One: We Need to Talk About Your Grades..

Examine the following code.

#include <stdio.h>
#include <stdlib.h>

/**
 * grade1.c
 *
 * This program will allocate memory for an integer on the stack, and
 * demonstrate how C treats values passed by value as opposed to by reference.
 */

int finalGradeValue (int grade, int bonus);  //grade passed by value
int finalGradeReference (int* grade, int bonus);  //grade passed by reference


int main() {

    int grade = 45;

    finalGradeValue (grade, 5);  // passed value not modified
    printf ("Your final mark is %d. ", grade);
    puts (grade < 50 ? "You failed!" : "You passed!");

    finalGradeReference (&grade, 5);  // passed value modified, notice '&'
    printf ("Your final mark is %d. ", grade);
    puts (grade < 50 ? "You failed!" : "You passed!");

    return 0;
}


int finalGradeValue (int grade, int bonus) {
    grade = grade + bonus;
    return grade;
}

int finalGradeReference (int* grade, int bonus) {
    *grade = *grade + bonus;
    return *grade;
}

Questions

The Stack

When you declare and/or initialize a variable within a block, C reserves the amount of memory required to store that value in a region of memory called the stack.

The Heap

When you want to allocate memory yourself, this is done on the heap. This is a much larger of memory which is only limited by the system’s available storage. User programs can allocate memory in C using malloc, calloc, and realloc, and release it using free, all of which are provided by stdlib.h.

Task Two: Grades List Take Two

Rewrite our grade checking program, this time storing the grade integer in the heap.

The Rules

Hints

Solutions