10 Dynamic Memory

10 Dynamic Memory - !"#"$% CMSC 216 Introduction to...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
!"#"$$ $ CMSC 216 Introduction to Computer Systems Lecture 10 Pointers, cont. and Make Jan Plane & Pete Keleher {jplane, als}@cs.umd.edu Administrivia • Project 1 grades out now • Project 2– questions --- due tonight • Quiz next Monday – (I know this is the next class meeting after the exam) • Exam #1 Thursday, in lecture – covers everything through last week (Makefiles) CMSC 216 - Wood, Sussman, Herman, Plane 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
!"#"$$ D YNAMIC M EMORY A LLOCATION ( CONT .) Chapter 11, Reek CMSC 216 - Wood, Sussman, Herman, Plane 3 Pointer aliases • We can have two pointers pointing to the same address • Remember freeing the space doesn’t change the value of the pointer or the value of the space pointed at int *p, *q; p = malloc(sizeof(int)); *p = 99; q = p; /*values of p and q and *p and *q*/ free(p); p = NULL; *q = 42; q is called a dangling pointer CMSC 216 - Wood, Sussman, Herman, Plane 4
Background image of page 2
!"#"$$ ! Common errors • Dereferencing pointers to freed space (directly or indirectly) • Forgetting to check the return value from malloc() for NULL • Not initializing the memory malloc() returns • Not allocating enough space: char string[] = "Inconceivable!"; char *p = malloc(strlen(string)); strcpy(p, string); /* Oops. .. */ CMSC 216 - Wood, Sussman, Herman, Plane 5 Common errors, cont. • Attempting to free non-heap memory: int i, *p; p = &i; free(p); /* ??? Undefined operation */ • Freeing something that's not the beginning of a dynamically allocated block: int *p = calloc(10, sizeof(int)); free(p + 3); CMSC 216 - Wood, Sussman, Herman, Plane 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
!"#"$$ ' Common errors, cont.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 11

10 Dynamic Memory - !"#"$% CMSC 216 Introduction to...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online