Целочисленное выделение / освобождение памяти - PullRequest
0 голосов
/ 30 сентября 2018

Получение следующей ошибки:

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;

}

1 Ответ

0 голосов
/ 01 октября 2018

Вы можете сохранить введенные пользователем данные в переменных указателей, для которых вы выделили память, а затем использовать указатели для управления их значениями:

int *numOne, *numTwo, *numThree;
numOne = new int;
numTwo = new int;
numThree = new int;

int *pointerOne, *pointerTwo, *pointerThree;
pointerOne = numOne;
pointerTwo = numTwo;
pointerThree = numThree;

cout << "\nEnter a number: ";
cin >> *numOne; // You can use cin >> *pointerOne;
cout << "Enter another number: ";
cin >> *numTwo; // You can use cin >> *pointerTwo;
cout << "Enter a third number: ";
cin >> *numThree; // You can use cin >> *pointerThree;

cout << "\nNOTICE: Incrementing entered values by one! ";
*pointerOne++;
*pointerTwo++;
*pointerThree++;
cout << "....COMPLETE!" << endl;

delete numOne;
delete numTwo;
delete numThree;
...