Array & Pointers CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Pointers OVERVIEW.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform.
Chapter 18 Vectors and Arrays Bjarne Stroustrup
CSC 533: Programming Languages Spring 2016
Pointers and Classes.
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Dynamic Storage Allocation
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Chapter 18 Vectors and Arrays
CSC 533: Programming Languages Spring 2015
Pointers & Arrays.
Motivation and Overview
Pointers.
Dynamic Memory CSCE 121 J. Michael Moore.
Programming Languages and Paradigms
Chapter 18 Vectors and Arrays
Pointer Data Type and Pointer Variables
Circular Buffers, Linked Lists
Anatomy of a Function Part 2
Introduction to the Standard Template Library
Compound Data CSCE 121 J. Michael Moore.
IO Overview CSCE 121 J. Michael Moore
Manipulators CSCE 121 J. Michael Moore
Pointers, Dynamic Data, and Reference Types
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Anatomy of a Function Part 3
How Functions Work Part 1
Chapter 12 Pointers and Memory Management
Input Validation CSCE 121 J. Michael Moore
Standard Template Library Find
Standard Template Library Model
By Hector M Lugo-Cordero September 17, 2008
Dynamic Memory A whole heap of fun….
STL: Traversing a Vector
Pointers and Arrays Beyond Chapter 16
Vectors CSCE 121 J. Michael Moore.
Guidelines for Writing Functions
Dynamic Memory A whole heap of fun….
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Pointer & Memory Allocation Review
CS250 Introduction to Computer Science II
Pointers & Arrays.
Chapter 9: Pointers and String
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CSCE 206 Lab Structured Programming in C
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
CSC 533: Programming Languages Spring 2019
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Array & Pointers CSCE 121 J. Michael Moore

Arrays We’ve avoided them so far preferring to use vectors Frequently are pointers. No bounds checking. Arrays do not know their sizes.

Array Offsets / Indexing int ary[20]; Defines a pointer and memory large enough for 20 integers. ary[index] *(address of first element + index * size of type) ary[15] *(address of first element + 15 * size of type)

Array Offsets / Indexing int ary[20]; cout << "address of first element: " << &ary[0] << endl; cout << "address of ary: " << &ary << endl; cout << "value in ary: " << ary << endl; cout << "address of 5th element: " << &ary[4] << endl; cout << "address of 5th element: " << ary + 4 << endl; cout << "value of 5th element: " << ary[4] << endl; Pointer arithmetic.

char* f() { char ch[20]; char* p = &ch[90]; // ... *p = 'a'; // we don’t know what this will overwrite char* q; // forgot to initialize *q = 'b'; // we don’t know what this will overwrite return &ch[10]; // Oops! ch disappears upon return // from f(); an infamous “dangling pointer” } void g() { char* pp = f(); *pp = 'c'; // we don’t know what this will overwrite; // f’s ch is gone after f’s return

cstring An array of char. Fixed size. Last character must be followed by ‘\0’. Reading cstring continues until it finds ‘\0’. If ‘\0’ not included in string, will read until??? Same problems with arrays. Where have we seen cstrings???

So Why Bother with Arrays? They are all that C has lots of C code “out there” (billions of lines) lots of C++ code in C style (hundreds of millions of line) You’ll eventually encounter code full of arrays and pointers They represent primitive memory in C++ programs we need them (mostly on free store allocated by new) to implement better container types, like vector Avoid arrays whenever you can largest single source of bugs in C and (unnecessarily) in C++ programs among the largest sources of security violations, usually (avoidable) buffer overflows

Acknowledgement Including slides created by Bjarne Stroustrup and Jennifer Welch