61_pdfsam_cs2022

61_pdfsam_cs2022 - strcat(s3, s2); return s3; } int main()...

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

View Full Document Right Arrow Icon
free malloc()’ed memory is not freed automatically Even after all pointers to it have been destroyed! Must call free() to deallocate memory when done using it void f(){ char *p; p = (char *) malloc(1000); } int main(){ while(1) f(); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
free malloc()’ed memory is not freed automatically Even after all pointers to it have been destroyed! Must call free() to deallocate memory when done using it void f(){ char *p; p = (char *) malloc(1000); /* some code */ free(p); } int main(){ while(1) f(); }
Background image of page 2
Memory leaks You need a call to free() for every call to malloc() Forgetting to call free() causes a memory leak The memory is not being used but still allocated Memory leaks are very common, but often difficult to find Other bad memory errors Calling free() more than once Calling free() on a pointer not returned by malloc()
Background image of page 3

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

View Full DocumentRight Arrow Icon
strcat() wrapper Idea: what if we create a wrapper function for strcat? This version didn’t work: char *my_strcat(char *s1, char *s2) { char s3[1024]; strcpy(s3, s1);
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: strcat(s3, s2); return s3; } int main() { char s1 = “hello”, s2 = “world”; char *result = my_strcat(s1, s2); printf(“%s\n”, result); return 0; } strcat() wrapper Solution: allocate the result on the heap But the caller is responsible for free’ing it char *my_strcat(char *s1, char *s2) { int length = strlen(s1) + strlen(s2) + 1; char *s3 = (char *) malloc(length); strcpy(s3, s1); strcat(s3, s2); return s3; } int main() { char s1 = “hello”, s2 = “world”; char *result = my_strcat(s1, s2); printf(“%s\n”, result); free(result); return 0; } An aside: Memory management in Java Java has similar memory management concepts Primitive types & references stored on the stack You can allocate space on the heap using new But you don’t have to worry about deallocating memory. Why? String getString() { return new String(); } void main() { String s = getString(); /* do stuff with the string */ }...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell.

Page1 / 6

61_pdfsam_cs2022 - strcat(s3, s2); return s3; } int main()...

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

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