Docsity

Stack and Heap Memory on Introduction to Low-Level Programming | CMSC 212, Study notes of Computer Science

Partial preview of the text

Download Stack and Heap Memory on Introduction to Low-Level Programming | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

Announcements

l^ Program

  • Due Date Extension: Tuesday, March 15, 2005

l^ Reading– Chapter 11 (Today)– Chapter 14 (Thursday)

Stack and Heap Memory

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

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

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

Stack Top Free Space Allocated Space

Heap Memory Management

l^ Allocating memory:–^

void *malloc(size_t requestedSize);^ • allocate requestedSize bytes of memory• no guaranteed initialization of that memory space– void *calloc(size_t requestCount, size_t objectSize);^ • allocate requestCount objects of size objectSize bytes each• set the requested memory to zero

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

Example of malloc

#include

<malloc.h> #include

<stdio.h> int^ main(void){T^ p;p^ =^ (T)

malloc(sizeof(T)); if^ (!p)

{ exit(-1);

often

nothing

can^

do^ on

failure

} /*^ use

of^ p

Memory De-allocation

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

not^ change p• good idea p=NULL; right after de-allocation.

l^ 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);• p = NULL;• if (*p) { …}

/*runtime error, pointer is no longer valid */

What if you need more memory?

l^ Could do multiple steps– malloc new space– copy data from old space to new– free old space l^ 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 l Derefencing NULL pointers– int *foo = NULL;– *foo = 3;

/* ERROR – segmentation fault right away*/ l^ Forgetting to check return value of malloc/calloc l^ Using malloc and not initializing values–^

memset l^ Going past end of allocated space–^

very easy when using array syntax and loops *int i, myInts;myInts = (int

*) calloc(k, sizeof(int)); if (!myInts) {printf("no

memory\n"); exit(-1);} for (i=0; i <=^

k; i++)

{

myInts[i] =

anotherFunc(i,

k);

}

Goes past end of array allocated

More Common Memory Errors

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

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

l^ Calling Free on other than the start of allocationint *ptr;ptr = (int *) calloc(10, sizeof(int));free (ptr + 3); l^ Using memory that has been freedT *curr;free (curr);curr = curr->next;

/* ERROR: curr has already been freed */

Pointers and Functions

l^ Functions can return pointer to dynamically allocatedmemory– ------------------------

good:

int *getInt() {int *ptr;ptr = (int *) calloc(42, sizeof(int));return ptr;} –^ -----------------------

bad:

int *getInt() {int a=7;int *ptr = &a;return ptr;}

Other Helpful Memory Routines

l^ char *strdup(char *string);– allocate enough memory to hold string (including it's null character)– copy string into the allocated memory– #include <string.h> l^ void *alloca(size_t size);– allocate memory on the

stack

  • memory is freed when the function returns\– #include <alloca.h>

Midterm Results

Total

l^ Re-grade Requests– must be in writing via the submit server– deadline is Thursday 3/10/05 1:45 PM– must hand-in your exam booklet to me by the deadline– you must not alter your exam booklet in any way if you are

  • Q
  • Q
  • Q
  • Q
  • Q
  • Q
      1. Avg
        1. Min
          1. Max
            1. Std. Dev

Common Mt #1 Mistakes

l^ #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

l^ #2– last integer (+2 is the value 4) l^ #3– Explaining two passes– Describing memory field & that a new op-code was needed