Pointers and Dynamic Memory Allocation

Size: px
Start display at page:

Download "Pointers and Dynamic Memory Allocation"

Transcription

1 Pointers and Dynamic Memory Allocation ALGORITHMS & DATA STRUCTURES 9 TH SEPTEMBER 2014

2 Last week Introduction This is not a course about programming: It s is about puzzling. well.. Donald Knuth Science is what we understand well enough to explain to a computer. Beware of bugs in the above code; I have only proved it correct, not tried it. 2

3 Algorithm Algorithm = Human (high-level) way of teaching a computer what to do to solve a problem An algorithm is a welldefined procedure which takes an input value and returns an output value 3

4 C++ Very powerful Bizarre syntax But this is NOT a C++ course. 4

5 Pointers Pointers Powerful feature of C++ One of the most difficult to master Essential for construction of interesting (recursive) data structures, e.g. lists and trees. 5

6 Addresses and Pointers owhen we declare a variable some memory is allocated for it. The memory location can be referenced through the identifier of the variable. othus, we have two properties for any variable : its address and its data value. othe address of the variable can be accessed through the referencing operator &. &i gives the memory location where the data value for i is stored oaddresses can be displayed, e.g., by using cout o Addresses displayed in HEXADECIMAL 6

7 Example #include <iostream.h> void main( ) { int data = 100; float value = 56.47; cout << data << &data << endl; cout << value << &value << endl; } value data FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF Output: 100 FFF FFF0 7

8 Pointer Variables The pointer data type Each address has a type, which depends on the (type of the) variable it belongs to. Suppose we have declare the variable int v; Then the type of v s address is int * pronounced as pointer-to-int or integer pointer We can also declare variables that have addresses as values. For example, a variable p holding an integer pointer is declared as int *p; 8

9 Declaration of Pointer Variables A pointer variable is declared by: datatype *pointervarname; The pointer variable pointervarname is used to point to a variable with a value of type datatype The * before the pointervarname indicates that this is a pointer variable, not a regular variable The * is not a part of the pointer variable name 9

10 Declaration of Pointer Variables (Cont..) Example int *ptr1; float *ptr2; ptr1 is a pointer to an int variable i.e., it can have the address of the memory location allocated to an int value ptr2 is a pointer to a float variable i.e., it can have the address of the memory location allocated to a float value 10

11 Declaration of Pointer Variables (Cont..) Whitespace doesn t matter and each of the following will declare ptr as a pointer (to a float) variable and data as a float variable float *ptr, data; float* ptr, data; float (*ptr), data; float data, *ptr; 11

12 Assignment of Pointer Variables A pointer variable has to be assigned a valid memory address (initialized) before it can be used in the program Example: float data = 50.8; float *ptr; ptr = &data; This will assign the address of the memory location allocated for the floating point variable data to the pointer variable ptr. This is OK, since the variable data has already been allocated some memory space having a valid address 12

13 Assignment of Pointer Variables (Cont..) FFF0 float data = 50.8; float *ptr; FFF1 FFF2 FFF3 ptr = &data; data FFF FFF5 FFF6 13

14 Assignment of Pointer Variables (Cont..) ptr FFF0 float data = 50.8; float *ptr; FFF1 FFF2 FFF3 ptr = &data; data FFF FFF5 FFF6 14

15 Assignment of Pointer Variables (Cont..) ptr FFF0 FFF4 FFF1 float data = 50.8; FFF2 float *ptr; FFF3 ptr = &data; data FFF FFF5 FFF6 15

16 Assignment of Pointer Variables (Cont..) Don t try to assign a specific integer value to a pointer variable since it can be disastrous float *ptr; ptr = 120; You cannot assign the address of one type of variable to a pointer variable of another type even though they are both integrals int data = 50; float *ptr; ptr = &data; 16

17 Initializing pointers A pointer can be initialized during declaration by assigning it the address of an existing variable float data = 50.8; float *ptr = &data; If a pointer is not initialized during declaration, it is wise to give it a NULL (or nullptr in C+11) value int *ip = 0; float *fp = nullptr; 17

18 The NULL pointer The NULL pointer is a valid address for any (pointer) data type. But NULL is not memory address 0. It is used to indicate that the pointer does not refer to a variable. It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer s job to check for this. 18

19 Dereferencing Dereferencing Using a pointer variable to access the value stored at the location pointed by the variable Provide indirect access to values and also called indirection Done by using the dereferencing operator * in front of a pointer variable Unary operator Highest precedence 19

