Deck 10: Pointers and Dynamic Arrays

Full screen (f)
exit full mode
Question
Given the definitions,
int *p1,*p2;
p1 = new int;
p2 = new int;
You are to compare and contrast the two assignments.
a)p1 = p2;
b)*p1 = *p2
Use Space or
up arrow
down arrow
to flip the card.
Question
In deep copy,pointers are followed and data and the pointer structure are duplicated.
Question
When declaring several pointer variables,there must be one pointer declarator * for each pointer variable.
Question
Dangling pointers present no problem in C++
Question
The default copy constructor and default operator = provide deep copy.
Question
Give three uses for the * operator.Name and describe each use.
Question
A function can return an array.
Question
A copy constructor has the same name as the class (let's call it A)and has a parameter that

A)Is call-by value of an A object
B)Is call-by-reference of another class
C)Is call-by-reference of an A class object
D)Is call-by-name of an object named ~A.
E)None of these
Question
Give the output from this code fragment:
int *p1,*p2;
p1 = new int;
p2 = new int;
*p1 = 10;
*p2 = 20;
cout << *p1 << " " << *p2 << endl;
*p1 = *p2;
cout << *p1 << " " << *p2 << endl;
*p1 = 30;
cout << *p1 << " " << *p2 << endl;
Question
Pointer variables are just memory addresses and can be assigned to one another without regard to type.
Question
If a class is named MyClass,what must the destructor be named?

A)Erase
B)MyClass
C)Any name the programmer wishes except the name of the class
D)~MyClass
E)None of the above.
Question
Dynamic variables or dynamically allocated variables in C++ are created and destroyed according to the program's needs.
Question
A pointer is an address,an address is an integer,but a pointer is not an integer.
Question
An array name is a constant pointer to array base type.
Question
There should eventually be a call to the operator delete on a pointer that
points to the memory allocated by each call to new.
Question
Given the declarations below,write a code fragment that allocates a nameless variable for pointer p1 to point to.
int *p1,*p2;
Question
Suppose we have the following definitions and assignments:
double *p,v;
p = &v;// p gets the address of v
What is the effect of this assignment?
*p = 99.99
Question
A pointer is a variable that holds the address of some other location in memory.
Question
You can get a pointer value to initialize a pointer variable from an object of an appropriate type with the "address-of" operator,&.
Question
One can use the & operator to extract the value that a pointer points to.
Question
Give the sequence of steps for creating and using a dynamic array.
Question
Why did C++11 introduce the nullptr constant when we already have NULL?
Question
Write a type definition for a type called NumberPtr that will be the type for pointer variables that hold pointers to dynamic variables of type double.Also,write a declaration for a pointer variable called myPoint,which is of type NumberPtr.
Question
It is an error to call the delete operator on a pointer a second time.Consider what happens if there is memory allocated on the free store for a class,a destructor,and initializing constructors have been defined,but no copy constructor was defined.Now suppose a function is called that has call-by-value parameters.Why does the program either die in the middle or give a segmentation fault?
Question
Describe the action of the new operator.What does the new operator return? What are the indications of error?
Question
Tell about the freestore (also known as heap).What is put there? How is this done? How does a program access the freestore? What happens to memory that has been allocated but is no longer needed?
Question
Here is the first line of the copy constructor for PFArrayD.The identifier PFArrayD is the name of the class,but in the header it is used three times with different meaning each time.Give the meaning for each use:
PFArrayD::PFArrayD( const PFArrayD& pfaObject)
Question
Your program creates a dynamically allocated array as follows:
int *entry;
entry = new int[10];
so that the pointer variable entry is pointing to the dynamically allocated array.Write code to fill this array with 10 numbers typed in at the keyboard.
Question
Write a type definition for pointer variables that will point to dynamically allocated arrays whose base type is char.The name of the type will be CharArray.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/29
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Pointers and Dynamic Arrays
1
Given the definitions,
int *p1,*p2;
p1 = new int;
p2 = new int;
You are to compare and contrast the two assignments.
a)p1 = p2;
b)*p1 = *p2
a)Makes p1 point to the same place in memory to which p2 points.Orphans memory p1 did point to.
b)Makes data where p1 points the same as the data where p2 points.
2
In deep copy,pointers are followed and data and the pointer structure are duplicated.
True
3
When declaring several pointer variables,there must be one pointer declarator * for each pointer variable.
True
4
Dangling pointers present no problem in C++
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
5
The default copy constructor and default operator = provide deep copy.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
6
Give three uses for the * operator.Name and describe each use.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
7
A function can return an array.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
8
A copy constructor has the same name as the class (let's call it A)and has a parameter that

A)Is call-by value of an A object
B)Is call-by-reference of another class
C)Is call-by-reference of an A class object
D)Is call-by-name of an object named ~A.
E)None of these
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
9
Give the output from this code fragment:
int *p1,*p2;
p1 = new int;
p2 = new int;
*p1 = 10;
*p2 = 20;
cout << *p1 << " " << *p2 << endl;
*p1 = *p2;
cout << *p1 << " " << *p2 << endl;
*p1 = 30;
cout << *p1 << " " << *p2 << endl;
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
10
Pointer variables are just memory addresses and can be assigned to one another without regard to type.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
11
If a class is named MyClass,what must the destructor be named?

