This document discusses pointers in C++. It begins by explaining that pointers store memory addresses and can be used to indirectly access the value of the variable located at that address. It then covers pointer declaration syntax, using the address of (&) and dereference (*) operators, and pointers to pointers. The document also discusses static vs dynamic memory allocation, using new and delete to manually allocate and free memory. It provides examples of pointers to arrays and array pointers. Finally, it shows examples of dynamically allocating memory for arrays using new and deleting it using delete[].
I prepared this slides for the student of FSC BSC BS Computer Science Students. These Slides are very easy to read and understand the pointer logic used in C++ Programming.
All Topic related to pointer is discussed and examples are given
I prepared this slides for the student of FSC BSC BS Computer Science Students. These Slides are very easy to read and understand the pointer logic used in C++ Programming.
All Topic related to pointer is discussed and examples are given
A class that automates conversion from a C++ recursive function to an iterative function. It allow the recursive function to preserve its structure by reproducing the "call stack" on an std::stack. The examples use combinatorics to illustrate usage.
This is the last slide of advancedC - Advanced C part 3. In the previous slides we learnt all the fundamentals that is required to learnt Advanced C. In this last slide of Advanced C you will be learning about Multilevel pointers, Command line argument, different kinds of functions, and also you will gain deep knowledge on pre processor and user defined data types. This will help you to improve your knowledge in Advanced C
This is continuation of the slide Advanced C part 1. In part 1 you learnt about fundamentals of C - How to build an algorithm, operators. In this module - Advanced C part 2 you will be learning about functions, pointers and standard Input Output functions. This slide will help you to move a further ahead in Advanced C and gain deeper knowledge on it.
02 of 03 parts
Get Part 1 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-1
Get Part 3 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-3
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to PLCs and embedded system.
03 of 03 parts
Get Part 1 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-1
Get Part 2 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-2
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to PLCs and embedded system.
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
C++ programming language notes for beginners and Collage students. Written for beginners. Colored graphics. Function by Function explanation with complete examples. Well commented examples. Illustrations are made available for data dealing at memory level.
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
C programming language notes for beginners and Collage students. Written for beginners. Colored graphics. Function by Function explanation with complete examples. Well commented examples. Illustrations are made available for data dealing at memory level.
A class that automates conversion from a C++ recursive function to an iterative function. It allow the recursive function to preserve its structure by reproducing the "call stack" on an std::stack. The examples use combinatorics to illustrate usage.
This is the last slide of advancedC - Advanced C part 3. In the previous slides we learnt all the fundamentals that is required to learnt Advanced C. In this last slide of Advanced C you will be learning about Multilevel pointers, Command line argument, different kinds of functions, and also you will gain deep knowledge on pre processor and user defined data types. This will help you to improve your knowledge in Advanced C
This is continuation of the slide Advanced C part 1. In part 1 you learnt about fundamentals of C - How to build an algorithm, operators. In this module - Advanced C part 2 you will be learning about functions, pointers and standard Input Output functions. This slide will help you to move a further ahead in Advanced C and gain deeper knowledge on it.
02 of 03 parts
Get Part 1 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-1
Get Part 3 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-3
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to PLCs and embedded system.
03 of 03 parts
Get Part 1 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-1
Get Part 2 from https://www.slideshare.net/ArunUmrao/notes-for-c-programming-for-bca-mca-b-sc-msc-be-amp-btech-1st-year-2
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to PLCs and embedded system.
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
C++ programming language notes for beginners and Collage students. Written for beginners. Colored graphics. Function by Function explanation with complete examples. Well commented examples. Illustrations are made available for data dealing at memory level.
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
C programming language notes for beginners and Collage students. Written for beginners. Colored graphics. Function by Function explanation with complete examples. Well commented examples. Illustrations are made available for data dealing at memory level.
Cyber security refers to every aspect of protecting an organization and its employees and assets against cyber threats. As cyberattacks become more common and sophisticated and corporate networks grow more complex, a variety of cyber security solutions are required to mitigate corporate cyber risk.
Honest Reviews of Tim Han LMA Course Program.pptxtimhan337
Personal development courses are widely available today, with each one promising life-changing outcomes. Tim Han’s Life Mastery Achievers (LMA) Course has drawn a lot of interest. In addition to offering my frank assessment of Success Insider’s LMA Course, this piece examines the course’s effects via a variety of Tim Han LMA course reviews and Success Insider comments.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Normal Labour/ Stages of Labour/ Mechanism of LabourWasim Ak
Normal labor is also termed spontaneous labor, defined as the natural physiological process through which the fetus, placenta, and membranes are expelled from the uterus through the birth canal at term (37 to 42 weeks
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...Levi Shapiro
Letter from the Congress of the United States regarding Anti-Semitism sent June 3rd to MIT President Sally Kornbluth, MIT Corp Chair, Mark Gorenberg
Dear Dr. Kornbluth and Mr. Gorenberg,
The US House of Representatives is deeply concerned by ongoing and pervasive acts of antisemitic
harassment and intimidation at the Massachusetts Institute of Technology (MIT). Failing to act decisively to ensure a safe learning environment for all students would be a grave dereliction of your responsibilities as President of MIT and Chair of the MIT Corporation.
This Congress will not stand idly by and allow an environment hostile to Jewish students to persist. The House believes that your institution is in violation of Title VI of the Civil Rights Act, and the inability or
unwillingness to rectify this violation through action requires accountability.
Postsecondary education is a unique opportunity for students to learn and have their ideas and beliefs challenged. However, universities receiving hundreds of millions of federal funds annually have denied
students that opportunity and have been hijacked to become venues for the promotion of terrorism, antisemitic harassment and intimidation, unlawful encampments, and in some cases, assaults and riots.
The House of Representatives will not countenance the use of federal funds to indoctrinate students into hateful, antisemitic, anti-American supporters of terrorism. Investigations into campus antisemitism by the Committee on Education and the Workforce and the Committee on Ways and Means have been expanded into a Congress-wide probe across all relevant jurisdictions to address this national crisis. The undersigned Committees will conduct oversight into the use of federal funds at MIT and its learning environment under authorities granted to each Committee.
• The Committee on Education and the Workforce has been investigating your institution since December 7, 2023. The Committee has broad jurisdiction over postsecondary education, including its compliance with Title VI of the Civil Rights Act, campus safety concerns over disruptions to the learning environment, and the awarding of federal student aid under the Higher Education Act.
• The Committee on Oversight and Accountability is investigating the sources of funding and other support flowing to groups espousing pro-Hamas propaganda and engaged in antisemitic harassment and intimidation of students. The Committee on Oversight and Accountability is the principal oversight committee of the US House of Representatives and has broad authority to investigate “any matter” at “any time” under House Rule X.
• The Committee on Ways and Means has been investigating several universities since November 15, 2023, when the Committee held a hearing entitled From Ivory Towers to Dark Corners: Investigating the Nexus Between Antisemitism, Tax-Exempt Universities, and Terror Financing. The Committee followed the hearing with letters to those institutions on January 10, 202
Acetabularia Information For Class 9 .docxvaibhavrinwa19
Acetabularia acetabulum is a single-celled green alga that in its vegetative state is morphologically differentiated into a basal rhizoid and an axially elongated stalk, which bears whorls of branching hairs. The single diploid nucleus resides in the rhizoid.
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Synthetic fiber production is a fascinating and complex field that blends chemistry, engineering, and environmental science. By understanding these aspects, students can gain a comprehensive view of synthetic fiber production, its impact on society and the environment, and the potential for future innovations. Synthetic fibers play a crucial role in modern society, impacting various aspects of daily life, industry, and the environment. ynthetic fibers are integral to modern life, offering a range of benefits from cost-effectiveness and versatility to innovative applications and performance characteristics. While they pose environmental challenges, ongoing research and development aim to create more sustainable and eco-friendly alternatives. Understanding the importance of synthetic fibers helps in appreciating their role in the economy, industry, and daily life, while also emphasizing the need for sustainable practices and innovation.
2. Topics
• Pointers
– Memory addresses
– Declaration
– Dereferencing a pointer
– Pointers to pointer
• Static vs. dynamic objects
– new and delete
3. Computer Memory
• Each variable is assigned a memory slot
(the size depends on the data type) and
the variable’s data is stored there
Variable a’s value, i.e., 100, is
stored at memory location 1024
100… … 1024 …
Memory address:
1024 1032
int a = 100;
…
1020
a
4. Pointers
• A pointer is a variable used to store the
address of a memory cell.
• We can use the pointer to reference this
memory cell
100… … 1024 …
Memory address: 1024 1032
…
1020
integer
pointer
5. Pointer Types
• Pointer
– C++ has pointer types for each type of object
• Pointers to int objects
• Pointers to char objects
• Pointers to user-defined objects
(e.g., RationalNumber)
– Even pointers to pointers
• Pointers to pointers to int objects
6. Pointer Variable
• Declaration of Pointer variables
type* pointer_name;
//or
type *pointer_name;
where type is the type of data pointed to (e.g. int, char, double)
Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer
7. Address Operator &
• The "address of " operator (&) gives the memory
address of the variable
– Usage: &variable_name
100… … … …
Memory address: 1024
int a = 100;
//get the value,
cout << a; //prints 100
//get the memory address
cout << &a; //prints 1024
…
1020
a
8. Address Operator &
10088 … … …
Memory address: 1024 1032
a
…
1020
b
#include <iostream>
using namespace std;
void main(){
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
Result is:
The address of a is: 1020
The address of b is: 1024
9. Pointer Variables
• The value of pointer p is the address of variable a
• A pointer is also a variable, so it has its own memory
address
10088 … 1024 …
Memory address: 1024 1032
…
1020
a p
int a = 100;
int *p = &a;
cout << a << " " << &a <<endl;
cout << p << " " << &p <<endl;
Result is:
100 1024
1024 1032
11. Dereferencing Operator *
• We can access to the value stored in the variable pointed to
by using the dereferencing operator (*),
10088 … 1024 …
Memory address: 1024 1032
…
1020
int a = 100;
int *p = &a;
cout << a << endl;
cout << &a << endl;
cout << p << " " << *p << endl;
cout << &p << endl;
Result is:
100
1024
1024 100
1032
a p
12. Don’t get confused
• Declaring a pointer means only that it is a pointer: int *p;
• Don’t be confused with the dereferencing operator, which is
also written with an asterisk (*). They are simply two
different tasks represented with the same sign
int a = 100, b = 88, c = 8;
int *p1 = &a, *p2, *p3 = &c;
p2 = &b; // p2 points to b
p2 = p1; // p2 points to a
b = *p3; //assign c to b
*p2 = *p3; //assign c to a
cout << a << b << c;
Result is:
888
13. A Pointer Example
The code
void doubleIt(int x,
int * p)
{
*p = 2 * x;
}
int main(int argc, const
char * argv[])
{
int a = 16;
doubleIt(9, &a);
return 0;
}
Box diagram
Memory Layout
9x
p
(8200)
x
(8196)
16a
main
doubleIt
p
a
(8192)
16
9
8192
main
doubleIt
a gets 18
14. Another Pointer Example
#include <iostream>
using namespace std;
int main (){
int value1 = 5, value2 = 15;
int *p1, *p2;
p1 = &value1; // p1 = address of value1
p2 = &value2; // p2 = address of value2
*p1 = 10; // value pointed to by p1=10
*p2 = *p1; // value pointed to by p2=
value // pointed to by p1
p1 = p2; // p1 = p2 (pointer value
copied)
*p1 = 20; // value pointed to by p1 = 20
cout << "value1==" << value1 << "/ value2=="
<< value2;
return 0;
Let’s figure out:
value1==? / value2==?
Also, p1=? p2=?
15. Another Pointer Example
int a = 3;
char s = ‘z’;
double d = 1.03;
int *pa = &a;
char *ps = &s;
double *pd = &d;
% sizeof returns the # of bytes…
cout << sizeof(pa) << sizeof(*pa)
<< sizeof(&pa) << endl;
cout << sizeof(ps) << sizeof(*ps)
<< sizeof(&ps) << endl;
cout << sizeof(pd) << sizeof(*pd)
<< sizeof(&pd) << endl;
16. Reference Variables
A reference is an additional name to
an existing memory location
9x
ref
Pointer:
9
x
ref
Reference:
int x=9;
int *ref;
ref = &x;
int x = 9;
int &ref = x;
17. Reference Variables
• A reference variable serves as an alternative name for an
object
int m = 10;
int &j = m; // j is a reference variable
cout << “value of m = “ << m << endl;
//print 10
j = 18;
cout << “value of m = “ << m << endl;
// print 18
18. Reference Variables
• A reference variable always refers to the
same object. Assigning a reference variable
with a new value actually changes the value
of the referred object.
• Reference variables are commonly used for
parameter passing to a function
19. Traditional Pointer Usage
void IndirectSwap(char *Ptr1, char
*Ptr2){
char temp = *Ptr1;
*Ptr1 = *Ptr2;
*Ptr2 = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(&a, &b);
cout << a << b << endl;
return 0;
}
20. Pass by Reference
void IndirectSwap(char& y, char& z) {
char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(a, b);
cout << a << b << endl;
return 0;
}
21. Pointers and Arrays
The name of an array points only to the first
element not the whole array.
1000
1012
1016
1004
1008
22. Array Name is a pointer constant
#include <iostream>
using namespace std;
void main (){
int a[5];
cout << "Address of a[0]: " << &a[0] << endl
<< "Name as pointer: " << a << endl;
}
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
23. Dereferencing An Array Name
#include <iostream>
using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
cout << *a << " "
<< a[0];
} //main
2
4
8
6
22a[4]
a[0]
a[2]
a[1]
a[3]
a
a
This element is
called a[0] or
*a
24. Array Names as Pointers
To access an array, any pointer to the first element
can be used instead of the name of the array.
We could replace *p by *a
2 2
#include <iostream>
using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
int *p = a;
cout << a[0] << " "
<< *p;
}
2
4
8
6
22a[4]
a[0]
a[2]
a[1]
a[3]
a p
a
25. Multiple Array Pointers
Both a and p are pointers to the same array.
2 2
4 4
#include <iostream>
using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
int *p = &a[1];
cout << a[0] << " "
<< p[-1];
cout << a[1] << " "
<< p[0];
}
2
4
8
6
22a[4]
a[0]
a[2]
a[1]
a[3]
p
p[0]
a[0]
26. Pointer Arithmetic
Given a pointer p, p+n refers to the element
that is offset from p by n positions.
2
4
8
6
22
a
a + 2
a + 4
a + 3
a + 1 p
p + 2
p + 3
p - 1
p + 1
27. *(a+n) is identical to a[n]
Dereferencing Array Pointers
2
4
8
6
22
a
a + 2
a + 4
a + 3
a + 1
a[3] or *(a + 3)
a[2] or *(a + 2)
a[1] or *(a + 1)
a[0] or *(a + 0)
a[4] or *(a + 4)
Note: flexible pointer syntax
28. Array of Pointers & Pointers to Array
a
b
c
An array of Pointers
p
int a = 1, b = 2, c = 3;
int *p[5];
p[0] = &a;
p[1] = &b;
p[2] = &c;
int list[5] = {9, 8, 7, 6, 5};
int *p;
P = list;//points to 1st entry
P = &list[0];//points to 1st entry
P = &list[1];//points to 2nd entry
P = list + 1; //points to 2nd entry
A pointer to an array
29. NULL pointer
• NULL is a special value that indicates an empty pointer
• If you try to access a NULL pointer, you will get an error
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address
of p
cout << *p << endl;//Error!
30. Storing 2D Array in 1D Array
int twod[3][4] = {{0,1,2,3}, {4,5,6,7},
{8,9,10,11}};
int oned[12];
for(int i=0; i<3; i++){
for(int j=0; j<4 ; j++)
oned[i*4+j] = twod[i][j];
}
31. Pointer to 2-Dimensional Arrays
table[ 0] or *( table + 0 )
table
table[ 1] or *( table + 1 )
table + 1
table[ 2] or *( table + 2 )
table + 2
int table[3][4] = {{1,2,3,4},
{5,6,7,8},{9,10,11,12}};
for(int i=0; i<3; i++){
for(int j=0; j<4; j++)
cout << *(*(table+i)+j);
cout << endl;
}
*(table[i]+j)
= table[i][j]What is
**table
?
table[i] =
&table[i][0]
refers to
the address
of the ith
row
33. Memory Management
• Static Memory Allocation
– Memory is allocated at compilation time
• Dynamic Memory
– Memory is allocated at running time
34. Static vs. Dynamic Objects
• Static object
(variables as declared in function calls)
– Memory is acquired
automatically
– Memory is returned
automatically when
object goes out of scope
• Dynamic object
– Memory is acquired by
program with an allocation
request
• new operation
– Dynamic objects can exist
beyond the function in
which they were allocated
– Object memory is returned
by a deallocation request
• delete operation
36. Object (variable) creation: New
Syntax
ptr = new SomeType;
where ptr is a pointer of type SomeType
p
Uninitialized int variable
Example
int* p = new int;
37. Object (variable) destruction: Delete
Syntax
delete p;
storage pointed to by p is returned to free store and p is now
undefined
p
Example
int* p = new
int;
*p = 10;
delete p;
10
38. Array of New:
dynamic arrays
• Syntax
P = new
SomeType[Expression];
– Where
• P is a pointer of type SomeType
• Expression is the number of objects to be
constructed -- we are making an array
• Because of the flexible pointer syntax, P can
be considered to be an array
39. Example
Dynamic Memory Allocation
n Request for “unnamed” memory from the Operating System
n int *p, n=10;
p = new int;
p = new int[100];
p
new
p
new
p = new int[n]; p
new
40. Memory Allocation Example
Want an array of unknown size
#include <iostream>
using namespace std;
void main()
{
int n;
cout << “How many students? “;
cin >> n;
int *grades = new int[n];
for(int i=0; i < n; i++){
int mark;
cout << “Input Grade for Student” << (i+1) <<
“ ? :”;
cin >> mark;
grades[i] = mark;
}
. . .
printMean( grades, n ); // call a function with
dynamic array
42. A Simple Dynamic List Example
cout << "Enter list size: ";
int n;
cin >> n;
int *A = new int[n];
if(n<=0){
cout << "bad size" << endl;
return 0;
}
initialize(A, n, 0); // initialize the array A with value 0
print(A, n);
A = addElement(A,n,5); //add an element of value 5 at the
end of A
print(A, n);
A = deleteFirst(A,n); // delete the first element from A
print(A, n);
selectionSort(A, n); // sort the array (not shown)
print(A, n);
delete [] A;
44. print()
void print(int list[], int size) {
cout << "[ ";
for(int i=0; i<size; i++)
cout << list[i] << " ";
cout << "]" << endl;
}
Remember in C++, array parameters are always passed
by reference. That is, void print(int list[], int size) {…} is the
same as
void print(int * list , int size) {…}
Note: no & used here, so, the pointer itself is passed by
value
45. Adding Elements
// for adding a new element to end of array
int* addElement(int list[], int& size, int value){
int* newList = new int [size+1]; // make new array
if(newList==0){
cout << "Memory allocation error for addElement!" <<
endl;
exit(-1);
}
for(int i=0; i<size; i++)
newList[i] = list[i];
if(size) delete [] list;
newList[size] = value;
size++;
return newList;
}
46. Delete the first element
// for deleting the first element of the array
int* deleteFirst(int list[], int& size){
if(size <= 1){
if( size) delete list;
size = 0;
return NULL;
}
int* newList = new int [size-1]; // make new array
if(newList==0){
cout << "Memory allocation error for deleteFirst!" <<
endl;
exit(-1);
}
for(int i=0; i<size-1; i++) // copy and delete old
array
newList[i] = list[i+1];
delete [] list;
size--;
return newList;
}
47. Adding Element (version 2)
// for adding a new element to end of array
// here “list” is a reference to a pointer variable: if the
value of the pointer is changed in function, the change is
global.
void addElement( int * & list, int & size, const int value ){
int * newList = new int [size + 1];
if( newList == NULL ){
cout << "Memory allocation error for addElement!" << endl;
exit(-1);
}
for( int i = 0; i < size; i++ )
newList[ i ] = list[ i ];
if( size ) delete [] list;
newList[ size ] = value;
size++;
list = newList;
return;
}
48. Deleting Element (version 2)
void deleteFirst( int * & list, int & size ){
if( size <= 1 ){
if( size )
delete list;
list = NULL;
size = 0;
return;
}
delete list; // delete the first element
list++;
size--;
return;
}
49. Another Main program
int main(){
int * A = NULL;
int size = 0;
int i;
for( i = 0; i < 10; i++ )
addElement( A, size, i );
for( i = 0; i < 10; i++ )
cout << A[i] << " ";
cout << endl;
for( i = 0; i < 4; i++ )
deleteFirst( A, size );
for( i = 0; i < 6; i++ )
cout << A[i] << " ";
cout << endl;
return 0;
}
0 1 2 3 4 5 6 7 8 9
4 5 6 7 8 9
50. Dangling Pointer Problem
int *A = new int[5];
for(int i=0; i<5; i++)
A[i] = i;
int *B = A;
delete [] A;
B[0] = 1; // illegal!
A
B
0 1 2 3 4
A
B
Locations do not belong to program
—
?
51. Memory Leak Problem
int *A = new int [5];
for(int i=0; i<5; i++)
A[i] = i;
A = new int [5];
A 0 1 2 3 4
— — — — —
These locations cannot be
accessed by program
A 0 1 2 3 42
52. A Dynamic 2D Array
A dynamic array is
an array of
pointers to save
space when not all
rows of the array
are full.
int **table;
32 18 2412
42 141912161113
1811
13 1413
22
table = new int*[6];
…
table[0] = new int[4];
table[1] = new int[7];
table[2] = new int[1];
table[3] = new int[3];
table[4] = new int[2];
table[5] = NULL;
table[0]
table[1]
table[2]
table[3]
table[4]
table[5]
table
53. Memory Allocation
int **table;
table = new int*[6];
table[0]= new int[3];
table[1]= new int[1];
table[2]= new int[5];
table[3]= new int[10];
table[4]= new int[2];
table[5]= new int[6];
table[0][0] = 1; table[0][1] = 2; table[0][2] = 3;
table[1][0] = 4;
table[2][0] = 5; table[2][1] = 6; table[2][2] = 7;
table[2][3] = 8; table[2][4] = 9;
table[4][0] = 10; table[4][1] = 11;
cout << table[2][5] << endl;
54. Memory Deallocation
• Memory leak is a serious bug!
• Each row must be deleted individually
• Be careful to delete each row before
deleting the table pointer.
– for(int i=0; i<6; i++)
delete [ ] table[i];
delete [ ] table;
55. int m, n;
cin >> m >> n >>
endl;
int** mat;
mat = new int*[m];
for (int
i=0;i<m;i++)
mat[i] = new
int[n];
Create a matrix of any dimensions, m by n:
int m, n;
cin >> m >> n >> endl;
int** mat;
mat = imatrix(m,n);
…
int** imatrix(nr, nc) {
int** m;
m = new int*[nr];
for (int i=0;i<nr;i++)
m[i] = new int[nc];
return m;
}
Put it into a function: