Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Memory API Chien-Chung Shen CIS, UD

Similar presentations


Presentation on theme: "Chapter 14 Memory API Chien-Chung Shen CIS, UD"— Presentation transcript:

1 Chapter 14 Memory API Chien-Chung Shen CIS, UD cshen@cis.udel.edu

2 Types of Memory Stack (or automatic) memory – managed implicitly by compiler for programmer void func() { int x; // declares an integer on the stack... } Heap memory – explicitly managed by programmer void func() { int *x = (int *) malloc(sizeof(int));... }

3 Size of Memory Allocation int *x = malloc(10 * sizeof(int)); printf("%d\n", sizeof(x)); int y[10]; printf("%d\n", sizeof(y)); there is enough static information for compiler to know that 40 bytes have been allocated sizeof() is a compile-time operator

4 Memory Bugs Forget to allo cate memory char *src = "hello”; char *dst; // oops! unallocated strcpy(dst, src); // segfault and die Not allocate enough memory char *src = "hello"; char *dst = (char *) malloc(strlen(src)); // too small! strcpy(dst, src); // work properly, but buffer overflow Forget to initialize allocated memory Forget to free memory – memory leak


Download ppt "Chapter 14 Memory API Chien-Chung Shen CIS, UD"

Similar presentations


Ads by Google