Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.

Slides:



Advertisements
Similar presentations
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
Advertisements

1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Stack and Heap Memory Stack resident variables include:
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
Dynamic memory allocation and Pointers Lecture 4.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Dynamic Allocation in C
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Computer Science 210 Computer Organization
Introduction to Programming Using C
Pointers.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Pointers and Pointer-Based Strings
Checking Memory Management
C Basics.
Pointer.
Programming Languages and Paradigms
Pointers Psst… over there.
Computer Science 210 Computer Organization
Pointers and References
Pointers Psst… over there.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Understanding Program Address Space
Pointers.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Dynamic Allocation in C
CS111 Computer Programming
C++ Pointers and Strings
Pass-by-Pointer Pointers are also used in C to enable a function to modify a variable held by the caller: void findExtrema(const int *pA, int Sz, int *pMin,
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
Data Structures and Algorithms Introduction to Pointers
A simple function.
Pointer Basics Psst… over there.
Pointers and References
C++ Pointers and Strings
Pointers and pointer applications
Pointers, Dynamic Data, and Reference Types
Introduction to Pointers
Dynamic Data Structures
Presentation transcript:

Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the job of the operating system (OS) to: - manage the allocation of memory to processes - keep track of what particular addresses each process is allowed to access, and how - reserve portions of memory exclusively for use by the OS - enforce protection of the memory space of each process, and of the OS itself - do all this as efficiently as possible

Pointer Concepts memory addresses contents pointer a variable whose value is a memory address pointee a value in memory whose address is stored in a pointer; we say the pointee is the target of the pointer memory . . . 0x00001010 A: 42 0x0000100C 0x00001008 B: 0x00001010 0x00001004 C: 0x00001008 0x00001000 0x00000FFC D: 0x00000FF8 0x00000FF8 ?? addresses contents

Pointer Concepts Since memory addresses are essentially just integer values, pointers are the same width as integers. A pointer has a type, which is related to the type of its target. Pointer types are simple; there is no automatic initialization. A pointer may or may not have a target. Given a pointer that has a target, the target may be accessed by dereferencing the pointer. A pointee may be the target of more than one pointer at the same time. Pointers may be assigned and compared for equality, using the usual operators. Pointers may also be manipulated by incrementing and decrementing, although doing so is only safe under precisely-defined circumstances. By convention, pointers without targets should be set to 0 (or NULL).

C Syntax: Declaring Pointers Declarations: int* p1 = NULL; // declaration of pointer-to-int char *p2 = 0; // pointer-to-char int **p3 = NULL; // pointer-to-a-pointer-to-int One syntax gotcha: int* q1 = NULL, q2 = NULL; // q2 is an int, not a pointer! int *q1 = NULL, *q2 = NULL; // q1 and q2 are both pointers

C Syntax: address-of Operator &X returns the address of the object X; the address-of operator int x = 42, y = 99; int* p1 = &x; // p1 stores address of variable x int* p2 = &y; // p2 stores address of variable y int** p3 = &p2; // p3 stores address of variable p2 . . . x: 42 y: 99 p1: xx p2: xx p3: xx

C Syntax: dereference Operator *P names the target of the pointer P; the dereference operator int x = 42, y = 99; int* p1 = &x; // p1 stores address of variable x int* p2 = &y; // p2 stores address of variable y int** p3 = &p2; // p3 stores address of variable p2 int aa = *p1; // aa stores value of the target of p1, 42 *p1 = 10; // the target of p1, which is x, stores 10 int bb = *p3; // illegal: *p3 is a pointer-to-int but bb // is just an int int bb = **p3; // bb stores value of the target of the // target of p3; p3 points to p2 and // p2 points to y, so bb gets value 99

Understanding C Syntax int Z = 42; int *X = &Z; . . . X refers to X, type is int* &X refers to address of X, type is int** *X refers to target of X, type is int *&X refers to target of address of X, which is just… X &*X refers to address of target of X, which is just… the value of X (only makes sense syntactically if X is a pointer)

C Example int main() { int x = 42, y = 99; int* p1 = &x; // p1 stores address of variable x int* p2 = &y; // p2 stores address of variable y int** p3 = &p2; // p3 stores address of variable p2 int aa = *p1; // aa stores value of the target of p1, 42 *p1 = 10; // the target of p1, which is x, stores 10 int bb = **p3; // bb stores value of the target of the // target of p3; p3 points to p1 and // p1 points to x, so bb gets value 99 return 0; }

C View &x the address of x x is an int &x is an int* int main() { . . . int* p1 = &x; // p1 is assigned the address of variable x int* p2 = &y; // p2 is assigned the address of variable y } &x the address of x x is an int &x is an int* &y the address of y

C View &p2 the address of p2 p2 is an int* &p2 is an int** int main() { . . . int** p3 = &p2; // p3 is assigned the address of variable p2 } &p2 the address of p2 p2 is an int* &p2 is an int**

C View *p1 the target of p1 p1 points to x x has the value 42 int main() { . . . int aa = *p1; // aa is assigned value of the target of p1; // p1 points to x; // x has the value 42 } *p1 the target of p1 p1 points to x x has the value 42 aa is assigned 42 Value of *p1 = value of target of p1 = value of x = 42

C View int main() { . . . *p1 = 10; // the target of p1, which is x, // is assigned the value 10 }

C View *p3 the target of p3 p3 points to p2 p2 points to y int main() { . . . int bb = **p3; // bb stores value of the target of the // target of p3; p3 points to p1 and // p1 points to x, so bb gets value 99 } *p3 the target of p3 p3 points to p2 p2 points to y y has the value 99 Value of **p3 = value of target of target of p3 = value of target of p2 = value of y = 99

Pointer Comparisons Pointers may be compared using the usual relational operators. p1 == p2 Do p1 and p2 have the same target? p1 < p2 Does p1 point to something "below" p2? *p1 == *p2 Do the targets of p1 and p2 have the same value?

Pointers as Parameters #include <stdint.h> int main() { uint32_t X = 100; uint32_t Y = 200; Swap(&X, &Y); return 0; } void Swap(uint32_t* A, uint32_t* B) { uint32_t Temp = *A; // Temp = 100 *A = *B; // X = 200 *B = Temp; // Y = 100 The pass-by-pointer protocol provides a called function with the ability to modify the value of the caller's variable.

Evil: Dangling Pointers and Aliases The most common source of errors with direct pointer use is to dereference a pointer that does not have a valid target: int *A; *A = 42; // A never had a target int *A = NULL; if ( A != NULL ) // used correctly, NULL *A = 42; // lets us check int *A = malloc( sizeof(int) ); // A has a target int *B = A; // B shares it; alias free(A); // neither has a target *B = 42; // ERROR

Evil: Dangling Pointers and Aliases What about doing this: int *A; if ( A != NULL ) // used correctly, NULL *A = 42; // lets us check Or this: void f(int *A) { if ( A != NULL ) *A = 42; }

Pointers and Raw Memory Suppose that we have a region of memory initialized as shown below, and a pointer p whose target is the first byte of the region: . . . 00000001 00000010 00000011 00000100 00000101 00000110 00000111 uint8_t* p8 increasing addresses  Then *p8 would evaluate to the single byte 000000001.

Pointers and Raw Memory Now suppose that we have a region of memory initialized as shown below, and a pointer p16 whose target is the first byte of the region: . . . 00000001 00000010 00000011 00000100 00000101 00000110 00000111 uint16_t* p16 increasing addresses  Then *p16 would evaluate to the two- byte value 000000001 00000010

increasing addresses  Pointer Casts Now suppose that we have a region of memory initialized as shown below, and a pointer p whose target is the first byte of the region: . . . 00000001 00000010 00000011 00000100 00000101 00000110 00000111 uint8_t* p increasing addresses  Then we can apply a typecast to the pointer p to access two bytes: *( (uint16_t*) p ) The expression above evaluates to the two-byte value: 000000001 00000010

increasing addresses  Understanding a Cast . . . 00000001 00000010 00000011 00000100 00000101 00000110 00000111 uint8_t* p increasing addresses  *( (uint16_t*) p ) This creates a nameless temporary pointer that: - has the type uint16_t* - has the same value as p (so it points to the same target as p) This does not change anything about p.

Pointers and Raw Memory Accesses To generalize, size matters:

In closing… I shot a pointer to the heap, and where it hit I did not care. Now my code does make me weep, and segfaults make me tear my hair. - anonymous