Programmazione I a.a. 2017/2018.

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.
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.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
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.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
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:
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.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Pointers Applications
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
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.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
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.
Dynamic Allocation in C
Object Lifetime and Pointers
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Advanced Programming with C
Lesson One – Creating a thread
Pointers, Polymorphism, and Memory Allocation
Dynamic Memory Allocation
Programmazione I a.a. 2017/2018.
Checking Memory Management
Programmazione I a.a. 2017/2018.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
Dynamic Memory Allocation
Programming and Data Structures
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
Pointers.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
EECE.2160 ECE Application Programming
Pointers Pointers point to memory locations
EECE.2160 ECE Application Programming
Chapter 10-1: Dynamic Memory Allocation
DYNAMIC MEMORY MANAGEMENT
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Presentation transcript:

Programmazione I a.a. 2017/2018

INTRO TO DYNAMIC MEMORY MANAGEMENT

Why dynamic memory When you’re writing a program, you often don’t know how much data it will have to process. Or you can anticipate that the amount of data to process will vary widely. In these cases, efficient resource use demands that you allocate memory only as you actually need it at runtime, and release it again as soon as possible. This is the principle of dynamic memory management, which also has the advantage that a program doesn’t need to be rewritten in order to process larger amounts of data on a system with more available memory.

Why dynamic memory Dynamic memory is a part of memory with different properties It’s the programmer who decides when to create an object and when to destroy it (i.e., remove it from memory)

functions The standard library provides the following four functions for dynamic memory management: Allocate a new block of memory. malloc( ), calloc( ) Resize an allocated memory block. realloc( ) Release allocated memory. free( ) All of these functions are declared in the header file stdlib.h. The size of an object in memory is specified as a number of bytes. Various header files, including stdlib.h, define the type size_t specifically to hold information of this kind. The sizeof operator, for example, yields a number of bytes with the type size_t.

ALLOCATING MEMORY DYNAMICALLY

Malloc() The malloc() function reserves a contiguous memory block whose size in bytes is at least size. When a program obtains a memory block through malloc( ), its contents are undetermined. void *malloc( size_t size );

Calloc() The calloc( ) function reserves a block of memory whose size in bytes is at least count × size. In other words, the block is large enough to hold an array of count elements, each of which takes up size bytes. Furthermore, calloc( ) initializes every byte of the memory with the value 0. void *calloc( size_t count, size_t size );

More on Both functions return a pointer to void, also called a typeless pointer. The pointer’s value is the address of the first byte in the memory block allocated, or a null pointer if the memory requested is not available. When a program assigns the void pointer to a pointer variable of a different type, the compiler implicitly performs the appropriate type conversion. When you access locations in the allocated memory block, the type of the pointer you use determines how the contents of the location are interpreted.

