CSE 220 – C Programming malloc, calloc, realloc.

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.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
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.
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.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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:
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
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.
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.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
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.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSC Programming for Science Lecture 34: Dynamic Pointers.
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.
Dynamic Allocation in C
Dynamic Storage Allocation
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation Strings
ENEE150 Discussion 07 Section 0101 Adam Wang.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers Revisited What is variable address, name, value?
Dynamic Memory Allocation
Programming Languages and Paradigms
Dynamic Memory Allocation
Programming and Data Structures
Pointers, Dynamic Data, and Reference Types
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
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.
Dynamic Allocation in C
Pointers and Arrays Beyond Chapter 16
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Presentation transcript:

CSE 220 – C Programming malloc, calloc, realloc

Outline Arrays and Pointers Memory allocation Memory deallocation

Is this code legal? Legal Illegal (Array Initialization) int array[10] = {1, 2, 3}; for (int * p = &a[0]; p < &a[10]; p++) { printf("%d\n", *p); } Legal Illegal (Array Initialization) Illegal (Pointer Arithmetic) I don't know

Using pointers to arrays Can traverse array by incrementing a pointer to it int a[N], *p, sumAll = 0, sumPartial = 0; for (p=&a[0]; p<&a[N]; p++) //add every element SumAll += *p; for (p=&a[0]; p<&a[N]; p=p+2) //add every other element SumPartial += *p;

Using array name as pointer The name of an array is a pointer to the first element of the array int a[N]; *a = 8; //Puts 8 in a[0] *(a+1) = 9; //Puts 9 in a[1] for (p=a; p<a+N; p++) sumAll += *p;

Is this code legal? Legal Illegal (While Condition) int array[10] = {1, 2, 3}; while (*array != 0) { array++; } Legal Illegal (While Condition) Illegal (Increment Operation) I don't know

Using array name as pointer It is not possible to assign a new value to the array name int a[N]; while (*a != 0) a++; //WRONG! Instead, use a temp pointer that moves through the array int *p = a; while (*p != 0) p++;

Dynamic Storage Allocation Fixed size data structures have the same number of elements from compilation time for the whole structure lifetime: int a[100]; //Will always have 100 elements Dynamic storage allocation: the ability to allocate storage during program execution Design data that grows and shrinks as needed Normally used for strings and arrays

Memory allocation functions malloc: allocates a block of memory without initializing calloc: allocates a block of memory and clears it realloc: resizes a previously allocated block of memory These functions are declared in the <stdlib.h> Take as input the number of bytes to allocate

Memory allocation functions pc allocated mem. char *pc = malloc(N+1); int *pi = malloc (400); Does malloc return int * or char *? malloc does not know the type of data that will be stored in the block of memory so it returns a generic pointer: void * char *pc = (char *) malloc(N+1); int *pi = (int *) malloc (400); If allocation fails, malloc returns null pointer: NULL

Dynamically Allocated Strings Write a function (named "concat") that takes two strings s1 and s2 and returns a third string obtained by concatenating s1 and s2 Allocate enough memory to hold s1 and s2: size of s1 + size of s2 + 1 (to fit \0) Write s1 in s3 Write s2 in s3 right after s1 Add the termination character \0

Dynamically Allocated Strings char * concat(char *s1, char *s2) { char * result; //Points to nothing in particular result = malloc(strlen(s1) + strlen(s2) + 1); /* If malloc succeeds, result points to new allocated memory */ if (result == NULL) { //Check if allocation succeeded printf(“Error: could not allocate memory\n”); } strcpy(result, s1); strcat(result, s2); return result;

Deallocating memory Memory allocated with malloc lives for the lifetime of the program char *s1 = (char *) malloc(1000); strcpy(s1, “attempt 1: some long text”); s1 = (char *) malloc(1000); strcpy(s1, “attempt 2: just another string”); strcpy(s1, “attempt 3: one last time………”);

1000 bytes a t e m p 1 : s o . 1000 bytes a t e m p 2 : s1 1000 bytes a t e m p 3

Deallocating memory Memory allocated with malloc lives for the lifetime of the program If no longer needed, make sure to free the memory used otherwise program might run out of memory. char *s1 = “Electric “, *s2 = “current “, char *s3 = “is measured ”, char *s4 = “in amperes (amps)”; char *r1 = concat(s1, s2); char *r2 = concat(r1, s3); char *r3 = concat(r2, s4);

A look at memory s1 s2 s3 s4 r1 r2 r3 Make sure to free memory that is not needed: free(r1); free(r2); r2 r3

Programs that use more memory often run slower. Why is it important to release memory that is no longer needed? Computers have a finite amount of memory, so a program that uses too much may crash. Programs that use more memory often run slower. If you love it, set it free. I don't know

Arrays of strings char tmp[20]; char * winningCars[5]; //Declare array of pointers //Each pointer still points to nothing in particular for (int i=0; i<5; i++) { scanf(“%s”, tmp); //Allocate memory for each pointer winningCars[i] = malloc(strlen(tmp) + 1); strcpy(winningCars[i], tmp); }

Arrays of strings char tmp[20]; char * winningCars[5]; … … 19 char tmp[20]; char * winningCars[5]; winningCars 4 scanf(“%s”, tmp); winningCars[0] = malloc(strlen(tmp) + 1); strcpy(winningCars[0], tmp);

Arrays of strings char tmp[20]; char * winningCars[5]; b c \0 … 19 winningCars 4 a b c \0 scanf(“%s”, tmp); winningCars[0] = malloc(strlen(tmp) + 1); strcpy(winningCars[0], tmp);

Arrays of strings char tmp[20]; char * winningCars[5]; e l o \0 … 19 winningCars 4 a b c \0 H e l o \0 scanf(“%s”, tmp); winningCars[1] = malloc(strlen(tmp) + 1); strcpy(winningCars[1], tmp);

Dynamically Allocated Arrays int *a = malloc(400); //Allocates 400 bytes How many elements in a? If int consists of 4 bytes, then a has 100 elements int *a = malloc (N * sizeof(int)); //N elements Once memory is allocated, treat a as any other array: a[1] = 5; *(a + 2) = 7;

Dynamically Allocated Arrays int *a = malloc (N * sizeof(int)); //N elements int *b = calloc (N, sizeof(int)); //N elements A has enough memory for n integers B has enough memory for n integers. All elements of B are initialized to zeros.

It has a more silly sounding name. Why not always use calloc (instead of malloc)? It has a more silly sounding name. It is slower (memory needs to be cleared first). It can hold fewer variables in the same space. I don't know

Dynamically Resize Arrays If previously allocated memory is too small or too big, can resize with realloc When calling realloc, the pointer given must be to memory allocated using malloc, calloc or realloc char *str = malloc(N + 1); …. str = realloc (str, 2*N + 1);

Dynamically Resize Arrays realloc tries to expand memory in place: p Sometimes, this is not possible: p Data is moved then expanded: p

Dynamically Resize Arrays There could be other pointers pointing to the old location. Make sure to update all the pointers since the block could have moved elsewhere. p q //Update q to point to the same location as p q = p;

Deallocating memory p p p q q p = malloc(…); q = malloc(…); q = p; free(ptr): deallocates the block of memory pointed to by ptr May lose track of memory blocks: p = malloc(…); q = malloc(…); q = p; p p p q q

Deallocating memory Memory without a pointer to it is called garbage A program that leaves garbage behind has a memory leak C does not have automatic garbage collection. It is the programmer’s job Pointer freed becomes a dangling pointer: does not have memory associated with it. It cannot be reused without being allocated some memory. If several pointers point to one location and one pointer is freed, all the pointers become dangling.