Computer science 2009-2010 C programming language lesson 3.

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.
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
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 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers Ethan Cerami Fundamentals of Computer New York University.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Computer Science 210 Computer Organization Pointers.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
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
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 17: The Last Puzzle Piece with Functions.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Engineering Computing I Chapter 5 Pointers and Arrays.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
UNIT 8 Pointers.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Stack and Heap Memory Stack resident variables include:
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
INC 161 , CPE 100 Computer Programming
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Computer Science 210 Computer Organization
14th September IIT Kanpur
5. Arrays, Pointers and Strings
Outline Defining and using Pointers Operations on pointers
7. Pointers, Dynamic Memory
Presentation transcript:

Computer science C programming language lesson 3

Aims of lesson 3 Pointers

Introduction In the computer memory, data are stored in a binary form. B01C … B01F Memory is divided into 8 bit cells called bytes. Each cell has its own address, an address being an hexadecimal number.

Introduction This statement implies two operations : float a; - Definition of a variable that can be used in the program to manipulate data. - Allocation of a memory space where the value of the variable will be stored. a B01C … B01F

Introduction Arguments are passed to functions by value : float a,b; a=5;b=a; fonc(a); Function fonc works with a copy of the variable a A function can not alter the value of a variable that is passed as argument : main() { float a; a=5; … fonc(a); printf(«%f\n»,a);} fonc(float a) {… a=0; … } « 5 » is displayed ! A function works with a copy of the variables. The variables inside the function are stored at another address. a adra 5 x adrx 5

The pointers Solution : - Pass the address of the variable instead of its value A pointer is a variable that contains the address of another variable. To access the address of an already existing data : & operator float n; Declaration of a pointer : - To manipulate this address, it is necessary to define a variable that contains it : what is its type ? float *p; p is a « pointer to a float » or *p is a float p is the address of the first memory cell that contains the data &n : represents the address of the variable n To access the value of the variable pointed by p : * operator (indirection or deferencing operator) *p represents the contents of the variable pointed by p

Example int n; int *p; … n=5; printf(«%d\n »,n); p=&n; printf(«%d\n »,*p); *p=1; printf(«%d\n »,n); printf(«%d\n »,*p); printf(«%x %x\n »,p,&n); printf(«%x %x\n »,p+1,&n+2 ); 5 5 *p is the contents of the memory cell pointed by p 1 1 BC01 BC01 (address in hexa) BC05 BC09

main( ) { double n; double *p; n=5; printf(«%lf\n»,n); p=&n; printf(«%lf\n»,*p); *p=1; printf(«%lf\n»,n); printf(«%lf\n»,*p); printf(«%x %x\n»,p,&n); printf(«%x %x\n»,p+1,&n+2 );}

Arrays and pointers Another example : declaration of an array - Definition of a variable that can be used in the program to manipulate data. - Allocation of a memory space of 100 adjacent cells of 4 bytes each int tab[100]; In fact an array is nothing else than a pointer ! (roughly) int tab[100]; for(i=0;i<n;i++) tab[i]=i; printf(«%d\n »,tab[0]); printf(«%d\n »,*tab); printf(«%d %d\n »,*(tab+1),tab[1]); printf(«%x %x\n»,tab,tab+5); the variable tab is a pointer that contains the address of the first element of the array AB08 AB0C

Advantage of arrays : no need to care about the memory allocation. Disadvantage : the size of the array should be known at the time it is declared (static allocation) Consequence : the size of the array has to be known at compile time. int n; int tab[n]; … n=100; for(i=1;i<n;i++) tab[i]=0; Arrays and pointers Another example : declaration of an array - Definition of a variable that can be used in the program to manipulate data - Allocation of a memory space of 100 adjacent cells of 4 bytes each int tab[100];

Dynamic allocation Solution : - Declare a variable containing an address i.e. a pointer - Dynamically allocate the necessary memory space at this address #include... int dim; int *tab;... dim = 100; tab=malloc(dim*sizeof(int)); for(i=0;i<dim;i++) tab[i]=0; - Memory allocation function : malloc(# bytes) - sizeof(type) : returns the size of a variable in # bytes

#include... int dim; int *tab;... dim=100; tab=malloc(dim*sizeof(int)); for(i=0;i<n;i++) tab[i]=0; Dynamic allocation Advantage : the dimension of an array does not need to be known to declare the variable that points to the array. - You have to free the memory space after use : The function is : But : before using an array you absolutely have to allocate its memory space ! … free(tab); free (pointer) But in general the program crashes here ! No problem at compile time

#include... int dim; int *tab;... dim=100; tab=malloc(dim*sizeof(int)); for(i=0;i<n;i++) tab[i]=0; Dynamic allocation Advantage : the dimension of an array does not need to be known to declare the variable that uses the array. - You have to free the memory space after use : The function is : But : before using an array you absolutely have to allocate its memory space ! … free(tab); free (pointer)

Pointer arithmetic - The operator + allows to move forward in the computer memory from the address contained in the pointer. If p points to a data of size T then p+5 points on the memory position that is 5T positions beyond int *tab; tab=malloc(100*sizeof(int)); for (i=0;i<100;i++) *(tab+i)=3*i; printf(«%d,%d,%d\n»,*tab,*(tab+1),*(tab+99)); Another possible writing printf(«%d,%d,%d\n»,tab[0],tab[1],tab[99]);

-The operator – allows to move backward. - The operators ++ et – allow to increment and decrement a pointer. NULL Pointer : by convention, it points to address #0000 float *p; p = NULL; Example : the malloc function returns a pointer on the allocated memory space, if the allocation is successful the NULL pointer if the allocation is unsuccessful tab=malloc(dim*sizeof(int)); if (tab!=NULL) for(i=0;i<dim;i++) tab[i]=0; else printf(« Allocation impossible\n »); Pointer arithmetic