Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.

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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
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.
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)
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
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.
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
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.
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{
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 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’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
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 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers Applications
Class 2 CSI2172
CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.
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
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
Memory Management in C Notes courtesy of Dr. D. Rothe, modified by Dr. R. Hasker.
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
14th September IIT Kanpur
Programming and Data Structures
Pointers and dynamic memory
CSC215 Lecture Memory Management.
CS111 Computer Programming
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
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
Presentation transcript:

Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation

pointer - * and &  What does these operator use for? * &  What is the difference given initialize int *a, b; *a = b; b = &a;

array - initialize  int a[5] = { 1, 2, 3, 4, 5};  int a[] = {1, 2, 3, 4, 5};  int a[7] = {1, 2, 3, 4, 5};  int a[5]; a = {1, 2, 3, 4, 5};  int *a = {1, 2, 3, 4, 5}  printf(“%d”, a[3]);

array - initialize  int a[2][3] = { 1, 2, 3, 4, 5, 6};  int a[2][3] = { { 1, 2, 3 }, {4, 5, 6 } };  int a[2][3] = { { 1, 2 }, {3, 4, 5 } };  int a[5][5] = { { 1, 2, 3 }, {4, 5, 6 } };

array - initialize  int a[][] = { { 1, 2, 3 }, {4, 5, 6 } };  int a[][3] = { { 1, 2, 3 }, {4, 5, 6 } };  int a[2][] = { { 1, 2, 3 }, {4, 5, 6 } };

Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation

array = pointer  int a[5] = {5, 4, 3, 2, 1}; int *p = a; int *q = p+3;  p is a pointer to first element of a  *q  *a  *a + 3  *(a+3)  p[2]  q[1]

Example int a = { 5, 4, 3, 2, 1}; int *p = a; for(int i=0; i<5; i++) { printf(“%d”, a[i]); printf(“%d”, *(a+i)); printf(“%d”, *p++); }

pointer > array int a[2][2] = { 1, 2, 3, 4}; int *p = &a[0][0]; for(int i=0; i<4; i++) { printf("%d", *p++); }

array != pointer  array cannot assign new value  compiler will allocate memory while declaring an array  array has fixed size wile pointer is more dynamic

Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation

sizeof()  return size of variable or type in byte  char i; char *p; char a[5];  printf("%d ", sizeof(char));  printf("%d ", sizeof(i));  printf("%d ", sizeof(p));  printf("%d ", sizeof(a));

malloc()  void *malloc(size_t size);  return a pointer to the first byte of a region of memory of size size that has been allocated from the heap  int *a = (int *)malloc(5 * sizeof (int));  int *a = (int *) malloc(5 * sizeof (a[0]));

calloc()  void *calloc(size_t num, size_t size);  allocate memory the size of which is equal to num * size.  int *a = calloc(5, size of (a[0]));

realloc()  void *realloc(void *ptr, size_t size);  change the size of the previously allocated memory pointed to by ptr to that specified by size.  int *a; … a = realloc(a, 5*sizeof(a[0]));

free()  void free(void *ptr);  return the memory pointed to by ptr to the heap  int *a = malloc(5 * size of (a[0])); … free(a);

pro & con  pro efficiently use of memory size is dynamic  con memory management is not done by compiler