7. Pointers, Dynamic Memory

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 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Today’s Agenda Array of Structures and pointers Dynamic Arrays – Integer Arrays – Float Arrays – Character Arrays – Array of structures Dynamic allocation.
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.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
Kernighan/Ritchie: Kelley/Pohl:
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.
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);
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Functions & Pointers in C Jordan Erenrich
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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;
 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.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Computer science C programming language lesson 3.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Stack and Heap Memory Stack resident variables include:
C Primer.
Day 03 Introduction to C.
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
14th September IIT Kanpur
5. Arrays, Pointers and Strings
Pointers  Week 10.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers and dynamic memory
Dynamic Memory Allocation
CS111 Computer Programming
Example passing the address of x x pint or
prepared by Senem Kumova Metin modified by İlker Korkmaz
By Hector M Lugo-Cordero September 17, 2008
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Pointers and Arrays Beyond Chapter 16
C (and C++) Pointers April 4, 2019.
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
15213 C Primer 17 September 2002.
Pointers.
Presentation transcript:

7. Pointers, Dynamic Memory 20th September IIT Kanpur C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Agenda Pointer to Pointer Dynamic Memory Allocation Pointer to functions C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Pointer to Pointer Declaration Place an additional asterisk newbalance is a pointer to a float pointer. double **newbalance; C Course, Programming club, Fall 2008

Pointer to Pointer contd.. {program: pointers.c} #include <stdio.h> int main() { int x, *p, **q; x = 10; p = &x; q = &p; printf(“%d %d %d\n”, x, *p, **q); return 0; } C Course, Programming club, Fall 2008

Dynamic Memory Allocation To allocate memory at run time. malloc(), calloc() both return a void* you’ll need to typecast each time. char *p; p = (char *)malloc(1000); /*get 1000 byte space */ int *i; i = (int *)malloc(1000*sizeof(int)); C Course, Programming club, Fall 2008

Dynamic Memory Allocation contd.. To free memory free() free(ptr) frees the space allocated to the pointer ptr int *i; i = (int *)malloc(1000*sizeof(int)); . free(i); C Course, Programming club, Fall 2008

C Course, Programming club, Fall 2008 Pointers to functions A function pointer stores the address of the function. Function pointers allow: call the function using a pointer functions to be passed as arguments to other functions return_type (*function_name)(type arg1, type arg2…) {program: function_pointer.c} C Course, Programming club, Fall 2008