lecture 11 Flashcards

1
Q
  • Each variable is stored at a ………….. and we use the ……………..to get it
A
  • unique address , we address operator & to get address of a variable

for exmple :-
int num = -90;
cout &laquo_space; &num ; will print out the address of this
variable in hexadecimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Pointer variable ( pointer) it’s a…………….
its called a pointer because iit points to the data

A

variable that holds an address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

arrays and reference variables are like pointers but the difference is that arays and referencevariables are done automatically but pointers are manually

A

..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • A pointer that contains the address 0 is called a …………..
  • The null pointer is represented by the literal value ………….. or ………
A
  • null pointer
  • nullptr (or NULL)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

int *intptr;

is a pointer of type int , meaning that it can hold the adress of an integer variable

A

intptr = #

assigning the pointer to point to the address of variable num

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the output of each

int x = 25;
int *ptx = &x;
cout &laquo_space;ptx ;
cout &laquo_space;*ptx;
*ptx = 100 ;
cout &laquo_space;x ;

A

first one prints address of x i hexadecimal
second one prints value of x 25
third one prints value of x after assigning 100 to the address pointed to by ptx

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

int a; //Integer
int* p; //Pointer
int** q; //Pointer to Pointer

a = 58;
p = &a;
q = &p;

cout &laquo_space;a ;
cout &laquo_space;p ;
cout &laquo_space;*p ;
cout &laquo_space;q ;
cout &laquo_space;*q ;
cout &laquo_space;**q ;
cout &laquo_space;&q ;

A

1) 58
2) address of a
3) 58
4) address of pointer p which is same as value in pointer q

5) value stored in pointer q ie: address of pointer p
6) value stored in address of pointer p = 58
7) address of pointer q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

int array[ ] = { 4, 5 , 6} ;
cout &laquo_space;* array ; // value stored in pointer array

A

4 because Array name can be used as a pointer constant as it poits to the value of the first element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

int array[ ] = { 4, 5 , 6} ;
int * arrayptr = array;
cout &laquo_space; arrayptr [1] ;

A

5 because Pointer can be used as an array name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

int vals[ ] = { 4, 7, 11} ;
int *valptr ;
valptr = vals ;

cout &laquo_space;*(valptr+1) ;
cout &laquo_space;*(valptr+2) ;

A
  • value stored in address ofsecond element = 7
  • value stored in address of third element = 11
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

we can access the value stored in a specific index of an array in 4 ways :

RULE : array [ i ] is equivalent to *(array + i )

int values [ ] = { 0 , 1 , 17} ;
int *ptv ;
ptv = values ;

cout &laquo_space;values [2] ;
cout &laquo_space;ptv [2] ;
cout &laquo_space;*(values + 2) ;
cout &laquo_space;*(ptv + 2) ;

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

operations on pointers :

int vals[ ] = { 4 , 7 , 11 };
int *valptr = vals ;

cout &laquo_space;valptr ++ ;
cout &laquo_space;valptr – ;
cout &laquo_space;* (valptr + 2) ;
cout &laquo_space;valptr = vals ;
cout &laquo_space;valptr += 2 ;
cout &laquo_space;valptr–val ;

A

1) increments the pointer by 1 to point at 7
2) decrements the pointer by 1 to point at 4
3) value stored at index 2 in the array = 11
4) value stored in index 0 = 4
5) value stored at index 2 = 11
6) value stored at index that 1st pointer valptr points to and 2nd pointer val points to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

if (ptr1 == ptr2) // compares addresses
if (*ptr1 == *ptr2) // compares contents

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

…………..operator is used to allocate memory dynamically while the program isrunning
and returns ……….

A

“new”
address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

1) automatic (static ) memoy allocation :

  • any variable that is declared in the compile time is stored in the stac memory
  • is only availble within the scope during compile time
  • the allocation and deallocation is done automatically by the compiler
  • stack memory is named stac because first in last out (meaning first declared vaiable or function is the last one deleted)
  • stack memory is limited and can easiily runout

2) dynamic (heap ) memory allocation :

  • is in the heap memory that is done by the user
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

why cant we declare an array of size given by the user by this code ?

int size ;
cin&raquo_space; size ;
int arr [ size ] ;

A

because we must declare the size of the array during compile time) in the stack memory allocation for a static array

in the previous example the array size is taken as an input from the user during the runtime not the compile time so we must use dynamic memory allocation in the heap memory

17
Q

compilation time is divided into two parts
1) compilation time : before running your program
2) runtime : after running your program

A
18
Q

if you create a memory location in the heap memory using dynamic memory allocation then you are responsible for deleting this memory space (deallocation) after finishing as it is not automatically done by the compiler

A
19
Q

1) New —–> lets us access the heap memory by a pointer
2) delete —-> deletes from heap memory

A
20
Q

int x = 5 ; // allocates space in stack memory
int *ptx = new int ; // allocate a space to store integer
in the heap memory
*ptx = 5 ; // el value ely el pointer byshawr 3aleh =5
delete ptx ; // delete the allocation in heap memory and the ptx variable is stored in the stack memory so it is automatically deleted

A
20
Q

int x = 5 ; // allocates space in stack memory

int *ptx = new int ; // allocate a space to store integer
in the heap memory

*ptx = 5 ; // el value ely el pointer byshawr 3aleh =5

delete ptx ; // delete the allocation in heap memory
and the ptx variable is stored in the
stack memory so it is automatically
deleted at the end of its scope

A
21
Q

we cannot return two things nor an array in functions instead we return a pointer

A
22
Q
  • Can allocate storage for a variable while program is running
  • Computer returns address of newly allocated variable
  • Uses new operator to allocate memory:
    double *dptr;
    dptr = new double;
  • new returns address of memory location
A
23
Q
  • this: predefined pointer available to a class’s member functions
  • Always points to the instance (object) of the class whose function is
    being called
  • Is passed as a hidden argument to all non-static member functions
  • Can be used to access members that may be hidden by parameters
    with same name
A