Pointers and Arrays Beyond Chapter 16. 16-2 Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Lectures 10 & 11.
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.
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.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
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’;
CS414 C Programming Tutorial Ben Atkin
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Run-time Environment and Program Organization
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
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.
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
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.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC Programming for Science Lecture 34: Dynamic 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.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
ENEE150 Discussion 07 Section 0101 Adam Wang.
Chapter 19 Data Structures
malloc(): Dynamic Memory Management
Day 03 Introduction to C.
Checking Memory Management
14th September IIT Kanpur
Pointers, Dynamic Data, and Reference Types
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
CS111 Computer Programming
Chien-Chung Shen CIS/UD
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
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.
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Pointers and Arrays Beyond Chapter 16

16-2 Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed anywhere in memory Array A contiguous chunk of allocated ROM The Array Name is the address of where the data starts The address in the Array Name may NOT be changed  E.g. It cannot point anywhere else Similarities Both passed as a reference to a function call

16-3 Passing arguments to functions int function(int *intArray, int size); intArray passed in stack May perform arithmetic on intArray (e.g. intArray++ )‏ int function(int intArray[], int size); Too slow to copy array into stack as argument intArray reference passed on stack (not array)‏ May perform arithmetic on intArray (e.g. intArray++) ‏ Since just a pointer is passed, the function does not know how big the array is, so the size is usually passed in separately.

16-4 Pointers vs Array – Compiler’s knowledge helps char charArray[6] = “world”; char *charPtr = “hello”; What do they look like? R0 = charArray[4] – translate into LC-3 R0 = charPtr[4] – translate into LC-3

Arrays, and Pointers Pointer and Array equivalence Array name is “pointer” to first element in array int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x; *y = x[3]; *x = y[6]; y[4] = *x; y = &x[4]; *y = x[9]; 16-5

Pointer Arithmetic int x[10] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x + 1; ++y; ++x; /* can’t do */ *y = ++x[3]; *(y+1) = x[5]++; *x = y[6]; y[4] = *x; y = &*(x+4) *y = *(x + 9); 16-6

16-7 Arrays of Pointers char *names[] = {“hello”,”how”,”are”,”you?”}; What is it? What does this look like?

16-8 char *names[] = {“hello”,”how”,”are”,”you?”}; char *word = names[1]; char **all = names+1; What are the values of the following? names[3][1] **names *(names[0]+3)‏ *(++word)‏ *(*(++all)+1)‏

16-9 multidimensional array int myInts[3][2] = {{0,1},{5,7},{-1,-3}}; Allocated as one contiguous chunk of memory. Looks like: myInts[2][1] – compiler translates to:

16-10 On your own (or with a partner): Draw the memory for the following declarations int intArray[][3] = {{2,4,6},{1,4,9},{1,3,5},{5,7,9}}; int *squares = &intArray[3][1]; int *odd = intArray[2];

16-11 Returning arrays from functions Assume that str is never more than 128 characters What is wrong with this function? char *copyString(char *str)‏ { char buffer[128]; int index = 0; while ((str[index] != ‘\0’) && (index < 128))‏ { buffer[index] = str[index]; index++; } buffer[index] = ‘\0’; return buffer; }

Dynamic Memory Allocation void *malloc(size_t size) malloc allocates size number of bytes in memory and returns a pointer to it. The memory is not cleared. Use: char *line; int *x; line = (char *) malloc (sizof(char) * 80); x = (int *) malloc(sizeof(int) * 10); 16-12

Dynamic Memory Allocation void free(void *ptr) free releases the memory pointed to by ptr. The memory must have been created by malloc or one of its kind. Use: char *line; line = (char *) calloc (80, sizof(char));. free(line); 16-13

Memory Layout Dynamically allocated memory is placed on the heap Dynamically allocated memory is not automatically freed. Code (instructions) Global/Static Data Heap Run-time stack 16-14

16-15 Fixing the code: char *copyString(char *str)‏ { int len = strlen(str); char *buffer = ________________________; int index = 0; while (str[index] != ‘\0’)‏ { buffer[index] = str[index]; index++; } buffer[index] = ‘\0’; return buffer; }

How To Dynamically Allocate char *names[] = {“hello”,”how”,”are”,”you?”}; What does this look like? 16-16

How To Dynamically Allocate int myInts[][2] = {{0,1},{5,7},{-1,-3}}; What does this look like? 16-17