Pointers and Dynamic Memory Allocation. Dynamic Data Suppose we write a program to keep track of a list of students How many student records should we.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
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.
Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Informática II Prof. Dr. Gustavo Patiño MJ
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’;
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Pointers, Dynamic Memory Allocation and Structures.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Class 2 CSI2172
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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,
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Dynamic Storage Allocation
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Day 03 Introduction to C.
© 2016 Pearson Education, Ltd. All rights reserved.
Computer Science 210 Computer Organization
Pointers and dynamic memory
Day 03 Introduction to C.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
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) {
Lecture 4: Process Memory Layout
Computer Science 210 Computer Organization
Dynamic Memory A whole heap of fun….
Memory Allocation CS 217.
Understanding Program Address Space
Pointers and dynamic memory
Dynamic Memory A whole heap of fun….
Chien-Chung Shen CIS/UD
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
C Programming Pointers
Dynamic Memory And Objects
Pointers, Dynamic Data, and Reference Types
POINTER CONCEPT 8/3/2019.
Run-time environments
Dynamic Data Structures
Presentation transcript:

Pointers and Dynamic Memory Allocation

Dynamic Data Suppose we write a program to keep track of a list of students How many student records should we create? What if we create too few? What if we create too many? Wouldn’t it be nice to create just as many as we need?!

Pointer Review 1.What is a pointer? 2.How does one declare a pointer variable? 3.If all pointers store an address, why must a data type be associated with a pointer? 4.What values are associated with a pointer? 5.When using indirection, what steps are followed to get the specified value *ptr in the ex. below? ie) int *ptr, x, y = 5; ptr = &y; x = *ptr;

Pointers to Structures Declare a pointer to a structure of type inventory_item

Pointers to Structures Declare a pointer to a structure of type inventory_item inventory_item *shirts; Store 1234 in the id field

Pointers to Structures Declare a pointer to a structure of type inventory_item inventory_item *shirts; Store 1234 in the id member field (*shirts).id = 1234;

Pointers to Structures Declare a pointer to a structure of type inventory_item inventory_item *shirts; Store 1234 in the id member field (*shirts).id = 1234; shirts->id = 1234;

Dynamic Memory Allocation Stack: Area where function data is allocated and reclaimed as program executed Heap: Area C sets aside assuming that the programmer will ask for more memory as program executes

Dynamic Memory Allocation malloc(…) –allocate a chunk of memory large enough to store an int –returns first address of location reserved – need to cast address to a valid type malloc(5) malloc(sizeof(int)) malloc(sizeof(struct_type)) malloc(10*sizeof(int)) free(var_name) –free up the memory pointed to by var_name

Examples int *int_ptr; int_ptr = (int*)malloc(sizeof(int)); inventory_item *shirts;

Examples int *int_ptr; int_ptr = (int*)malloc(sizeof(int)); inventory_item *shirts; shirts = (inventory_item*)malloc(sizeof(inventory_item); (*shirts).id = 1234; shirts->cost = 20.00;

Examples int *int_ptr; int_ptr = (int*)malloc(sizeof(int)); inventory_item *shirts; shirts = (inventory_item*)malloc(sizeof(inventory_item); (*shirts).id = 1234; shirts->cost = 20.00; free(int_ptr); free(shirts);