Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
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:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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’;
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.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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,
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Buffer Overflow Walk-Through
Memory allocation & parameter passing
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Pointers.
Day 03 Introduction to C.
CSE 374 Programming Concepts & Tools
ENEE150 Discussion 07 Section 0101 Adam Wang.
Pointers and Memory Overview
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Programmazione I a.a. 2017/2018.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Buffer Overflow Walk-Through
Dynamic Memory Allocation
CSC 253 Lecture 8.
Programming and Data Structures
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Pointers.
Outline Defining and using Pointers Operations on pointers
Pointers and Arrays Beyond Chapter 16
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Overloading functions
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
Exercise Arrays.
Chapter 9: Pointers and String
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Chapter 16 Pointers and Arrays
Presentation transcript:

Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power brings with it the freedom to screw things up spectacularly as simple errors can corrupt the state of your program in ways that make debugging difficult. The goal of these slides is to make sure that you understand exactly what you're doing when you use pointers in your code. The iconic clay figure depicted in this slide is Binky of the claymation Pointer Fun with Binky.

Memory 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB Code and data for your program are stored in random-access memory (RAM), which is basically a huge array of 1 byte (8 bits) blocks. Each block is associated with a hexadecimal number that represents its memory address. Just as ints are variables that store integers and floats are variables that store floating-point values, a pointer is a variable that stores memory addresses. In the appliance (a 32-bit operating), a memory address is 4 bytes, so it makes sense that a pointer is also 4 bytes. 0x8 0x9 0xA 0xB

You should now be able to understand this comic!

<type>* <variable name> Creating Pointers Here's how to declare a pointer in C. Remember that a pointer's value is a memory address. Its type then describes the data located at that address. So in the first example, int* x declares a pointer to 4 bytes of memory that will contain an integer. Declaring pointers: <type>* <variable name> Examples: int* x; char* y; float* z;

Referencing and Dereferencing & is the reference, or address-of, operator. It returns the address in memory at which a variable is stored. * is the dereference operator. A pointer's value is a memory address. When the dereference operator is applied to a pointer, it returns the data stored at that memory address. Referencing: &<variable name> Dereferencing: *<pointer name>

Under the hood... Variable Address Value x 0x04 5 ptr 0x08 copy 0x0C int x = 5; int* ptr = &x; int copy = *ptr; Here are examples of the referencing and dereferencing operators in use. Let's think carefully about what's going on under the hood as each line executes. The first line declares an integer called x. 4 bytes are allotted for x at memory address 0x04, and the value 5 is stored there. The second line declares an int pointer called ptr. 4 bytes are allotted for ptr at memory address 0x08, and the address of x is stored there. The third line declares an integer called copy. 4 bytes are allotted for copy at memory address 0x0C, and the value of the integer that ptr is pointing to is stored there.

Track the values x ptr 5 &x 35 int x = 5; int* ptr = &x; *ptr = 35; Let's track the values of these variables as each line executes: In the first line, x is initialized to 5. In the second line, ptr is assigned x's memory address. x remains 5. In the third line, we dereference ptr (aka, go to the value it is pointing to) and change that value to 35.

Test Yourself a b c pa pb pc a = b * c; a *= c; b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int* pa = &a, *pb = &b, *pc = &c; Test yourself by tracking the values of these variables just as we did in the previous slide.

Answers a b c pa pb pc a = b * c; a *= c; b = *pa; pc = pa; 20 4 5 &a &b &c 100 500 10000 50000 a = b * c; a *= c; b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int* pa = &a, *pb = &b, *pc = &c; Answers to last slide's exercise!

Pointer Arithmetic Adding/subtracting n adjusts the pointer by n * sizeof(<type of the pointer>) bytes x y 5 0x04 0x08 You can adjust pointers by adding or subtracting an integer. Adding or subtracting n adjusts the pointer by n * sizeof(<pointer type>) bytes. For example, in the code on this slide, y is a pointer to an int called x, which is stored at 0x04. Therefore, adding 1 to y shifts the value of y by 1 * sizeof(int) or 4 bytes. int x = 5; int* y = &x; y += 1;

What will print? int main(void) { char* str = "happy cat"; int counter = 0; for (char* ptr = str; *ptr != '\0'; ptr++) counter++; } printf("%d\n", counter); Pay close attention to the syntax of this for loop. ptr starts out pointing to the first character of the string. Incrementing ptr shifts the pointer by 1 * sizeof(char)or 1 byte, moving ptr to the next character in the string. After the loop executes, the length of the string will be printed.

1 2 3 Pointers and Arrays 1 2 int array[3]; *array = 1; 1 2 1 2 3 int array[3]; *array = 1; *(array + 1) = 2; *(array + 2) = 3; Under the hood, an array is treated like a pointer to its first element, so standard pointer arithmetic applies: *array is equivalent to array[0] *(array+1) is equivalent to array[1] *(array + 2) is equivalent to array[2].

Dynamic Memory Allocation In this diagram of a program’s memory, the heap is a pool of available memory that can be allocated dynamically while your program is running. Whereas a function's local (i.e. stack) variables disappear when the function returns, memory allocated from the heap will persist until it is explicitly given back to the operating system.

A call to malloc() prototype: void* malloc(size in bytes); example: The C library provides a function called malloc() (short for memory allocation) that allocates a block of memory from the heap and returns a pointer to it. Whenever your program needs fresh storage at run time that must outlast the current function, it can call malloc(), passing it the number of bytes needed. Since the sizes of data types vary from system to system, C provides the sizeof() operator for determining the size of a particular type. In this example, sizeof(int) * 10 evaluates to 40 bytes on the appliance, so we are asking the OS for a pointer to 40 bytes of memory. **** Note that sizeof() is considered an operator not a function because it takes any kind of data type and returns the appropriate size, whereas a function must specify what type of argument it takes. **** prototype: void* malloc(size in bytes); example: int* ptr = malloc(sizeof(int) * 10);

Check for NULL! int* ptr = malloc(sizeof(int) * 10); if (ptr == NULL) Normally malloc() returns a pointer to a block of newly allocated memory from the heap. However, if there is an error, or the heap has been exhausted, malloc() returns NULL. For this reason, ALWAYS CHECK THE POINTER RETURNED BY MALLOC!! If the returned pointer is NULL, you must handle this case by signaling an error. Programs that don't perform this check can dereference NULL, which results in a segmentation fault. int* ptr = malloc(sizeof(int) * 10); if (ptr == NULL) { printf("Error -- out of memory.\n"); return 1; }

A call to free() prototype: void free(pointer to heap memory); When a program has finished using heap memory, it should release the storage by calling free() on the pointer that was originally produced by malloc(). All memory that is malloc'd must later be free'd (but only once), and only memory that was produced by malloc() should be free'd. prototype: void free(pointer to heap memory); example: free(ptr);

#include <stdio.h> #include <cs50.h> int main(void) { int* ptr = malloc(sizeof(int)); if (ptr == NULL) printf("Error -- out of memory.\n"); return 1; } *ptr = GetInt(); printf("You entered %d.\n", *ptr); free(ptr); Let's walk through this program: First, we ask the OS for a pointer to 4 bytes of memory in the heap. Next, we check to make sure malloc() did not return NULL. We then get an integer from the user, store it in the memory that malloc() gave us, and print the integer. Finally, we free the malloc'd memory.