20 Dereferencing (Cont..) Example: float data = 50.8; float *ptr; ptr = &data; cout << *ptr; Once the pointer variable ptr has been declared, *ptr represents the object pointed to by ptr (or the value located at the address specified by ptr) and may be treated like any other variable of float type 20

21 Dereferencing (Cont..) The dereferencing operator * can also be used in assignments. *ptr = 200; Make sure that ptr has been properly initialized 21

22 Dereferencing Example #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } ptr data FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF Output: 22

23 Dereferencing Example (Cont..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; }utput: ptr data FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF FFF

24 Dereferencing Example (Cont..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } ptr data FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF Output: 24

25 Dereferencing Example (Cont..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: 27.4 ptr data FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF

26 Dereferencing Example (Cont..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } ptr data FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF Output:

27 Operations on Pointer Variables Assignment the value of one pointer variable can be assigned to another pointer variable of the same type Relational operations - two pointer variables of the same type can be compared for equality, and so on Some limited arithmetic operations integer values can be added to and subtracted from a pointer variable value of one pointer variable can be subtracted from another pointer variable 27

28 const Pointer Variables Two possibilities 1. the variable itself is a constant the pointer value cannot be changed; int v = 42; int * const q = &v; 2. the object to which it refers is (treated as) a constant. int v = 42; const int *p = &v; 28

29 Pointers to arrays A pointer variable can be used to access the elements of an array of the same type. int gradelist[8] = {92,85,75,88,79,54,34,96}; int * mygrades = gradelist; cout << gradelist[1]; cout << *mygrades; cout << *(mygrades + 2); cout << mygrades[3]; Initially mygrades refers to the first element of gradelist. Note that the array name gradelist acts like the pointer variable mygrades. 29

30 Dynamic Memory Allocation 30

31 Types of Program Data Static Data: Memory allocation exists throughout execution of program Automatic Data: Automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function Dynamic Data: Explicitly allocated and deallocated during program execution by C++ instructions written by programmer 31

32 Allocation of Memory Static Allocation: Allocation of memory space at compile time. Dynamic Allocation: Allocation of memory space at run time. 32

33 Scope (revisited) Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. A local variable is limited in scope to all the code below the declaration until the end of the enclosing block. function arguments are local for-loop: scope is heading + body global variables are not limited in scope. Their visibility is throughout the entire program after declaration 33

34 Dynamic memory allocation Dynamic allocation is useful when arrays need to be created whose size is not known until run time complex structures of unknown size and/or shape need to be constructed as the program runs objects need to be created and the constructor arguments are not known until run time 34

35 Dynamic memory allocation Pointers need to be used for dynamic allocation of memory Use the operator new to dynamically allocate space Use the operator delete to later free this space 35

36 The new operator If memory is available, the new operator allocates memory space for the requested object/array, and returns a pointer to (address of) the memory allocated. If sufficient memory is not available, the new operator returns NULL. The dynamically allocated object/array exists until the delete operator destroys it. 36

37 The delete operator The delete operator deallocates the object or array currently pointed to by the pointer which was previously allocated at run-time by the new operator. the freed memory space is returned to Heap the pointer is then considered unassigned If the value of the pointer is NULL there is no effect. 37

38 Example int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 FDE1 FDE2 FDE3 0EC4 0EC5 0EC6 0EC7 38

39 Example (Cont..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr FDE0 FDE1 FDE2 FDE3 0EC4 ptr = NULL; 0EC4 0EC5 0EC6 0EC7 39

40 Example (Cont..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr FDE0 FDE1 FDE2 FDE3 0EC4 ptr = NULL; 0EC4 22 0EC5 0EC6 0EC7 40

41 Example (Cont..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr FDE0 FDE1 FDE2 FDE3 0EC4 ptr = NULL; Output: 22 0EC4 0EC5 0EC6 22 0EC7 41

42 Example (Cont..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr FDE0 FDE1 FDE2 FDE3? ptr = NULL; 0EC4 0EC5 0EC6 0EC7 42

43 Example (Cont..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr FDE0 FDE1 FDE2 FDE3 0 ptr = NULL; 0EC4 0EC5 0EC6 0EC7 43

44 Dynamic allocation and deallocation of arrays Use the new[intexp] to create an array of objects instead of a single instance. On the delete statement use [] to indicate that an array of objects is to be deallocated. 44

45 Example of dynamic array allocation int* grades = NULL; int numberofgrades; cout << "Enter the number of grades: "; cin >> numberofgrades; grades = new int[numberofgrades]; for (int i = 0; i < numberofgrades; i++) cin >> grades[i]; for (int j = 0; j < numberofgrades; j++) cout << grades[j] << " "; delete [] grades; grades = NULL; 45

46 Dynamic allocation of 2D arrays A two dimensional array is really an array of arrays (rows). To dynamically declare a two dimensional array of int type, you need to declare a pointer to a pointer as: int **matrix; 46

47 Dynamic allocation of 2D arrays (Cont..) To allocate space for the 2D array with r rows and c columns: You first allocate the array of pointers which will point to the arrays (rows) matrix = new int*[r]; This creates space for r addresses; each being a pointer to an int (array). Then you need to allocate the space for the 1D arrays themselves, each with a size of c for(i=0; i<r; i++) matrix[i] = new int[c]; 47

48 Dynamic allocation of 2D arrays (Cont..) The elements of the array matrix now can be accessed by the matrix[i][j] notation Keep in mind, the entire array is not in contiguous space (unlike a static 2D array) The elements of each row are in contiguous space, but the rows themselves are not. matrix[i][j+1] is after matrix[i][j] in memory, but matrix[i][0] may be before or after matrix[i+1][0] in memory 48

49 Example: Binomial Coefficients binomial coefficient: k n k n k n Pascal s Identity 1 0 n n n )!!(! n-k k n k n !(4)! 7! 3 7

50 Binomial coefficients in C++ int choose (int n, int k) { } if (k == 0 k == n) { return 1; } else { } return choose(n-1,k-1) + choose(n-1,k); Double recursion: (very) inefficient!

51 Efficient solution using arrays int **binomial; void PascalsTriangle (int N) { } binomial = new int *[N+1]; for (int n = 0; n <= N; n++) { } binomial[n] = new int [n+1]; binomial[n][0] = 1; binomial[n][n] = 1; for (int k = 1; k < n; k++) { } Dynamic programming binomial[n][k] = binomial[n-1][k-1] + binomial[n-1][k];

52 Passing pointers to a function 52

53 Pointers as arguments to functions Pointers can be passed to functions just like other types. Just as with any other argument, verify that the number and type of arguments in function invocation match the prototype (and function header). 53

54 Example of pointer arguments void Swap(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(&x,&y); // passes addresses of x and y explicitly cout << x << " " << y << endl; } 54

55 Example of reference arguments void Swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(x,y); // passes addresses of x and y implicitly cout << x << " " << y << endl; } References are like pointer constants with implicit (de)referencing. 55

56 More example void main () { int r, s = 5, t = 6; int *tp = &t; r = MyFunction(tp,s); r = MyFunction(&t,s); r = MyFunction(&s,*tp); } int MyFunction(int *p, int i) { *p = 3; i = 4; return i; } 56

57 Memory leaks and Dangling Pointers 57

58 Memory leaks When you dynamically create objects, you can access them through the pointer which is assigned by the new operator Reassigning a pointer without deleting the memory it pointed to previously is called a memory leak It results in loss of available memory space 58

59 Memory leak example int *ptr1 = new int; int *ptr2 = new int; *ptr1 = 8; *ptr2 = 5; ptr2 = ptr1; ptr1 ptr2 8 5 How to avoid? ptr1 ptr

60 Inaccessible object An inaccessible object is an unnamed object that was created by operator new and which a programmer has left without a pointer to it. It is a logical error and causes memory leaks. 60

61 Dangling Pointers Is a pointer that points to memory allocations that have been deallocated local objects whereof the scope has ended dynamically allocated objects that have been deleted explicitly 61

62 Dangling Pointer example int *answer ( ) { int result = 6*7; return &result; } void main ( ) { int *ptr = answer ( ); cout << " answer = " << *ptr; } 62

63 Dangling Pointer example 2 int *ptr1 = new int; int *ptr2; *ptr1 = 8; ptr2 = ptr1; delete ptr1; ptr1 ptr2 ptr1 8 How to avoid? ptr2 63

64 Pointers to objects

65 Pointers to (composite) objects Any type that can be used to declare a variable/object can also have a pointer type. Consider the following class: class Rational { private: int numerator; int denominator; public: Rational(int n, int d); void Display(); }; 65

66 Pointers to objects (Cont..) Rational *rp = NULL; Rational r(3,4); rp = &r; rp FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD 0 66

67 Pointers to objects (Cont..) Rational *rp = NULL; Rational r(3,4); rp = &r; numerator = 3 denominator = 4 r rp FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD

68 Pointers to objects (Cont..) Rational *rp = NULL; Rational r(3,4); rp = &r; numerator = 3 denominator = 4 r rp FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD FFF

69 Pointers to objects (Cont..) If rp is a pointer to an object, then two notations can be used to reference the instance/object rp points to. Using the de-referencing operator * (*rp).display(); Using the member access operator -> rp -> Display(); 69

70 Dynamic Allocation of a Class Object Consider the Rational class defined before Rational *rp; int a, b; cin >> a >> b; rp = new Rational(a,b); rp -> Display(); // (*rp).display(); delete rp; rp = NULL; 70

71 Next week: lists Questions? 71

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am Exam 1 on July 18, 2005 10:00-11:40am 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. This is the

More information

Pointers! Arizona State University 1

Pointers! Arizona State University 1 Pointers! CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 10 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018 Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory

More information

POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory

More information

CSC 211 Intermediate Programming. Arrays & Pointers

CSC 211 Intermediate Programming. Arrays & Pointers CSC 211 Intermediate Programming Arrays & Pointers 1 Definition An array a consecutive group of memory locations that all have the same name and the same type. To create an array we use a declaration statement.

More information

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int

More information

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int

More information

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011 March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

What is an algorithm?

What is an algorithm? Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address

More information

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c. Chapter 9: Pointers 9.1 Getting the Address of a Variable C++ Variables [ not in book ] A Variable has all of the following attributes: 1. name 2. type 3. size 4. value 5. storage class static or automatic

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation Lecture 15 COP 3014 Fall 2017 November 6, 2017 Allocating memory There are two ways that memory gets allocated for data storage: 1. Compile Time (or static) Allocation Memory

More information

Pointers and References

Pointers and References Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers

More information

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 10 Pointers and Dynamic Arrays Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic

More information

Exam 3 Chapters 7 & 9

Exam 3 Chapters 7 & 9 Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting

More information

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body Week 3 Pointers, References, Arrays & Structures Gaddis: Chapters 6, 7, 9, 11 CS 5301 Fall 2013 Jill Seaman 1 Arguments passed by value! Pass by value: when an argument is passed to a function, its value

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to. Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value

More information

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int

More information

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8 Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within

More information

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC CSCI 262 Data Structures 9 Dynamically Allocated Memory POINTERS AND ARRAYS 2 Arrays Arrays are just sequential chunks of memory: Arrays and Pointers Array variables are secretly pointers: x19 x18 x17

More information

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 10 Pointers and Dynamic Arrays 1 Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n Static and Dynamic Memory Allocation In this chapter we review the concepts of array and pointer and the use of the bracket operator for both arrays and pointers. We also review (or introduce) pointer

More information

CA31-1K DIS. Pointers. TA: You Lu

CA31-1K DIS. Pointers. TA: You Lu CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a

More information

Memory and C++ Pointers

Memory and C++ Pointers Memory and C++ Pointers C++ objects and memory C++ primitive types and memory Note: primitive types = int, long, float, double, char, January 2010 Greg Mori 2 // Java code // in function, f int arr[];

More information

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere

More information

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana POINTERS Programación de Computadores Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana 2018-01 Pointers A pointer is a reference to

More information

CS 161 Exam II Winter 2018 FORM 1

CS 161 Exam II Winter 2018 FORM 1 CS 161 Exam II Winter 2018 FORM 1 Please put your name and form number on the scantron. True (A)/False (B) (28 pts, 2 pts each) 1. The following array declaration is legal double scores[]={0.1,0.2,0.3;

More information

C10: Garbage Collection and Constructors

C10: Garbage Collection and Constructors CISC 3120 C10: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/5/2018 CUNY Brooklyn College 1 Outline Recap OOP in Java: composition &

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.. COMP6771 Advanced C++ Programming Week 5 Part Two: Dynamic Memory Management 2016 www.cse.unsw.edu.au/ cs6771 2.. Revisited 1 #include 2 3 struct X { 4 X() { std::cout

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer September 26, 2016 OOPP / C++ Lecture 4... 1/33 Global vs. Class Static Parameters Move Semantics OOPP / C++ Lecture 4... 2/33 Global Functions

More information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

More information

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information

Variation of Pointers

Variation of Pointers Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]

More information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Computer Science Midterm 3 Fall, Points Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do

More information

Propedéutico de Programación

Propedéutico de Programación Propedéutico de Programación Coordinación de Ciencias Computacionales 5/13 Material preparado por: Dra. Pilar Gómez Gil Chapter 15 Pointers, Dynamic Data, and Reference Types Dale/Weems Recall that in

More information

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

double d0, d1, d2, d3; double * dp = new double[4]; double da[4]; All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

FORM 2 (Please put your name and form # on the scantron!!!!)

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

C11: Garbage Collection and Constructors

C11: Garbage Collection and Constructors CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1 Outline Recap Project progress and lessons

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

UEE1302 (1102) F10 Introduction to Computers and Programming (I) Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation Why use Pointers? Share access to common data (hold onto one copy, everybody

More information

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 5 September 15, 2011 CPSC 427a, Lecture 5 1/35 Functions and Methods Parameters Choosing Parameter Types The Implicit Argument Simple Variables

More information

Dynamic Allocation of Memory

Dynamic Allocation of Memory Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013

More information

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer: Chapter-11 POINTERS Introduction: Pointers are a powerful concept in C++ and have the following advantages. i. It is possible to write efficient programs. ii. Memory is utilized properly. iii. Dynamically

More information

Scope. Scope is such an important thing that we ll review what we know about scope now:

Scope. Scope is such an important thing that we ll review what we know about scope now: Scope Scope is such an important thing that we ll review what we know about scope now: Local (block) scope: A name declared within a block is accessible only within that block and blocks enclosed by it,

More information

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS CSE 100: C++ TEMPLATES AND ITERATORS Announcements iclickers: Please register at ted.ucsd.edu. Start ASAP!! For PA1 (Due next week). 10/6 grading and 10/8 regrading How is Assignment 1 going? A. I haven

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

Pointers and Terminal Control

Pointers and Terminal Control Division of Mathematics and Computer Science Maryville College Outline 1 2 3 Outline 1 2 3 A Primer on Computer Memory Memory is a large list. Typically, each BYTE of memory has an address. Memory can

More information

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Using pointers Pointers are polymorphic Pointer this Using const with pointers Stack and Heap Memory leaks and dangling pointers

More information

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or Ch 9. Pointers CS 2308 Spring 2014 Jill Seaman 1 A Quote A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

What have we learned about when we learned about function parameters? 1-1

What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? Call-by-Value also known as scalars (eg. int, double, char,

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017 Pointer Arithmetic Lecture 4 Chapter 10 Robb T. Koether Hampden-Sydney College Wed, Jan 25, 2017 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 1 / 36 1 Pointer Arithmetic

More information

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another Week 4 Pointers & Structs Gaddis: Chapters 9, 11 CS 5301 Spring 2015 Jill Seaman 1 Pointers and Addresses! The address operator (&) returns the address of a variable. int x; cout

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 ARRAYS ~ VECTORS KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15 C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15 This chapter introduces the notion of dynamic memory allocation of variables and objects in a C++ program.

More information

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES [1] ~~~~~~~~~~~~~~~~~ POINTER A pointers is a variable that holds a memory address, usually the location of another variable in memory. IMPORTANT FEATURES OF POINTERS (1) provide the means through which

More information

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2 Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

More information

C Pointers. 6th April 2017 Giulio Picierro

C Pointers. 6th April 2017 Giulio Picierro C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword

More information

For Teacher's Use Only Q No Total Q No Q No

For Teacher's Use Only Q No Total Q No Q No Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9

More information

Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1.

Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1. Lecture 9 Pointer, linked node, linked list TDDD86: DALP Utskriftsversion av Lecture in Data Structures, Algorithms and Programming Paradigms 22nd September 2017 Ahmed Rezine, IDA, Linköping University

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Launchpad Lecture -10

Launchpad Lecture -10 Saturday, 24 September Launchpad Lecture -10 Dynamic Allocation, Object Oriented Programming-1 Prateek Narang 2 Recap 1. How to define pointers? 2. Address of Operator? 3. Dereference operator? 4. Arithmetic

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 8 September 26, 2016 CPSC 427, Lecture 8 1/23 Storage Management (continued) Bar Graph Demo CPSC 427, Lecture 8 2/23 Storage Management

More information

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1 Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

Variables, Memory and Pointers

Variables, Memory and Pointers Variables, Memory and Pointers A variable is a named piece of memory The name stands in for the memory address int num; Variables, Memory and Pointers When a value is assigned to a variable, it is stored

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites

More information

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

More information

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

COMP26120: Pointers in C (2018/19) Lucas Cordeiro COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11

More information

Constants, References

Constants, References CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const

More information

Memory Allocation in C

Memory Allocation in C Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called

More information

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. CHAPTER 08 POINTERS What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. The Pointers are one of the C++ s most useful and powerful features. How Pointers

More information

Introducing C++ to Java Programmers

Introducing C++ to Java Programmers Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003 1 Philosophy of C++ Bjarne Stroustrup invented C++ in the early 1980's at Bell Laboratories First called "C with classes" Design Goals:

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information