Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Dynamic memory allocation
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.
Data Structures Using C++ 2E
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 6 Data Types
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Introduction to Programming Lecture 39. Copy Constructor.
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.
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.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
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.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Pointers & Dynamic Arrays Shinta P.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: 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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
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.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Windows Programming Lecture 03. Pointers and Arrays.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
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.
Dynamic Storage Allocation
Programming Languages and Paradigms
Basic notes on pointers in C
14th September IIT Kanpur
Data Structures and Analysis (COMP 410)
Data Representation Bits
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
By Hector M Lugo-Cordero September 17, 2008
Introduction to Problem Solving and Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Data Structures
Presentation transcript:

Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012

The Structure of a Program’s Memory Space Memory space of a program=a sequence of bytes –The bytes are numbered starting from 0: 0, 1, 2,... Every variable occupies a certain number of consecutive bytes in memory –An int occupies 4 bytes –A double occupies 8 bytes –A char occupies 1 byte The address of a variable = the index of the first byte occupied in memory –If an int variable occupies the bytes numbered 1000, 1001, 1002, 1003, the address of the variable is 1000

Memory Content & Pointers Every variable stores its value in the bytes it occupies in memory –A char stores its value in 1 byte –An int stores its value as 4 consecutive bytes Pointer = special type of variable –Its size = architecture-dependent (32 bits = 4 bytes in our case) –Stores an address (the index of a byte in memory) –The pointer points to the address it stores –Multiple types of pointers int* p (pointer to int) char* p (pointer to char) int** p (pointer to pointer to int) double*** p... Each type of pointer has 4 bytes and stores an address Differences: –int x; –int *p, char* q; –p = q = &x; –*p // refers to the int which starts at the address &x –*q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)

Pointers - example

Dynamic Memory Allocation We can assign to a pointer the address of an existing variable (or the address stored by another pointer) We can allocate memory on demand (dynamically) and assign the address of the allocated memory zone to a pointer new type_name => allocates a memory zone to store a value of the type type_name and returns its address new type_name[20] => allocates a contiguous memory zone to store 20 consecutive values of the type type_name and returns the address of the first such value (which is the address of the memory zone) Internally, the operating system stores the size of a dynamically allocated memory zone (how many bytes were allocated starting from the address of the memory zone) delete pointer to addr => deallocates the memory zone whose address is addr –addr must be the address of a memory zone which was previously dynamically allocated

Pointers & Dynamic Memory Allocation – Sample Program #include struct mystruct { int x; char b[20]; double d; struct mystruct *next; }; int x, *p1, **pp1, ***ppp1; int *v, **A; struct mystruct *sp, **spp, ***sppp, *vs, *e1, *e2, *e3, *e; int main() { x = 7; p1 = new int; *p1 = 10; printf("%d, %d\n", p1, *p1); p1 = &x; printf("%d, %d\n", p1, *p1); pp1 = new int*; *pp1 = new int; **pp1 = 11; printf("%d, %d, %d\n", pp1, *pp1, **pp1); *pp1 = p1; printf("%d, %d, %d\n", pp1, *pp1, **pp1); pp1 = &p1; printf("%d, %d, %d\n", pp1, *pp1, **pp1); x = 3; printf("%d, %d, %d\n", pp1, *pp1, **pp1); **pp1 = 5; printf("%d\n", x); ppp1 = &pp1; printf("%d, %d, %d, %d\n", ppp1, *ppp1, **ppp1, ***ppp1); ***ppp1 = 90; printf("%d\n", x); ppp1 = new int**; *ppp1 = new int*; **ppp1 = new int; ***ppp1 = 9; printf("%d, %d, %d, %d\n", ppp1, *ppp1, **ppp1, ***ppp1); printf("%d\n", x);

Pointers & Dynamic Memory Allocation – Sample Program (cont.) v = new int[100]; v[x = 17] = 8; printf("%d\n", v[x]); A = new int*[600]; A[345] = new int[512]; A[345][100] = 16; printf("%d, %d\n", A[345][100], A[345][101]); sp = new struct mystruct; sp->x = 17; sp->b[3] = 14; sp->d = 9.3; printf("%d, %d\n", sp->x, (*sp).x); spp = &sp; printf("%d, %d\n", (*spp)->x, (**spp).x); sppp = &spp; (***sppp).x = 9; printf("%d, %d, %d, %d\n", (***sppp).x, (**sppp)->x, sp->x, (*spp)->x); delete sp; printf("%d, %d, %d, %d\n", (***sppp).x, (**sppp)->x, sp->x, (*spp)->x); vs = new struct mystruct[100]; for (int i = 0; i < 50; i++) vs[i].x = 18; printf("%d, %d\n", vs[33].x, vs[66].x); delete vs; printf("%d, %d\n", vs[33].x, vs[66].x); e1 = new struct mystruct; e1->x = 8; e2 = new struct mystruct; e2->x = 19; e1->next = e2; e3 = new struct mystruct; e3->x = 23; e2->next = e3; e3->next = NULL; e = e1; while (e != NULL) { printf("e->x=%d\n", e->x); e = e->next; } return 0; }