Pointer Lesson 1 CS1313 Spring 2015 1 Pointer Lesson 1 Outline 1.Pointer Lesson 1 Outline 2.A Pointer Experiment 3.Point! 4.What is a Pointer? 5. NULL.

Slides:



Advertisements
Similar presentations
Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Advertisements

Dynamic memory allocation
An introduction to pointers in c
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Programming and Data Structure
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Computer Science 210 Computer Organization Pointers.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
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
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Computer science C programming language lesson 3.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming Using C
CSE 220 – C Programming Pointers.
14th September IIT Kanpur
User Defined Functions 2 Outline
Return by Reference CSCE 121 J. Michael Moore.
EECE.2160 ECE Application Programming
CS111 Computer Programming
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Lecture-8 Pointers and Memory Management
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 16 Pointers and Arrays
Presentation transcript:

Pointer Lesson 1 CS1313 Spring Pointer Lesson 1 Outline 1.Pointer Lesson 1 Outline 2.A Pointer Experiment 3.Point! 4.What is a Pointer? 5. NULL Pointer 6.Are Pointers Useful? 7.Pointers and Allocation 8.What Does malloc Do? 9.Pointers and Deallocation 10.Function Arguments 11.Pass by Copy Example 11.Pass by Copy or Pass by Reference 12.Pass by Reference 13.Pass by Reference Example 14.The Address Operator & 15.Address Operator and scanf 16.Pass by Copy vs Pass by Reference #1 17.Pass by Copy vs Pass by Reference #2 18.Pass by Copy vs Pass by Reference #3 19.Pass by Reference Bad Example 20.Pass by Reference Good Example 21.Is Pass by Reference Really by Reference?

Pointer Lesson 1 CS1313 Spring A Pointer Experiment 1.Take out a sheet of scrap paper. 2.Tear it in half. 3.Tear it in half again. 4.On one of the quarter sheets, write legibly either: your full name (first and last), or an integer from 1 to Fold it in half. 6.Fold it in half again. 7.When the hat comes around the first time, put your quarter sheet of paper into it. 8.When the hat comes around the second time, take a random quarter sheet of paper out of it. If you draw your own name, take out another one and put your name back in.

Point! 9.Let’s pick someone. 10.Have them stand up and read their piece of paper, then stay standing. 11.If they read a name, that person should also stand up, and the person who read their name should point at them. 12.Let’s do several of those around the room. 13.So the people pointing at other people are “pointers,” and the people who have a number are “values.” Pointer Lesson 1 CS1313 Spring

Pointer Lesson 1 CS1313 Spring What is a Pointer? A pointer is a variable whose value is an address. int* int_pointer; This means: Grab a bunch of bytes in memory, name them int_pointer, and think of them as storing an address, which is a special kind of int. How many bytes? On most platforms that you can buy today, a pointer is 8 bytes.

Pointer Lesson 1 CS1313 Spring NULL Pointer A NULL pointer is a pointer that points to nowhere. int* int_pointer = (int*)NULL; This initialization statement means that the int pointer int_pointer should initially point to nowhere.

Pointer Lesson 1 CS1313 Spring Are Pointers Useful? We’ve already seen a context where pointers are useful: dynamic allocation of arrays. A dynamically allocated array is really just a pointer to the first byte of the first element of that array: float* project1_score = (float*)NULL; project1_score project1_score = (float*)malloc(sizeof(float) * number_of_students); project1_score

Pointer Lesson 1 CS1313 Spring Pointers and Allocation When you allocate an array project1_score = (float*)malloc(sizeof(float) * number_of_students); you’re setting a pointer variable’s value to the address of the first byte of the first element of the array.

Pointer Lesson 1 CS1313 Spring What Does malloc Do? The malloc function finds a block of memory that is otherwise not being used, claims it, and returns its address (that is, a pointer to the block’s first byte). project1_score = (float*)malloc(sizeof(float) * number_of_students); In this case, malloc finds an unclaimed block of sizeof(float) * number_of_students bytes, lays claim to that block, and returns its address to be assigned to project1_score (which is a pointer, which is to say its value is an address in main memory).

Pointer Lesson 1 CS1313 Spring Pointers and Deallocation When you deallocate an array free(project1_score); you’re releasing the block of memory that contains the array; that is, you’re no longer claiming it. But, that doesn’t change the value of the pointer variable. The pointer’s value is still the address of the block of memory – which no longer is the array. This is BAD BAD BAD! So, you have to assign NULL to the pointer IMMEDIATELY: project1_score = (float*)NULL;

Pointer Lesson 1 CS1313 Spring Function Arguments When you call a function in C and you pass it some arguments, those arguments are passed by copy. This means that the formal arguments in the function definition are actually copies of the actual arguments in the function call. They live at different addresses than the originals. Pass by copy is also known as: pass by value; call by copy; call by value.