example float *myFunc( size_t n ) { // Reserve storage for an object of type double. double *dPtr = malloc( sizeof(double) ); if ( dPtr == NULL ) // Insufficient memory. /* ... Handle the error ... */ return NULL; } else { // Got the memory: use it. *dPtr = 0.07; /* ... */

example 1000 q p 110000000 000000000 110000000 000000000 malloc( sizeof(int) ); void* int*p = (int*) malloc(sizeof(int)); *p = 3; short int*q = (short int*) malloc(sizeof(int)); *q= 3;

CHARACTERISTICS OF ALLOCATED MEMORY

Characteristics 1 A successful memory allocation call yields a pointer to the beginning of a memory block. “The beginning” means that the pointer’s value is equal to the lowest byte address in the block. An allocated memory block stays reserved for your program until you explicitly release it by calling free( ) or realloc( ). In other words, the storage duration of the block extends from its allocation to its release, or to end of the program.

Characteristics 2 The arrangement of memory blocks allocated by successive calls to malloc(), calloc( ), and/or realloc( ) is unspecified. Heap int*p = malloc (10); int*q = calloc(3, 5); int* r= malloc(20);

RESIZING AND RELEASING MEMORY

Free() and realloc() When you no longer need a dynamically allocated memory block, you should give it back to the operating system. You can do this by calling the function free( ). Alternatively, you can increase or decrease the size of an allocated memory block by calling the function realloc().

Free() The free( ) function releases the dynamically allocated memory block that begins at the address in ptr. A null pointer value for the ptr argument is permitted, and such a call has no effect. void free( void *ptr );

Realloc() The realloc( ) function releases the memory block addressed by ptr and allocates a new block of size bytes, returning its address. The new block may start at the same address as the old one. void *realloc( void *ptr, size_t size ); realloc( ) also preserves the contents of the original memory block—up to the size of whichever block is smaller. If the new memory block is larger than the original, then the values of the additional bytes are unspecified.

Realloc() It is permissible to pass a null pointer to realloc( ) as the argument ptr. If you do, then realloc() behaves similarly to malloc(), and reserves a new memory block of the specified size. The realloc() function returns a null pointer if it is unable to allocate a memory block of the size requested. In this case, it does not release the original memory block or alter its contents.

Pointers in free() & realloc() You may pass these functions only a null pointer or a pointer value obtained from a prior call to malloc( ), calloc( ), or realloc( ). If the pointer argument passed to free( ) or realloc( ) has any other value, or if you try to free a memory block that has already been freed, the program’s behavior is undefined.

realloc 1000 11000000 11000000 or 900 if the block is reallocated at 00000000 11000000 00000000 or 900 if the block is reallocated at address 900 int* p= (int*) malloc(sizeof(int)); *p= 3; int* p= (int*) realloc(p, sizeof(short int));

realloc 1100000 00000000 00000000 int* p= (int*) malloc(sizeof(int)); int* p= (int*) realloc(p, sizeof(long int)); 00000000 ?

free 1000 11000000 00000000 int* p= (int*) malloc(sizeof(int)); *p= 3; free(p);

More info The memory management functions keep internal records of the size of each allocated memory block. This is why the functions free( ) and realloc( ) require only the starting address of the block to be released, and not its size. There is no way to test whether a call to the free( ) function is successful, because it has no return value. Starting address Amount of bytes int* p= (int*) malloc(4); int* q= (int*) calloc(3, 4); 1000 free(p); 1000 4 1020 1020 12

EXAMPLE int *p_array; double *d_array; // Allocated in the heap p_array = (int *) malloc(sizeof(int)*5); // allocate 5 ints d_array = (double *) malloc(sizeof(double)*6); // allocate 6 doubles for(int i=0; i < 5; i++) { p_array[i] = 0; } for(int i=0; i < 6; i++) { d_array[i]= 0; free (p_array); p_array = (int *) malloc(sizeof(int)*100); // allocate 100 ints

ERRORS

errors Since in C is up to the programmer to manage dynamic memory (or heap) with allocation/deallocation and pointers to ojects in the heap, this is more error-prone. In other languages (e.g., Java) run-time support of the language (i.e., Java Virtual Machine) implements garbage collection. The garbage collector, or just collector, automatically attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Unreferenced objects are are automatically removed from the heap Employee e=new Employee();   e=null;

Garbage collection Typically, garbage collection has certain disadvantages, including consuming additional resources, performance impacts, possible stalls in program execution, and incompatibility with manual resource management.

deallocation Always remember to deallocate objects with free() when you do not need them anymore Otherwise, the object may become unreachable: not referenced by any pointer In this is repeated, the heap can be fully consumed: it is limited, as memory in general. When it is fully consumed, a program may crash or however, you cannot allocate what you want It is a problem if your program runs for a long period (or forever: servers) These are defined as memory leaks

They just become unreachable: it’s a memory leak example #include<stdlib.h> int main( ) { int* i_array= (int*) calloc(4, sizeof(int)); i_array= NULL; *(i_array+2)= 3; } The 16 bytes allocated starting from i_array are not deallocated from memory They just become unreachable: it’s a memory leak

Frequent errors Frequent errors are memory usage after a call to free (dangling pointer) or deferecing a pointer assigned to NULL, calling free twice ("double free"), etc., usually causes a segmentation fault and results in a crash of the program. Your program is only allowed to touch memory that belongs to it -- the memory previously mentioned. Any access outside that area will cause a segmentation fault.

Deferencing a null pointer int main(void) { int*p =NULL; int c = 3 + *p; } MacBook-Francesco:ProgrammI francescosantini$ ./test Segmentation fault: 11

Void in ponters A pointer of type void * represents the address of an object, but not its type. You can use such quasi-typeless pointers mainly to declare functions that can operate on various types of pointer arguments, or that return a “multipurpose” pointer. The standard memory management functions are a simple example: void *malloc( size_t size ); void *realloc( void *ptr, size_t size ); void free( void *ptr );

example #include <stdio.h> #include <stdlib.h> int main() { int i= 0; while (1) { int* p= (int*) malloc(10000000000000); i++; printf("Number of times: %d\n", i); if (p == NULL) break; }

output MacBook-Francesco:ProgrammI francescosantini$ ./a.out Number of times: 1 Number of times: 2 Number of times: 3 Number of times: 4 Number of times: 5 Number of times: 6 Number of times: 7 Number of times: 8 Number of times: 9 Number of times: 10 Number of times: 11 Number of times: 12 Number of times: 13 Number of times: 14 a.out(19788,0x7fff9fd113c0) malloc: *** mach_vm_map(size=10000000000000) failed (error code=3) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug Number of times: 15