EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
C Programming Lecture 14 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
1 Arrays and Strings. 2 An array is a collection of variables of the same type that are referred to through a common name. It is a Data Structure which.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers Applications
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Outline Midterm results Static variables Memory model
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Advanced Programming with C
Day 03 Introduction to C.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Dynamic Memory Allocation
CSC 253 Lecture 8.
Programming and Data Structures
CSC 253 Lecture 8.
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Pointers and Arrays Beyond Chapter 16
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Presentation transcript:

EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b Outline Dynamic Memory Allocation malloc( ), free( ) and calloc( ) functions Dynamically Allocated Arrays DYNAMIC MEMORY ALLOCATION

DYNAMIC MEMORY ALLOCATION Dynamic allocation is the means by which a program can obtain memory while it is running. Pointers provide necessary support for C’s powerful dynamic memory allocation. In general global variables are allocated storage at compile time. Local variables use the program stack. However neither global nor local variables can be added during program execution. Memory allocated by C’s dynamic allocation functions come from the “heap”: the region of free memory that lies between your program’s permanent storage area and the stack.

DYNAMIC MEMORY ALLOCATION… void * malloc(size_t number_of_bytes); In general the heap contains a fairly large amount of free memory. malloc( ) function malloc( ) function dynamically allocates memory during the program. It is a library function included in the <stdlib.h> header file. The function prototype is given below: void * malloc(size_t number_of_bytes); where, size_t is defined in header file as unsigned integer and number_of_bytes is the amount of memory we wish to allocate after a successful call, malloc( ) returns a pointer to the first byte of the region of memory.

void * malloc(size_t number_of_bytes); where, size_t is defined in header file as unsigned integer and number_of_bytes is the amount of memory we wish to allocate After a successful call, malloc( ) returns a pointer to the first byte of the region of memory. If memory is not enough the function returns a NULL. Ex: Code fragments below allocate 1000 bytes of contigious memory. char *p; p =(char *)malloc(1000); //(char *) forces void pointer to //become a character pointer

Ex: Code below allocates space for 50 integers. int *p; /* (int *) forces void pointer to become a integer pointer*/ p =(int *) malloc(50*sizeof(int)); Since heap is not infinite you must check the value returned by malloc( ) to make sure it is not null. You can use the following code fragment for this purpose. if( !(p=(int *)malloc(100))) { printf(“Out of memory \n”); exit(1); }

free( ) function free( ) function is the opposite of malloc( ). It returns previously allocated memory to the system. It has the prototype below: void free(void *p); It is critical that you never call free( ) with an invalid argument. This will destroy the free list. calloc( ) function calloc( ) function allocates an amount of memory equal to num*size. That is calloc( ) allocates enough memory for an array of num objects, each object being size bytes long. Memory allocated by calloc( ) is released by free( ) function.

Memory allocated by calloc( ) is released by free( ) function. The name calloc comes from “ contiguous allocation”. calloc( ) has the following function prototype: void *calloc(size_t n, size_t number_of_bytes); where, size_t is defined in header file as unsigned integer. n is the number of object to be used in the dynamic array and number_of_bytes is the size of each object in the array. Ex: Code fragments below dynamically allocates 100 elements to an integer array. int *p; p = (int *) calloc(100,sizeof(int));//(int *) forces the void /* pointer to become an int pointer */

Dynamically Allocated Arrays Sometimes you will want to allocate memory using malloc( ) or calloc( ) but operate on that memory as if it were an array, using an array index. Since any pointer may be indexed as if it were a single-dimensional array this is not a problem. Ex: In this program pointer s is used in the call to gets() and then indexed as an array to print the string backwards.

#include<stdio.h> #include<stdlib.h> #include<string.h> void main(void) { char *s; register int t; s= (char *)malloc(80); // (char *) forces the void * to if(!s) // an char *. printf(“Memory request failed \n”); exit(1); } gets(s); /* standard lib fn to pass array into functions*/ for(t=strlen(s)-1; t>=0; t--) putchar(s[t]); free(s);

Ex: The following program asks user to specify the size of the dynamic integer array to be used and then displays the array elements entered by the user in the reverse order. #include<stdio.h> #include<stdlib.h> void main(void) { int *aptr, i, n; printf(“Specify the size of you integer array\n”); scanf(“%d”,&n); aptr= (int *)calloc(n, sizeof(int)); // (int *) forces // the void * to int *. printf(“The array is ready Enter %d numbers one by one\n”, n); for(i=0;i<n;i++) scanf(“%d”,&aptr[i]); for(i=n-1;i>=0;i--) printf(“%d,”,aptr[i]); free(aptr); }