Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring 2005. Lecture 12 Outline 12.1Introduction.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Dynamic memory allocation
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.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
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.
Pointers Applications
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
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.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
CSCI206 - Computer Organization & Programming
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction - Dynamic Memory Allocation 12.2The malloc( ) function 12.3 The calloc( ) function 12.4 The realloc( ) function 12.5 Summary

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page Introduction So far you've learned how to declare and reserve a piece of memory space before it is used in your program. For instance, you have to specify the size of an array in your program (or the compiler has to figure out the size if you declare an unsized array) before you assign any data to it at runtime. In this lesson you'll learn to allocate memory space dynamically when your program is running. The four dynamic memory allocation functions covered in this lesson are: –malloc ( ) function  used when allocating memory –calloc( ) function  used when allocating memory –realloc( ) function  used to increase/decrease an available memory –free( ) function  used to free previously allocated memory

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page Allocating Memory at Runtime There are many cases when you do not know the exact sizes of arrays used in your programs, until much later when your programs are actually being executed. You can specify the sizes of arrays in advance, but the arrays can be too small or too big if the numbers of data items you want to put into the arrays change dramatically at runtime. Fortunately, C provides you with four dynamic memory allocation functions that you can employ to allocate or reallocate certain memory spaces while your program is running. Also, you can release allocated memory storage as soon as you don't need it.

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page The malloc() Function You can use the malloc() function to allocate a specified size of memory space. The syntax for the malloc() function is #include void *malloc(size_t size); Here size indicates the number of bytes of storage to allocate. The malloc() function returns a void pointer. Note that the header file, stdlib.h, has to be included before the malloc() function can be called. Because the malloc() function returns a void pointer, its type is automatically converted to the type of the pointer on the left side of an assignment operator. int *iptr = (int *) malloc(sizeof(int)); float *fptr = (float *) malloc(128); char *str = (char *) malloc(sizeof(char x[50])); If the malloc() function fails to allocate a piece of memory space, it returns a null pointer. Normally, this happens when there is not enough memory. Therefore, you should always check the returned pointer from malloc() before you use it.

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 5 Malloc() example #include main(){ char *cptr; printf(“Pelase Enter a sentence\n”); cptr =(char *) malloc(sizeof(char)*81); /* allocate 81 bytes a type cast to char pointer */ if ( cptr !=NULL ){ gets(cptr); printf(“You entered this: %s\n”, cptr); free(cptr); } else{ printf(“Sorry I am unable to allocate 81 bytes for you \n”); exit(0); } return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page The calloc() Function Besides the malloc() function, you can also use the calloc() function to allocate a memory storage dynamically. The differences between the two functions are that the latter takes two arguments and that the memory space allocated by calloc() is always initialized to 0. There is no such guarantee that the memory space allocated by malloc() is initialized to 0. The syntax for the calloc() function is #include void *calloc(size_t nitem, size_t size); Here nitem is the number of items you want to save in the allocated memory space. size gives the number of bytes that each item takes. The calloc() function returns a void pointer too. If the calloc() function fails to allocate a piece of memory space, it returns a null pointer.

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 7 Example of calloc() #include main(){ char *cptr; printf(“Pelase Enter a sentence\n”); cptr =(char *) calloc(81,sizeof(char)); /* allocate 81 bytes a type cast to char pointer */ if ( cptr !=NULL ){ gets(cptr); printf(“You entered this: %s\n”, cptr); free(cptr); } else{ printf(“Sorry I am unable to allocate 81 bytes for you \n”); exit(0); } return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 8 Another example for calloc() 1: /* Using the calloc() function */ 2: #include 3: #include 4: /* main() function */ 5: main() 6: { 7: float *ptr1, *ptr2; 8: int i, n; 9: int termination = 1; 10: 11: n = 5; 12: ptr1 = (float *) calloc(n, sizeof(float)); 13: ptr2 = (float *) malloc(n * sizeof(float)); 14: if (ptr1 == NULL) 15: printf("malloc() failed.\n"); 16: else if (ptr2 == NULL) 17: printf("calloc() failed.\n"); 18: else { 19: for (i=0; i<n; i++) 20: printf("ptr1[%d]=%5.2f, ptr2[%d]=%5.2f\n", 21: i, *(ptr1 + i), i, *(ptr2 + i)); 22: free(ptr1); 23: free(ptr2); 24: termination = 0; 25: } 26: return termination; 27: } ptr1[0] = 0.00, ptr2[0] = ptr1[1] = 0.00, ptr2[1] = ptr1[2] = 0.00, ptr2[2] = ptr1[3] = 0.00, ptr2[3] = 0.00 ptr1[4] = 0.00, ptr2[4] = output

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page The realloc() Function The realloc() function gives you a means to change the size of a piece of memory space allocated by the malloc() function, the calloc() function, or even itself. The syntax for the realloc() function is #include void *realloc(void *block, size_t size); Here block is the pointer to the start of a piece of memory space previously allocated. size specifies the total byte number you want to change to. The realloc() function returns a void pointer. The realloc() function returns a null pointer if it fails to reallocate a piece of memory space. The realloc() function is equivalent to the malloc() function if the first argument passed to realloc() is NULL. In other words, the following two statements are equivalent: ptr_flt = (float *) realloc(NULL, 10 * sizeof(float)); ptr_flt = (float *) malloc(10 * sizeof(float)); Also, you can use the realloc() function as the free() function. You do this by passing 0 to realloc() as its second argument. For instance, to release a block of memory pointed to by a pointer ptr, you can either call the free() function like this: free(ptr); or use the realloc() function in the following way: realloc(ptr, 0);

Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page Summary In C, there are four functions that can be used to allocate, reallocate, or release a block of memory space dynamically at runtime. The malloc() function allocates a block of memory whose size is specified by the argument passed to the function. The free() function is used to free up a block of memory space previously allocated by the malloc(), calloc(), or realloc() function The calloc() function can do the same job as the malloc() function. In addition, the calloc() function can initialize the allocated memory space to 0. The realloc() function is used to reallocate a block of memory that has been allocated by the malloc() or calloc() function. If a null pointer is passed to the realloc() function as its first argument, the function acts like the malloc() function. If the second argument of the realloc() function is set to 0, the realloc() function is equivalent to the free() function that releases a block of allocated memory. You have to include the header file stdlib.h before you can call the malloc(), calloc(), realloc(), or free() function. You should always check the values returned from the malloc(), calloc(), or realloc() function, before you use the allocated memory made by these functions.