Pointer Lesson 1 CS1313 Spring Pass by Copy Example % cat my_bad_increment.c #include int main () { /* main */ int x = 5; void my_increment(int var); printf("main: before call, x = %d\n", x); my_increment(x); printf("main: after call, x = %d\n", x); } /* main */ void my_increment (int var) { /* my_increment */ printf("my_increment: before inc, var = %d\n", var); var += 1; printf("my_increment: after inc, var = %d\n", var); } /* my_increment */ % gcc -o my_bad_increment my_bad_increment.c % my_bad_increment main: before call, x = 5 my_increment: before inc, var = 5 my_increment: after inc, var = 6 main: after call, x = 5

Pointer Lesson 1 CS1313 Spring Pass by Copy or Pass by Reference Okay, so pass by copy means that changing the value of the copy doesn’t change the value of the original. Is there a way to pass an argument so that, in the function, we can change the value of the formal argument, and that’ll change the value of the actual argument in the call? Yes: pass by reference.

Pointer Lesson 1 CS1313 Spring Pass by Reference Pass by reference means that, instead of passing a copy of the actual argument, you pass the address of the actual argument. If we can pass the address, then we can modify the value of the variable that lives at that address.

Pointer Lesson 1 CS1313 Spring Pass by Reference Example % cat my_good_increment.c #include int main () { /* main */ int x = 5; void my_increment(int* var); printf("main: before call, x = %d\n", x); my_increment(&x); printf("main: after call, x = %d\n", x); } /* main */ void my_increment (int* var) { /* my_increment */ printf("my_increment: before inc, *var = %d\n", *var); *var += 1; printf("my_increment: after inc, *var = %d\n", *var); } /* my_increment */ % gcc -o my_good_increment my_good_increment.c % my_good_increment main: before call, x = 5 my_increment: before inc, *var = 5 my_increment: after inc, *var = 6 main: after call, x = 6

Pointer Lesson 1 CS1313 Spring The Address Operator & The address operator & is an operator that means “the address of:” % cat addr_op.c #include int main () { /* main */ int* ip = (int*)NULL; int i; ip = &i; i = 5; printf("i=%d, *ip=%d\n", i, *ip); *ip = 6; printf("i=%d, *ip=%d\n", i, *ip); } /* main */ % gcc –o addr_op addr_op.c % addr_op i=5, *ip=5 i=6, *ip=6

Pointer Lesson 1 CS1313 Spring Address Operator and scanf We already know a case where we use the address operator: scanf. When we call scanf, we want to change the value of the argument(s) at the end of the call: scanf("%d", &number_of_elements); We want to modify the value of number_of_elements. So we have to pass the address of this variable, so that scanf can change its value.

Pointer Lesson 1 CS1313 Spring Pass by Copy vs Pass by Reference #1 In C, when an argument is passed to a function, the program grabs a new location in memory and copies the value of the actual argument into this new location, which is then used as the formal argument. This approach is known by several names: pass by value call by value pass by copy call by copy By contrast, if we use pointers – and possibly the address operator & in the actual argument(s) – then this accomplishes pass by reference (even though the pointer is passed by copy).

Pointer Lesson 1 CS1313 Spring Pass by Copy vs Pass by Reference #2 We can visualize pass by reference by imagining Henry’s house, which has the address 123 Any Street We can refer to Henry’s house this way: Henry’s house But we can also refer to Henry’s house this way: Dr. Neeman’s house So, “Henry’s house” and “Dr. Neeman’s house” are two different names for the same location; they are aliases.

Pointer Lesson 1 CS1313 Spring Pass by Copy vs Pass by Reference #3 We can refer to Henry’s house this way: Henry’s house But we can also refer to Henry’s house this way: Dr. Neeman’s house So, “Henry’s house” and “Dr. Neeman’s house” are aliases: two different names for the same location. With pass by reference, when we call a function, each actual argument and its corresponding formal argument are aliases of the same location in memory.

Pointer Lesson 1 CS1313 Spring Pass by Reference Bad Example % cat henrys_house_bad.c #include int main () { /* main */ int henrys_house; void who(int dr_neemans_house); who( henrys_house); printf("%d people live in Henry’s house.\n", henrys_house); } /* main */ void who (int dr_neemans_house) { /* who */ printf("How many people live in Dr Neeman’s house?\n"); scanf("%d", &dr_neemans_house); } /* who */ % gcc -o henrys_house_bad henrys_house_bad.c % henrys_house_bad How many people live in Dr Neeman's house? people live in Henry's house.

Pointer Lesson 1 CS1313 Spring Pass by Reference Good Example % cat henrys_house_good.c #include int main () { /* main */ int henrys_house; void who(int* dr_neemans_house); who(&henrys_house); printf("%d people live in Henry’s house.\n", henrys_house); } /* main */ void who (int* dr_neemans_house) { /* who */ printf("How many people live in Dr Neeman’s house?\n"); scanf("%d", dr_neemans_house); } /* who */ % gcc -o henrys_house_good henrys_house_good.c % henrys_house_good How many people live in Dr Neeman's house? 4 4 people live in Henry's house.

Pointer Lesson 1 CS1313 Spring Is Pass by Reference Really by Reference? In C, the only passing strategy is pass by copy. To pass by reference, we have to piggyback on top of pass by copy – because in C, everything is pass by copy. So, the value that we have to pass by copy is the address of the actual argument, which we achieve using the address operator &. In other words, in C pass by reference is actually pass by copy: you copy the address.