Docsity

Stack and Heap Memory and Dynamic Memory | CMSC 212, Study Guides, Projects, Research of Computer Science

Partial preview of the text

Download Stack and Heap Memory and Dynamic Memory | CMSC 212 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Announcements

z^

Project #2 Deadline– Extended to Tuesday 3/15/05 8:00 PM

z^

Midterm #2 returned at end of class today

z^

Reading– Chapter 11 (Today)– Chapter 14 (Tuesday)

Stack and Heap Memory

z^

Stack Memory– Allocated and de-allocated in a specific order

  • last allocated is the first to be freed
    • Useful for representing program local variables z^

Heap Memory– Can allocate and de-allocate in any order

Stack Top Free Space

Allocated Space

Heap Memory Management

z^

Allocating memory:– void *malloc(size_t requestedSize);

  • allocate requestedSize bytes of memory
    • void *calloc(size_t requestCount, size_t objectSize);
      • allocate requestCount objects of size objectSize bytes each• set the requested memory to zero

z^

Type Information– malloc/calloc return void *– need to convert to desired type using cast operator

Example

T p;p = (T) malloc(sizeof(T));if (!p) {

exit(-1); /* often nothing can do on failure / } / use of p */void do something(int k) {

int i, *myInts;myInts = (int *) calloc(sizeof(int),k);if (!myInts) exit(-1);for (i=0; i < k; i++) {

myInts[i] = anotherFunc(i, k); }

Memory Deallocation

z^

free(void *p);– returns space pointed to by p– does

not

change p

  • good idea p=NULL; right after deallocation.

z^

Using Free– always free memory when done with it– never free things that are not allocated by malloc/calloc:

  • int a = 3;• int *p = &a;• free(p);

/* error, p points to something not allocated */

  • Never dereference a pointer after free is called:
    • free(p);• if (*p) { …}

/* error, pointer is no longer valid */

What if you need more memory?

z^

Could– malloc new space– copy data from old space to new– free old space

z^

C provides a short cut– void *realloc(void *ptr, size_t new_size);– allocates new space (larger than old)– copies old data and frees it– returns pointer to new space

Common Dynamic Memory Errors

z^

Derefencing NULL pointers– int *foo = NULL;– foo = 3; / ERROR */

z^

Forgetting to check return value of malloc/calloc

z^

Using malloc and not initializing values

z^

Going past end of allocated space– very easy when using array syntax and loops

for (i=0; i

k; i++) {

myInts[i] = anotherFunc(i, k); }^

Goes past end of previous example

More Common Memory Errors

z^

Calling Free on something not from heapint *ptr; j;j = 3;ptr = &j;free(ptr);

/* ERROR: ptr doe not point to something in heap */

z^

Calling Free on other than the start of allocationint *ptr;ptr = calloc(sizeof(int), 10);free (ptr + 3);

z^

Using memory that has been freedT *curr;free (curr);curr = curr->next;

/* ERROR: curr has already been freed */

Pointers and Functions

z^

Parameters– can use const to prevent functions from changing valuesint frob(const char *data, int size) {

if (data[0] == 'Q') { .. }

/* OK: just using the data */

data[2] = 42

/* Error: can't modify data */

z^

Functions can return pointer typesint *getStuff() {

int *ptr;ptr = calloc(sizeof(int), 42);return ptr; }

Blocks of Memory

z^

C supports operations on blocks of memory– might be an array, a struct, an array of structs, etc.

z^

Copying– void *memcpy(void *dest, const void *src, size_t len);

  • copies memory pointed to by src to memory pointed to by dest - copies len bytes of memory• returns the value of src -^

void *memmove(void *dest, const void *src, size_t len);• copies memory pointed to by src to memory pointed to by

dest

  • copies len bytes of memory• returns the value of src• handles the case if dest and src overlap

Common Mt #1 Mistakes

z^

#1– Program Counter

  • advances one location per instruction, branch changes
    • Function Prototype
      • defines name, paramter types, and return types• allows compiler to type check functions in separate files

z^

#2– last integer (+2 is the value 4)

z^

#3– Explaining two passes– Describing memory field & that a new op-code was needed

Common Mt #1 Mistakes (cont.)

z^

#4– Expressions can be valid even if they don't refer to scalars

z^

#5– finding comma, and not overwriting parts of string that are

needed

  • printing lastname firstname not firstname lastname z^

#6– Make example - go re-read how to create a make file– cp copies one file, doesn't combine two– Review basic UNIX commands