0% found this document useful (0 votes)
2 views

Lec19_pointers_part3

Chapter 9 discusses pointers and dynamic memory allocation in C++, focusing on C-Strings and arrays. It explains how to pass arrays to functions as pointers and return pointers from functions, emphasizing the importance of not returning pointers to local variables. Additionally, it covers dynamically allocating and freeing memory for 2D arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lec19_pointers_part3

Chapter 9 discusses pointers and dynamic memory allocation in C++, focusing on C-Strings and arrays. It explains how to pass arrays to functions as pointers and return pointers from functions, emphasizing the importance of not returning pointers to local variables. Additionally, it covers dynamically allocating and freeing memory for 2D arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 9:

Pointers

Copyright © 2012 Pearson Education, Inc.


Dynamic array of characters
• C-String is an array of characters
• We used to define it like this:
char str[20];
• This is static memory allocation.
• We can allocate memory dynamically for a
C-String like this:
char * str;
str = new char[20];

Copyright © 2012 Pearson Education, Inc.


Dynamic array of characters
• You can deal with C-String later as you
normally do.
• To read C-String:
cin>>str;
or cin.getline(str,20);
• To print C-String:
cout<<str;

Copyright © 2012 Pearson Education, Inc.


Passing an array as a pointer to
functions
• We learned that arrays are automatically
passed by reference to functions which
makes any changes to array in the
function reflect on the actual array
parameter.

void add1ToArray(int grades[],int size)


{
for(int i=0;i<size;i++)
grades[i]++;
}
Copyright © 2012 Pearson Education, Inc.
Passing an array as a pointer to
functions
• We can also pass arrays to functions as
pointers and still any changes to array in
the function reflect on the actual array
parameter.

void add1ToArray(int * grades,int size)


{
for(int i=0;i<size;i++)
grades[i]++;
}

Copyright © 2012 Pearson Education, Inc.


Passing an array as a pointer to
functions
• Whether you pass arrays by reference or
as a pointer this doesn’t affect any code in
the function or the function call.
• The function call for both of the functions
add1ToArray will be:
int main()
{
int g[]={5,7,9,11,13};
add1ToArray(g, 5);
return 0;
}
Copyright © 2012 Pearson Education, Inc.
9.9
Returning Pointers from Functions

Copyright © 2012 Pearson Education, Inc.


Returning Pointers from
Functions
• Pointer can be the return type of a function:
int* newNum();
• The function must not return a pointer to a local
variable in the function.

• A function should only return a pointer:


– to data that was passed to the function as a
pointer/reference argument, or
– to dynamically allocated memory

Copyright © 2012 Pearson Education, Inc.


Returning Pointers from
Functions
• Don’t do that!!
int* f()
{
int num=23;
int* p = &num; //points to local variable
return p;
}
• This causes no problems
int* f()
{
int* p = new int;//dynamically allocated memory
*p = 23;
return p;
}
Copyright © 2012 Pearson Education, Inc.
Returning Pointers from
Functions
• This causes no problems
int* f(int * param)
{
int* p = param;//copy pointer parameter
*p = 23;
return p;
}
• This causes no problems
int* f(int & param)
{
int* p = &param;//copy address of reference
//parameter
*p = 23;
return p;
} Copyright © 2012 Pearson Education, Inc.
Returning array as pointer
From Program 9-15

Copyright © 2012 Pearson Education, Inc.


Allocating 2D Arrays
Dynamically
• A 2D array can be normally defined as follows:
int matrix[3][4]; //static memory
//allocation
• To dynamically allocate a 2D array, you need to
define a double pointer:
int** matrix;

• Next you need to allocate the first dimension


which is the number of rows:
matrix = new int*[3];
This allocated 3 pointers to integers.

Copyright © 2012 Pearson Education, Inc.


Allocating 2D Arrays
Dynamically
• Then finally using each of the pointers, you
should allocate memory with the size of the
columns.
for(int i=0;i<3;i++)
matrix[i] = new int[4]

0 1 2 3
matrix[0] 0
matrix[1] 1
matrix[2] 2

Copyright © 2012 Pearson Education, Inc.


Allocating 2D Arrays
Dynamically

Copyright © 2012 Pearson Education, Inc.


Passing 2D Arrays to functions
using pointers

Copyright © 2012 Pearson Education, Inc.


Freeing memory for dynamically
allocated 2D arrays
• For each row you need to delete that row’s
elements using its pointer.
for(int i=0;i<rows;i++)
delete[] matrix[i];
0 1 2 3
• But you also matrix[0] 0
need to reset matrix[1] 1
each pointer to 0. matrix[2] 2

for(int i=0;i<rows;i++)
{
delete[] matrix[i];
matrix[i] = 0;
}
Copyright © 2012 Pearson Education, Inc.

You might also like