Download presentation
Presentation is loading. Please wait.
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.