Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

1) Declare and initialize a pointer px to an integer variable x with t.docx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité

Consultez-les par la suite

1 sur 2 Publicité

1) Declare and initialize a pointer px to an integer variable x with t.docx

Télécharger pour lire hors ligne

1) Declare and initialize a pointer px to an integer variable x with the initialized value 3. Consider two cases of the value of x allocate on
(a) the stack memory
(b) the heap memory
2) Write a comment on each instruction down below and provide a memory diagram for heap and stack to illustrate these statements.
int* iptr = new int[5]; //
int* iptr_1 = new int(5); //
Solution
Question 1)
(a)the stack memory
Answer: Decleration and initialization of pointer is given below:
int x = 3 // Here x is a integer variable having value 3
int* px = NULL; //Here we have decleared a pointer of integer type named px, which initially point to NULL
px = &x; // integer pointer px holding the address of integer variable
cout<<*px // *px results into value 3
b) the heap memory
int x = 3; // Here x is a integer variable having value 3
int* px = new int(x); //Here we have created a interger pointer px and alloaced dynamic memory to it using new operator and its point to the value of x
cout<<*px // this statement results into value 3
Question 2)
int* iptr = new int[5]; //Here iptr is a interger pointer pointing to the first index that is 0th index of an interger array having 5 elements;
int* iptr_1 = new int(5); //Here iptr is a interger pointer pointing to the value 5
.

1) Declare and initialize a pointer px to an integer variable x with the initialized value 3. Consider two cases of the value of x allocate on
(a) the stack memory
(b) the heap memory
2) Write a comment on each instruction down below and provide a memory diagram for heap and stack to illustrate these statements.
int* iptr = new int[5]; //
int* iptr_1 = new int(5); //
Solution
Question 1)
(a)the stack memory
Answer: Decleration and initialization of pointer is given below:
int x = 3 // Here x is a integer variable having value 3
int* px = NULL; //Here we have decleared a pointer of integer type named px, which initially point to NULL
px = &x; // integer pointer px holding the address of integer variable
cout<<*px // *px results into value 3
b) the heap memory
int x = 3; // Here x is a integer variable having value 3
int* px = new int(x); //Here we have created a interger pointer px and alloaced dynamic memory to it using new operator and its point to the value of x
cout<<*px // this statement results into value 3
Question 2)
int* iptr = new int[5]; //Here iptr is a interger pointer pointing to the first index that is 0th index of an interger array having 5 elements;
int* iptr_1 = new int(5); //Here iptr is a interger pointer pointing to the value 5
.

Publicité
Publicité

Plus De Contenu Connexe

Similaire à 1) Declare and initialize a pointer px to an integer variable x with t.docx (20)

Plus par deant5 (20)

Publicité

1) Declare and initialize a pointer px to an integer variable x with t.docx

  1. 1. 1) Declare and initialize a pointer px to an integer variable x with the initialized value 3. Consider two cases of the value of x allocate on (a) the stack memory (b) the heap memory 2) Write a comment on each instruction down below and provide a memory diagram for heap and stack to illustrate these statements. int* iptr = new int[5]; // int* iptr_1 = new int(5); // Solution Question 1) (a)the stack memory Answer: Decleration and initialization of pointer is given below: int x = 3 // Here x is a integer variable having value 3 int* px = NULL; //Here we have decleared a pointer of integer type named px, which initially point to NULL px = &x; // integer pointer px holding the address of integer variable cout<<*px // *px results into value 3 b) the heap memory int x = 3; // Here x is a integer variable having value 3 int* px = new int(x); //Here we have created a interger pointer px and alloaced dynamic memory to it using new operator and its point to the value of x cout<<*px // this statement results into value 3 Question 2)
  2. 2. int* iptr = new int[5]; //Here iptr is a interger pointer pointing to the first index that is 0th index of an interger array having 5 elements; int* iptr_1 = new int(5); //Here iptr is a interger pointer pointing to the value 5

×