Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.

Slides:



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

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
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.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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’;
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
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.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 2006 Pearson Education, Inc. All rights reserved Pointers.
C and Data Structures Baojian Hua
CSc 352 C : Arrays, Structs, Pointers
Memory Layout C and Data Structures Baojian Hua
Pointers Applications
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
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.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 03. Pointers and Arrays.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic Allocation in C
Intro to Pointers in C CSSE 332 Operating Systems
Computer Organization and Design Pointers, Arrays and Strings in C
CSE 374 Programming Concepts & Tools
Pointers.
CSC 253 Lecture 8.
Variables with Memory Diagram
CSC 253 Lecture 8.
Memory Allocation CS 217.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the programs is up, but if the results don't get back today, the due date for the corrections will change. 1

declaring strings Suppose you can assume a string won’t be longer than 64 characters. How long do you need to make the character array? 2

declaring strings Suppose you can assume a string won’t be longer than 64 characters. How long do you need to make the character array? 65 characters! Why? Because all strings must end with a NULL (‘\0’). If the string has 64 letters, the character array must have 65 letters to have a place for the ‘\0’ 3

Reminder C does not initialize variable values. If a value (including array values) are not set by the programmer, they can have any value. 4

Example 1 5 str_rev is a function of type “char *”, i.e., returns a pointer to a character the argument is an array (its size is not part of its type) string library functions array  pointer

Example 1… 6 figure out where the ‘\0’ is use this to control how many array elements to processes

Example 1… 7

Example 2: string reversal using pointers 8 array  pointer

Example 2… 9 abcd\0 s tptr

Example 2… 10

When 1 = 4 11 pointers of different types pointer arithmetic: add 1 to pointers of different types

When 1 = 4… 12 -o : “put the output in the file specified, instead of the default a.out” but each pointer was incremented by 1!!!

What’s going on Pointer arithmetic is performed relative to the size of the pointee type – for char* pointers, “+= 1” increments by 1 – for int* pointers, “+= 1” increments by 4 (if size of int = 4)  in general, “+= 1” will increment a pointer by the size (in bytes) of the type being pointed at analogously for other arithmetic Reason: portability: want code to be able to step through an array of values without worrying about architecture-dependent issues of their size 13

Figuring out sizes: sizeof() 14 sizeof() invoked with a type name sizeof() invoked with a variable name sizeof() invoked with a pointer dereference

More sizeof() sizeof() applied to an array returns the total size of that array – but be careful about implicit array/pointer conversions 15 what is passed to f() is a pointer, not the whole array

Dereferencing+updating pointers A common C idiom is to use an expression that  gives the value of what a pointer is pointing at; and  updates the pointer to point to the next element : *p++ – similarly: *p--, *++p, etc. 16 parsed as: * ( p++ ) evaluates to: value of p = some address a (side effect: p incremented by ‘++’) evaluates to: contents of location a = *p (side effect: p incremented by ‘++’)

Walking a pointer down an array 17 dereference the pointer to access memory, then increment the pointer

*p++ vs. (*p)++ after x = (*p)++ 18 p x p x after x = *p++ p x

Two common pointer problems Uninitialized pointers – the pointer has not been initialized to point to a valid location Dangling pointers – the pointer points at a memory location that has actually been deallocated 19

Background: Runtime Memory Organization Layout of an executing process’s virtual memory: 20 code global data memory mapped files operating system stack (grows downwards) heap (grows upwards) high addresses low addresses 0xffffffff 0x