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

C++ This program I created compiles all the way to the end, but has an error at

ID: 3912363 • Letter: C

Question

C++

This program I created compiles all the way to the end, but has an error at the end "RUN FAILED (exit value 1,........). Please help me find the error......

#include <cstdlib>
#include <iostream>


using namespace std;

int main(int argc, char** argv) {

int iFirst, iSecond, iThird;
int *pFirst, *pSecond, *pThird;
  
cout << "Enter 3 Integers" << endl;
cout << endl;
cout << "Enter 1st Integer: " << endl;
cin >> iFirst;
if ( (pFirst = new int[iFirst]) == 0)
{
cerr << "Not sufficient memory. The program will exit!";
exit(1);
}

cout << "Enter 2nd Integer: " << endl;
cin >> iSecond;
if ( (pSecond = new int[iSecond]) == 0)
{
cerr << "Not sufficient memory. The program will exit!";
exit(1);
}
cout << "Enter 3rd Integer: " << endl;
cin >> iThird;
if ( (pThird = new int[iThird]) == 0)
{
cerr << "Not sufficient memory. The program will exit!";
exit(1);
}
  
pFirst = &iFirst, pSecond = &iSecond, pThird = &iThird;
  
cout << "1st Integer Value: " << iFirst << endl;
cout << "1st Integer Memory Location: " << pFirst << endl;
cout << endl;
  
cout << "2nd Integer Value: " << iSecond << endl;
cout << "2nd Integer Memory Location: " << pSecond << endl;
cout << endl;
  
cout << "3rd Integer Value: " << iThird << endl;
cout << "3rd Integer Memory Location: " << pThird << endl;
cout << endl;
  
delete[] pFirst;
delete[] pSecond;
delete[] pThird;
  
exit(1);
return 0;
}

Explanation / Answer

This error is present because first you are trying to allocate memory dynamically then you are changing the memory address stored in the variables to the address of integers which are local variables and not allocated dynamically hence there is this error because you can't free a pointer that is not dynamically allocated

So I think that you are trying to free the array hence the code will be

#include <cstdlib>
#include <iostream>


using namespace std;

int main(int argc, char** argv) {

int iFirst, iSecond, iThird;
int *pFirst, *pSecond, *pThird;
  
cout << "Enter 3 Integers" << endl;
cout << endl;
cout << "Enter 1st Integer: " << endl;
cin >> iFirst;
if ( (pFirst = new int[iFirst]) == 0)
{
cerr << "Not sufficient memory. The program will exit!";
exit(1);
}

cout << "Enter 2nd Integer: " << endl;
cin >> iSecond;
if ( (pSecond = new int[iSecond]) == 0)
{
cerr << "Not sufficient memory. The program will exit!";
exit(1);
}
cout << "Enter 3rd Integer: " << endl;
cin >> iThird;
if ( (pThird = new int[iThird]) == 0)
{
cerr << "Not sufficient memory. The program will exit!";
exit(1);
}
  
//pFirst = &iFirst, pSecond = &iSecond, pThird = &iThird;
  
cout << "1st Integer Value: " << iFirst << endl;
cout << "1st Integer Memory Location: " << &iFirst << endl;
cout << endl;
  
cout << "2nd Integer Value: " << iSecond << endl;
cout << "2nd Integer Memory Location: " << &iSecond << endl;
cout << endl;
  
cout << "3rd Integer Value: " << iThird << endl;
cout << "3rd Integer Memory Location: " << &iThird << endl;
cout << endl;
  
delete[] pFirst;
delete[] pSecond;
delete[] pThird;
return 0;
}

Do give a thumbs up

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