Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Dynamic memory allocation
An introduction to pointers in c
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
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Programming and Data Structure
SPL/2010 Pointers and Parameter Passing in C++ 1.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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.
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.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Pointers. 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.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Semantics of Arrays and Pointers By: M. Reza Heydarian Introduction Pointers Arrays Semantics of Arrays Semantics of Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Stack and Heap Memory Stack resident variables include:
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Understanding Program Address Space
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly with memory that has been dynamically allocated efficiently work with complex data types such as large structure, linked lists, and arrays.

Definition: A pointer is a variable whose content is the address of another variable. The size of the pointer variable must be n bits where 2 n bytes is the size of the address space. On our current Intel x86 machines, the amount of space required to hold a pointer variable is 8 bytes and is not related to the size of the entity to which it points. Introduction to Pointers

To declare a pointer in a program just use the type it points to followed by *. int *a; short *b; unsigned char *c; Introduction to Pointers

You can declare a pointer and have it point to (make its value be) that location in memory for the variable called x as follows: int x = 7; // variable x of type integer int *ptr; // a pointer to an integer variable ptr = &x; // pointer points to memory location x The asterisk (*) in the declaration denotes that ptr is to be a pointer and the int indicates the type of data to which ptr will point. The address operator (&) in the third line refers to the address of x. Introduction to Pointers

What is physically going in RAM? int x = 7; int *ptr = &x; x ptr 7 x *

Dereferencing a Pointer If you want to change the value that ptr is pointing to, you can write the following: *ptr = 25; same effect as, x = 25; In either case, the value at that memory location was changed to 25. Remember: A direct assignment to a pointer variable will change the address of the pointer, not the value at the address that it is pointing to. A pointer directly refers to the address of a variable, and the * operator on the pointer will allow you to change the value at the location of that address via dereferencing (indirection).

Like all variables, pointers must be initialized before they are used. /* p17.c */ /* Example of a common error: failure to intialize */ /* a pointer before using it.. This program is */ /* is FATALLY flawed.... */ main() { int* ptr; *ptr = 99; printf("val of *ptr = %d and ptr is %p \n", *ptr, ptr); } But unfortunately, on Linux this program appears to work! class/210/examples ==> p17 val of *ptr = 99 and ptr is 0x Initialization of pointers

The program appears to work because the address 0x just happens to be a legal address in the address space of this program. Unfortunately, it may be the address of some other variable whose value is now 99!!! This situation is commonly referred to as a loose pointer and bugs such as these may be very hard to find. Initialization of pointers

We can convert the bug from latent to active by changing the location of the variable ptr. Here we move it down the stack by declaring an array of size 200. class/2100/examples ==> cat p18.c /* p17.c */ /* Example of a common error: failure to intialize */ /* a pointer before using it */ main() { int a[200]; // force ptr to live down in uninit turf int* ptr; printf("val of ptr is %p \n", ptr); *ptr = 99; printf("val of *ptr = %d and ptr is %p \n", *ptr, ptr); } Initialization of Pointers

class/2100/examples ==> p18 val of ptr is (nil) class/2100/examples ==> Note that in this case the second printf() is not reached because the program segfaulted and died when it illegally attempted to assign the value 99 to memory location 0 (nil). Initialization of Pointers

Never declare a pointer without initializing it in the declaration. int *ptr = NULL; Minimizing latent loose pointer problems

Stack and Heap Memory Stack resident variables include: –Parameters passed to functions –Variables declared inside basic blocks that are not declared static. For stack resident variables, the size of the variables must be known at compile time. Heap resident variables include: –Variables declared outside of all functions. –Variables declared inside basic building blocks that are declared static. –Memory areas dynamically allocated at run time with malloc().

In the C language, variables that are declared within any basic block are allocated on the runtime stack at the time the basic block is activated. /* p19.c */ main() Addresses of y and ptr { int y; int* ptr; static int a; ptr = &y; // assign the address of y to the pointer *ptr = 99; // assign 99 to what the pointer points to (y) printf("y = %d ptr = %p addr of ptr = %p \n", y, ptr, &ptr); ptr = &a; printf("The address of a is %p \n", ptr); } Correct use of the pointer

class/210/examples ==> p17 y = 99 ptr = 0xbffff894 addr of ptr = 0xbffff890 The address of a is 0x804958c Note that a is heap resident and has an address far removed from the stack resident y and ptr. Correct use of the pointer

class/210/examples ==> p17 y = 99 ptr = 0xbffff894 addr of ptr = 0xbffff890 The address of a is 0x804958c Note that a is heap resident and has an address far removed from the stack resident y and ptr. Correct use of the pointer