1 / 4

Chapter 14 Memory API

Chapter 14 Memory API. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. Types of M emory. 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

varian
Télécharger la présentation

Chapter 14 Memory API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 14Memory 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() { intx; // 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)); inty[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 allocate 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

More Related