Получение следующей ошибки:
malloc: *** ошибка для объекта 0x7ffee85b4338: освобожденный указатель не был выделен
Я думал, что выделил и освободил должным образом, но не должен был.Что я сделал не так?Он будет компилироваться с нулевыми ошибками и нулевыми предупреждениями, но не будет печатать последний оператор (COMPLETE) после освобождения памяти.
См. Исходный код ниже.Любая помощь приветствуется.
#include <iostream>
using namespace std;
int main(){
int numOne, numTwo, numThree;
cout << "When prompted please enter a whole number." << endl;
//obtain user input for numbers and store to integer variables
cout << "\nEnter a number: ";
cin >> numOne;
cout << "Enter another number: ";
cin >> numTwo;
cout << "Enter a third number: ";
cin >> numThree;
//print out user's entered numbers as stored in variables
cout<< "\nThe numbers you entered currently stored as variables are: ";
cout << numOne << ", " << numTwo << " and " << numThree << endl;
//create pointers and allocate memory
int* pointerOne = new int;
int* pointerTwo = new int;
int* pointerThree = new int;
//store location of variables to pointers
pointerOne = &numOne;
pointerTwo = &numTwo;
pointerThree = &numThree;
//print out user's entered numbers as stored in pointers
cout<< "The numbers you entered currently stored as pointers are: ";
cout << *pointerOne << ", " << *pointerTwo << " and " << *pointerThree << endl;
//alter user's numbers to show that pointers alter variables
cout << "\nNOTICE: Incrementing entered values by one! ";
*pointerOne = *pointerOne + 1;
*pointerTwo = *pointerTwo + 1;
*pointerThree = *pointerThree + 1;
cout << "....COMPLETE!" << endl;
//print out user's entered numbers as stored in variables
cout<< "\nThe numbers you entered incremented by one shown using variables: ";
cout << numOne << ", " << numTwo << " and " << numThree << endl;
//deallocate memory
cout << "\nWARNING: Deallocating pointer memory! ";
delete pointerOne;
delete pointerTwo;
delete pointerThree;
cout << "....COMPLETE!" << endl;
}