A)Erase
B)MyClass
C)Any name the programmer wishes except the name of the class
D)~MyClass
E)None of the above.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
12
Dynamic variables or dynamically allocated variables in C++ are created and destroyed according to the program's needs.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
13
A pointer is an address,an address is an integer,but a pointer is not an integer.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
14
An array name is a constant pointer to array base type.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
15
There should eventually be a call to the operator delete on a pointer that
points to the memory allocated by each call to new.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
16
Given the declarations below,write a code fragment that allocates a nameless variable for pointer p1 to point to.
int *p1,*p2;
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
17
Suppose we have the following definitions and assignments:
double *p,v;
p = &v;// p gets the address of v
What is the effect of this assignment?
*p = 99.99
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
18
A pointer is a variable that holds the address of some other location in memory.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
19
You can get a pointer value to initialize a pointer variable from an object of an appropriate type with the "address-of" operator,&.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
20
One can use the & operator to extract the value that a pointer points to.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
21
Give the sequence of steps for creating and using a dynamic array.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
22
Why did C++11 introduce the nullptr constant when we already have NULL?
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
23
Write a type definition for a type called NumberPtr that will be the type for pointer variables that hold pointers to dynamic variables of type double.Also,write a declaration for a pointer variable called myPoint,which is of type NumberPtr.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
24
It is an error to call the delete operator on a pointer a second time.Consider what happens if there is memory allocated on the free store for a class,a destructor,and initializing constructors have been defined,but no copy constructor was defined.Now suppose a function is called that has call-by-value parameters.Why does the program either die in the middle or give a segmentation fault?
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
25
Describe the action of the new operator.What does the new operator return? What are the indications of error?
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
26
Tell about the freestore (also known as heap).What is put there? How is this done? How does a program access the freestore? What happens to memory that has been allocated but is no longer needed?
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
27
Here is the first line of the copy constructor for PFArrayD.The identifier PFArrayD is the name of the class,but in the header it is used three times with different meaning each time.Give the meaning for each use:
PFArrayD::PFArrayD( const PFArrayD& pfaObject)
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
28
Your program creates a dynamically allocated array as follows:
int *entry;
entry = new int[10];
so that the pointer variable entry is pointing to the dynamically allocated array.Write code to fill this array with 10 numbers typed in at the keyboard.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
29
Write a type definition for pointer variables that will point to dynamically allocated arrays whose base type is char.The name of the type will be CharArray.
Unlock Deck
Unlock for access to all 29 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 29 flashcards in this deck.