【问题标题】:Integer memory allocation/deallocation整数内存分配/释放
【发布时间】:2018-09-30 18:03:59
【问题描述】:

得到以下错误:

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;

}

【问题讨论】:

  • pointerOne = &amp;numOne; 使pointerOne 指向numOne。它不再指向您之前创建的无名 new int。只是不要newdelete,你的代码应该没问题。
  • 嗯,我的项目是使用 new 和 delete 运算符来学习管理内存。所以我不确定我需要做些什么才能做到这一点。
  • 嗯,我的项目是使用 new 和 delete 运算符来学习管理内存。 -- 你通过了解何时和何时来学习管理内存不要使用newdelete,不要以奇怪和迂回的方式使用new / delete
  • @HashimGari 您要么想使用位于堆栈上的numXXX 局部变量,要么使用指向分配的动态内存的pointerXXX。如果你写pointerOne=new Int; pointerOne=&amp;numOne,你分配了一个int,将它的地址存储在指针中,然后用本地numOne的地址覆盖它。所以你丢失了动态 int 的地址,以后不能删除它=内存泄漏。并且永远不要在未从new 返回的地址上调用delete
  • 所以我不确定我需要做什么。我的目标是将整数存储到三个不同的变量中,创建三个指向每个值的指针,显示变量和指针,同时使用 new 和 delete 运算符....

标签: c++ memory memory-management allocation


【解决方案1】:

您可以将用户的输入存储到为他们分配内存的指针变量中,然后使用指针来操作它们的值:

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;

【讨论】:

  • 谢谢!出于某种原因,这就是我没有点击的内容。立即创建一个变量并为该变量分配内存!非常感谢!
猜你喜欢
  • 2011-12-09
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 2012-02-01
  • 2010-09-21
  • 2013-02-28
相关资源
最近更新 更多