Pointers and dynamic memory

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
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.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
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.
Dynamic Memory Management CAS CS210 Ying Ye Boston University.
CIS 415 – Operating System (Lab) CIS 415 Lab 5 Valgrind (memcheck) Dave Tian.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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 applications To understand the relationship between arrays and pointers. To understand the design and concepts behind pointer arithmatic. To write.
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{
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 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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.
ECE Application Programming
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
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.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
Dynamic Memory Allocation Strings
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers and dynamic memory
Linked list.
Memory Management in C Notes courtesy of Dr. D. Rothe, modified by Dr. R. Hasker.
CSCI206 - Computer Organization & Programming
Arrays & Dynamic Memory Allocation
Some examples.
Pointers.
14th September IIT Kanpur
CSC 270 – Survey of Programming Languages
Dynamic memory allocation and Intraprogram Communication
Pointers.
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
prepared by Senem Kumova Metin modified by İlker Korkmaz
Pointers and dynamic memory
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

Pointers and dynamic memory

malloc, calloc, realloc, free Allocate block of memory malloc - void* malloc(size_t size) //unsigned int calloc realloc free - deallocate block of memory Memory (heap)   200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

malloc, calloc, realloc, free Memory (heap)   200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Allocate block of memory malloc - void* malloc(size_t size) //unsigned int void *p = malloc(4) print p //200

malloc, calloc, realloc, free Memory (heap)   200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Allocate block of memory malloc - void* malloc(size_t size) //unsigned int int *p = (int*) malloc(3*sizeof(int)) print p //200 *p = 2 *(p+1) = 4 *(p+2) = 5

malloc, calloc, realloc, free Memory (heap)   200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Allocate block of memory malloc - void* malloc(size_t size) //unsigned int int *p = (int*) malloc(3*sizeof(int)) print p //200 p[0] = 2 p[1] = 4 p[2] = 5

malloc, calloc, realloc, free Allocate block of memory malloc - void* malloc(size_t size) //unsigned int calloc - void* calloc(size_t num, size_t size) int *p = (int*) calloc(3, sizeof(int)) Memory (heap)   200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

malloc, calloc, realloc, free Allocate block of memory malloc - void* malloc(size_t size) //unsigned int calloc - void* calloc(size_t num, size_t size) realloc - void* realloc(void *ptr, size_t size) Memory (heap)   200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

#include<stdio. h> #include<stdlib #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int A[n]; for (i = 0; i < n; i++) A[i] = 1+i; printf("the value is %d\n", A[i]); }

malloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; printf("%d ", A[i]); }

calloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) calloc(n,sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; printf("%d ", A[i]); }

calloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) calloc(n,sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) printf("%d ", A[i]); }

free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; free(A); printf("%d ", A[i]); }

free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; //free(A); printf("%d ", A[i]); }

free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; free(A); A[3] = 9; printf("%d ", A[i]); }

free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; free(A); A = NULL; //Adjust pointer to NULL after free printf("%d ", A[i]); }

realloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, 2*n*sizeof(int)); printf("Previous block address = %p, new address = %p\n", A,B); for (i = 0; i < 2*n; i++) printf("%d\n", B[i]); }

realloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, 2*n*sizeof(int)); printf("Previous block address = %p, new address = %p\n", A,B); for (i = 0; i < 2*n; i++) printf("%d\n", B[i]); }

realloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, (n/2)*sizeof(int)); printf("Previous block address = %p, new address = %p\n", A,B); printf("%d\n", B[i]); }

realloc as free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, 0); //equivalent to free(A) printf("Previous block address = %p, new address = %p\n", A,B); printf("%d\n", B[i]); }

realloc as malloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(NULL, n*sizeof(int)); //equivalent to malloc printf("Previous block address = %p, new address = %p\n", A,B); printf("%d\n", B[i]); }