Dynamic Memory Management and Pointers in ANSI C Programming

com101b lecture 14 pointers part3 n.w
1 / 7
Embed
Share

Learn about dynamic memory management and pointers in ANSI C programming with topics including the sizeof operator, malloc, realloc, and free functions. Understand how to allocate and deallocate memory dynamically for efficient memory usage in C programming.

  • C programming
  • Pointers
  • Memory Management
  • ANSI C
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COM101B LECTURE 14: POINTERS PART3 ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  2. DYNAMIC MEMORY MANAGEMENT sizeof opetrator: is a unary operator returns the size of a type or data object has the following form: sizeof (typename) sizeof expression REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  3. SIZEOF OPERATOR when applied to an expression, analyzes the expression at compile time and determine its type returns the same result as if it had been applied to the type of the application Example: short s, *sp; sizeof(s) is the same as sizeof(short) sizeof(sp) is the same as sizeof(short *) sizeof(*sp) is the same as sizeof(short) REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  4. DYNAMIC MEMORY MANAGEMENT There are four dynamic memory management functions: malloc, realloc, calloc, free malloc, calloc : used to obtain storage for an object realloc: used to change the size of the storage allocated to an object free: used for releasing the storage REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  5. MALLOC void *malloc (size_t size); it allocates a storage and returns the address to the allocated memory return NULL if allocation fails Ex: float* fp = (float*)malloc(20*sizeof(float)); REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  6. REALLOC void* realloc(void* p, size_t size); changes the size of an object that is pointed to by p size parameter specifies the new size if the original data size is smaller than size, the memory is enlarged and the data in the original address is moved to the newly allocated space. if the original data size is larger than size, some of the data in the memory is lost REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  7. FREE FUNCTION void free(void* p); free function deallocates the storage pointed to by p that storage is previously allocated by malloc, calloc or realloc Example: int* p = (int*) malloc(10*sizeof(int)); free(p); REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

More Related Content