【问题标题】:realloc(): invalid old size even when malloc() is used to allocate memoryrealloc():即使使用 malloc() 分配内存,旧大小也无效
【发布时间】:2019-07-20 20:24:05
【问题描述】:

我正在尝试在 C++ 中实现动态堆栈。 我在类堆栈中有 3 个成员 1.cap是容量。 2.top-指向栈顶 3. arr- 指向整数的指针。

在类构造函数中,我将内存分配给堆栈(malloc)。 稍后在 meminc() 中,我试图重新分配内存。

我已经编写了一个函数 meminc() 来重新分配内存,但是我得到了这个无效的旧大小错误。

如果您让我知道这段代码有什么问题,将会很有帮助。我也会感谢给我的任何建议。 谢谢。

#include <iostream>

using namespace std;

#define MAXSIZE 5

class stack {
    int cap;
    int top;
    int *arr;

public:
    stack();
    bool push(int x);
    bool full();
    bool pop();
    bool empty();
    bool meminc();
};

stack::stack()
{
    cap = MAXSIZE;
    arr = (int *)malloc(sizeof(int)*MAXSIZE);
    top = -1;
}

bool stack::meminc()
{
    cap = 2 * cap;
    cout << cap << endl;
    this->arr = (int *)realloc(arr, sizeof(int)*cap);
    return(arr ? true : false);
}

bool stack::push(int x)
{
    if (full())
    {
        bool x = meminc();
        if (x)
            cout << "Memory increased" << endl;
        else
            return false;
    }

    arr[top++] = x;
    return true;
}

bool stack::full()
{
    return(top == MAXSIZE - 1 ? true : false);
}

bool stack::pop()
{
    if (empty())
        return false;
    else
    {
        top--;
        return true;
    }
}

bool stack::empty()
{
    return(top == -1 ? true : false);
}

int main()
{
    stack s;
    char y = 'y';
    int choice, x;
    bool check;

    while (y == 'y' || y == 'Y')
    {
        cout << "                 1.push\n                    2.pop\n" << endl;
        cin >> choice;

        switch (choice)
        {
        case 1: cout << "Enter data?" << endl;
            cin >> x;
            check = s.push(x);
            cout << (check ? "              push complete\n" : "              push failed\n");
            break;

        case 2: check = s.pop();
            cout << (check ? "              pop complete\n" : "               pop failed\n");
            break;

        default: cout << "ERROR";
        }
    }
}

【问题讨论】:

  • 您的堆栈需要 iostream,这很有趣。

标签: c++ memory dynamic malloc realloc


【解决方案1】:

要补充约翰的答案,

你使用 realloc() 的方式……有缺陷。

bool stack::meminc()
{
    cap = 2 * cap;
    cout << cap << endl;
    this->arr = (int *)realloc(arr, sizeof(int)*cap);
    return(arr ? true : false);
}

如果 realloc() 失败,它将返回 nullptr 并且指向原始内存区域的唯一指针 (arr) 将消失。此外,您应该简单地使用return arr != nullptr; 而不是return(arr ? true : false);

realloc()的正确tm使用方式:

bool stack::meminc()
{
    int *temp = (int*) realloc(arr, sizeof(*temp) * cap * 2);
    if(!temp)
        return false;
    cap *= 2;
    arr = temp;
    return true;
}

另外,你的 copy-ctor、赋值运算符和 d-tor 在哪里?

【讨论】:

  • 谢谢swordfish,谢谢指正,我是初学者,你能告诉我为什么我需要一个复制构造函数。我确实添加了析构函数。
  • 您需要一个复制 ctor,因为标准复制 ctor 只会复制指针值而不是它指向的数据。
【解决方案2】:

full 函数不正确。应该是

bool stack::full()
{
    return(top == cap - 1 ? true : false);
}

或者更简单,加上const

bool stack::full() const
{
    return top == cap - 1;
}

您也错误地使用了top 变量。由于 top 从 -1 开始,您应该在设置值之前增加 top,而不是之后

arr[++top] = x;

不是错误,但从设计的角度来看,meminc 应该是一个私有函数。

【讨论】:

  • 谢谢。我更正了代码。为了摆脱 realloc 错误,我添加了 arr=NULL;在 realloc 语句之前,它起作用了。你能告诉我为什么我必须添加这一行吗?我正在学习。
  • @AshishSurve 您不应该添加该行,通过添加 arr=NULL; 您正在将重新分配转换为分配。如果它仍然不适合你,那么你在其他地方有一个错误。不要只是在不了解它们的作用的情况下将内容添加到代码中,您只是在隐藏错误而不是修复错误。
  • @AshishSurve 你修复了我发现的第二个错误吗?我认为这是更严重的一个。
  • 谢谢你,约翰,我更正了代码。如果有任何改进,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 2012-07-09
  • 2014-08-26
  • 2019-10-31
  • 2015-02-05
  • 2016-07-04
  • 2014-08-26
  • 2014-05-18
相关资源
最近更新 更多