









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










z^
z^
z^
z^
Stack Top Free Space
Allocated Space
z^
z^
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); }
z^
not
change p
z^
/* error, p points to something not allocated */
/* error, pointer is no longer valid */
z^
z^
z^
z^
z^
z^
for (i=0; i
k; i++) {
myInts[i] = anotherFunc(i, k); }^
Goes past end of previous example
z^
/* ERROR: ptr doe not point to something in heap */
z^
z^
/* ERROR: curr has already been freed */
z^
if (data[0] == 'Q') { .. }
/* OK: just using the data */
data[2] = 42
/* Error: can't modify data */
z^
int *ptr;ptr = calloc(sizeof(int), 42);return ptr; }
z^
z^
void *memmove(void *dest, const void *src, size_t len);• copies memory pointed to by src to memory pointed to by
dest
z^
z^
z^
z^
z^
needed