More Pointers in C Computer Organization I 1 August 2009 ©2006-09 McQuain, Feng & Ribbens Allocating Arrays Dynamically You allocate an array by.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Lectures 10 & 11.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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:
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:
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.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
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.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
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.
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.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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 OVERVIEW.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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 in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
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.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Duke CPS 6. 1 arrays and strings: pointers and memory allocation l Why not rely solely on string and Vector classes? ä how are string and Vector implemented?
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.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Dynamic Allocation in C
Dynamic Storage Allocation
Computer Organization and Design Pointers, Arrays and Strings in C
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
struct Copy Operation In C, variables are copied in three situations:
Pointers and Memory Overview
Programmazione I a.a. 2017/2018.
Programming Languages and Paradigms
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Allocation in C
Homework Starting K&R Chapter 5 Good tutorial on pointers
C Programming Lecture-8 Pointers and Memory Management
SPL – PS2 C++ Memory Handling.
Presentation transcript:

More Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Allocating Arrays Dynamically You allocate an array by allocating a suitably-sized block of memory: int N;... // assume N is assigned a value int *A = malloc( N * sizeof(int) ); // allocate array for (int pos = 0; pos < N; pos++) { // access using ptr name A[pos] = pos * pos; } Any pointer name can be used with array syntax (bracket notation)… but you'd better make sure that the pointee really is an array.

More Pointers in C Computer Organization I 2 August 2009 © McQuain, Feng & Ribbens Array Names vs Pointers int N;... // assume N is assigned a value int *A = malloc( 1000 * sizeof(int) ); // allocate array int B[1000]; // declare array for (int pos = 0; pos < N; pos++) { // access using ptr name A[pos] = pos * pos; B[pos] = pos * pos; } free( A ); // OK; deallocates array A A = NULL; // OK free( B ); // NO! cannot deallocate static memory B = NULL; // NO! array name is const pointer So, what's the difference between an array name (static allocation) and a pointer?

More Pointers in C Computer Organization I 3 August 2009 © McQuain, Feng & Ribbens Arithmetic on Pointers int A[1000]; // allocate array; static doesn’t matter int *p = A; // p points to A[0] p = p + 100; // where does p point now?? p = p – 50; // now? p++; // now? p--; // now? So, what's the effect of: The effect of adding a value to a pointer depends on the pointer type:

More Pointers in C Computer Organization I 4 August 2009 © McQuain, Feng & Ribbens Arithmetic on Pointers The effect of adding a value to a pointer depends on the pointer type: When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

More Pointers in C Computer Organization I 5 August 2009 © McQuain, Feng & Ribbens Using Arithmetic on Pointers uint64_t strlen(const char* Source) { uint64_t Length = 0; while ( *Source != '\0' ) { Length++; Source++; } return Length; } Here's an implementation of a variation of the C library strlen() function; note the use of pointer arithmetic: And here's a slightly more idiomatic version: uint64_t strlen(const char* Source) { uint64_t Length = 0; while ( *Source++ != '\0' ) Length++; return Length; }

More Pointers in C Computer Organization I 6 August 2009 © McQuain, Feng & Ribbens Using Arithmetic on Pointers void strcpy(char dest[], const char source[]) { int i = 0; while ( true ) { dest[i] = source[i]; if (dest[i] == '\0') break; // we're done i++; } Here's an implementation of the C library strcpy() function: And here's a version that uses pointer arithmetic to achieve the same effect: void strcpy(char* dest, const char* source) { while ( (*dest++ = *source++) != '\0' ) ; }

More Pointers in C Computer Organization I 7 August 2009 © McQuain, Feng & Ribbens Identity vs Equality x equals yx and y, in some precise sense, have the same value In C, this is equivalent to x == y. x is identical to yx and y are actually the same object In C, this is equivalent to &x == &y. Side notes: If x and y are pointers, then x equals y if and only if x and y have the same target. In other words, two pointers are equal if and only if their targets are identical.

More Pointers in C Computer Organization I 8 August 2009 © McQuain, Feng & Ribbens Memory Leaks A memory leak occurs when a process allocates memory dynamically and then fails to deallocate that memory before losing access to it: int *p = malloc(1000 * sizeof(int));... p = malloc(1500 * sizeof(int)); // leaked original array... p = NULL; // leaked second array Memory leaks are common in badly-written C/C++ code. Taken to extremes, they can consume all available memory on a system. Garbage-collected memory management automates the process of deallocation, but can never be more efficient than well-written code that deallocates memory manually.