Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAMMING Are there any syntax or logical errors in my code? #include <ios

ID: 3882334 • Letter: C

Question

C++ PROGRAMMING

Are there any syntax or logical errors in my code?

#include <iostream>

const int SIZE = 10;

using namespace std;

int main()
{
int *list = new int[SIZE];
int *ptr2;

for(int i=0; i<SIZE;i++) {
list[i] = i+1;
}

ptr2 = list;

delete [ ] ptr2;
cout << endl;

for(int i=0; i<SIZE; i++) {
cout << list[i] << " ";
}
}

WHAT ABOUT THIS CODE BELOW AS WELL. ANY SYNTAX OR LOGICAL ERRORS?

int main()
{
int *ptr1 = new int;
int *ptr2;

*ptr1 = 5;
ptr2 = ptr1;

cout << endl << *ptr2 << endl;
delete ptr2;

cout << endl << *ptr1 << endl;

cout << endl;

return 0;
}

Explanation / Answer

For the 1st code segment there is no syntax error present; but logical error is present here. As in this code list and ptr2 are pointing to same location so deleting one pointer means making other pointer
a dangling pointer(A pointer pointing to a memory location that has been freed is called dangling pointer); so after deleting ptr2 the list pointer also not be able to acess the same value which was pointed by it as now it(list) has become the dangling pointer.
Hence here the output would be Garbage values.

So, after deleting ptr2 accessing the location pointed by ptr2 for output through another pointer is the logical error here.

/*Detailed explanation with the code segment is given below*/

#include <iostream>

const int SIZE = 10;

using namespace std;

int main()
{
int *list = new int[SIZE]; //this statement will dynamically allocate 10 int spaces in memory which will be pointed by pointer list
int *ptr2; //this statement declare another int pointer named ptr2

//the following loop assigns values to the spaces pointed by list ponier
for(int i=0; i<SIZE;i++) {
list[i] = i+1;
}
//after the execution of following statement ptr2 will point to the same address which is pointed by list pointer
ptr2 = list;
//the following statement will free the memory pointed by pointer ptr2
delete [ ] ptr2;
//after executing the previous statement the list pointer becomes dangling pointer
cout << endl;
//now the following loop will print some garbage values as list pointer is now a dangling pointer
for(int i=0; i<SIZE; i++) {
cout << list[i] << " ";
}
}

/******************************************************************************************************/

For the 2nd code segment there is also no syntax error present; but logical error is present here. As in this code ptr1 and ptr2 are pointing to same location so deleting one pointer means making other pointer
a dangling pointer; so after deleting ptr2 the ptr1 pointer also not be able to access the same value which was pointed by it as now it(ptr1) has become the dangling pointer.
Hence here the output after deleting ptr2 would be Garbage values. But before deleting ptr2 the output would be 5(which is correct value).

So, after deleting ptr2 accessing the location pointed by ptr2 for output through another pointer is the logical error here.

/*Detailed explanation with the code segment is given below*/

int main()
{
int *ptr1 = new int;//this statement will allocate a memory for int type variable which would be pointed by ptr1  
int *ptr2; //declaring another int pointer ptr2

*ptr1 = 5; //assigning value to the memory location pointer by pointer ptr1
ptr2 = ptr1; //after the execution of this statement ptr2 will point to the same address which is pointed by ptr1 pointer
//the following statement will correctly dislay the value of memory location pointer by ptr2 and the value is 5 here
cout << endl << *ptr2 << endl;
delete ptr2; //this statement will free the memory space pointed by pointer ptr2
//after executing the previous statement the ptr1 pointer becomes dangling pointer as it also points to the same location pointed by ptr2
//now accessing the content of memory through ptr1 will give garbage value
cout << endl << *ptr1 << endl; //prints garbage value

cout << endl;

return 0;
}

/*Hope this explanation will help you.*/

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote