CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

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 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Informática II Prof. Dr. Gustavo Patiño MJ
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 Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
C++ Pointer and Functions
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Run-Time Storage Organization
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.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Run-time Environment and Program Organization
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
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.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Runtime Environments Compiler Construction Chapter 7.
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. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Writing a Good Program 8. Elementary Data Structure.
C++ Memory Overview 4 major memory segments Key differences from Java
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.
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.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
POINTERS.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
System Programming Practical Session 7 C++ Memory Handling.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Dynamic Storage Allocation
Stack and Heap Memory Stack resident variables include:
Overview 4 major memory segments Key differences from Java stack
Motivation and Overview
Run-time organization
CSC113: Computer Programming (Theory = 03, Lab = 01)
Checking Memory Management
Student Book An Introduction
Pointers Revisited What is variable address, name, value?
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
Overview 4 major memory segments Key differences from Java stack
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

Logical Structure of Computer Memory address (hex) int, pointer etc. (4 byte = 32bits) Lower address Higher address (Assuming a typical 32-bit computer) 0 (0x0) 4 (0x4) 8 (0x8) 12 (0xc) 16 (0x10) 20 (0x14) : : char (1byte)

Three types of data storage Lower address Higher address (The location of each memory area may be different depending on compiler implementation.) Automatic Memory (Stack) Static Memory Code (functions) Free Store (Heap) (grows in this direction) dynamically allocated/deleted data by new / delete global variable etc. automatic variable (local variable)

Memory map in a C++ program int x = 10 int y = 20; void func() {... } int main() { int i = 123; int *p; p = new int; *p = 456;... } Stack Static Memory Code Heap (grows in this direction) p (0x299b0) y (20) func()’s code main()’s code i (123) 456 x (10) 0xefffa9c 0xefffa98 (The addresses in the picture are examples. Actual addresses depend on programs and compilers.) 0x299b0 0x x x138f4 0x13910

Stack and stack frame void func2() { int x, y; } void func1() { int p, q; func2(); } int main() { int a, b; func1(); } Stack frame for main When entering a function, a new stack frame is created. When execution of the function is done, its stack frame will be removed from the stack top. Removed stack frames cannot be accessed. b a q x p y Stack frame for func1 Stack frame for func2 Stack is used for storing local variables (and return address (but not illustrated in this slide)) (Stacking order within a stack frame depends on compiler implementation.)

Stack and stack frame void func2(int q1, int q2) { int e, f; } void func1(int p1, int p2) { int c, d; func2(); } int main(int argc, char* argv[]) { int a, b; func1(); } Function arguments are also local variables argv argc b p2 a p1 c d q2 q1 e f

Stack and Recursion #include using namespace std; int sum(int x) { if (x == 0) { return 0; } else { return x + sum(x – 1); } int main() { cout << sum(5) << endl; return 0; } Stack allows recursion 2 nd x=4 4 th x=2 5 th x=1 3 rd x=3 1 st x=5 6 th x=0

Pointers int* a; // 'a' is a pointer to int int b = 17; // b, c contains int int c; a = &b; // a=200 (address of b) c = *a; // c= a b 17 int int* address Contain an address of a data object operator '&' (applied to any type of variables) gives the variable’s address ex. &b gives b ’s address operator '*' (applied to pointers) gives the contents which is pointed by the pointer ex. *a gives the contents which is pointed by a (Applying '*' operator to a pointer is called ‘dereference’)

Pointers int* a;// just declaring that a is ‘a pointer to int ’ int b; int c; a = &b;// get b ’s address and store it into a c = *a;// get the contents which a points to and // store it into c Don’t get confused by the meaning of ‘ * ’...

Pointers to a pointer int** a; int* b; int* e; int c=17, d; a = &b; b = &c; d = *b; //d=17 e = *a; //e= a b 300 c int* int** int Contain an address of a pointer Any number of levels of pointing is possible. (pointer to pointer to pointer etc.)

Arrays of pointers int* a[3]; int b=5,c=99,d=17; a[0]=&b; a[1]=&c; a[2]=&d; int a[0] int b c d a[1] a[2] Have pointers as their array elements

Pointers to a function void func(float) {... } void (*a)(float); a = func; (*a)(1.2); //causes function call a(1.2); //causes function call void func(int a) {... } 200 void(*a)(int) 200 a func Have an address of a function

Operations on pointers int a[10], b; int* c; c = &a[0];// c points to a[0] c += 2;// c points to a[2] // (value of c increases // by 8 (=2*sizeof(int)) c++;// c points to a[3] b = *c++;// a[3] is copied to b // and c points to a[4] *c = 3;// a[4] becomes 3 (*c)++;// a[4] is incremented

Dynamic storage allocation int *a = new int;// allocates 1 int object int *b = new int[100];// allocates an array of int delete a;// deallocation of 1 int delete[] b;// deallocation of an int array delete b;// wrong deallocation !! // (only b[0] will be deallocated) new / delete - Dynamically allocates/deletes a memory chunk on the heap - Used for creating dynamic data structure such as a linked list - For each use of new, corresponding delete must exist - delete[] must be used for deletion of arrays - Do not mix with malloc()/free()

References int a, b; int& x;// x is declared as a reference int& y;// y is declared as a reference x = a;// hereafter, x is an alias of a. // a can be referred with a name ’x’ x = 1;// means ’a = 1’ x = b;// now, b is just copied to a // (because x is an alias of a) y = 1;// error !! (no initialization) References are ‘aliases’ (Initialization is required)

References int& a;// declare that a is ‘a reference to int ’ int* b; // declare that b is ‘a pointer to int ’ int c; int d; a = c;// initialize reference a with c b = &a;// get a ’s address (meaning, c ’s address) // and store it into b d = *b;// get the contents which b points to and // store it into d (meaning, d will have the same // value as c ) Don’t get confused by the meaning of ‘ & ’...

Ex. usage of references void increment(int *p) { *p = *p + 1; } main() { int a = 1; increment(&a); } void increment(int &p) { p = p + 1; } main() { int a = 1; increment(a); } [pointer] [reference]

const keyword int x; int* const y = &x;// a const pointer y y++;// error !! (y itself is const) *y = 10;// OK (what is pointed by y // is not const) int x = 123;// a constant int 123 const int *x = y;// a pointer to a constant int int const *x = y;// same meaning as above *x = 10;// error !! (what is pointed by // x is const) x++;// OK (x itself is not const) int x = 123; const int& y = x;// const reference y = 10;// error !! const keyword specifies “read-only”

Arrays and const int a[10];// OK. 10 is constant. int size = 10; int a[size];// error !! size is not a constant. // (Actually, g++ has a language // extension and accepts this. // But generally speaking, // you should not do this. // It’s not a portable way. const int size = 10; int a[size];// OK int& a[10];// error !! ref. requires init. // (arrays of references cannot // be initialized.)