首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >malloc( ):内存损坏(快速)错误C++

malloc( ):内存损坏(快速)错误C++
EN

Stack Overflow用户
提问于 2014-10-26 02:34:17
回答 2查看 17.4K关注 0票数 1

我在C++工作。我正在研究一种方法来改变堆栈的最大容量,我对我正在得到的一个错误感到困惑。下面是我的方法。

代码语言:javascript
复制
void Stack::setCapacity(unsigned newCapacity){
if(newCapacity< this->getSize()){
    throw StackException("setCapacity()", 
    "the size is larger than the desired capacity");
} else {
    if(newCapacity != myCapacity){
        Item * tempArray = new Item[newCapacity];
        if(newCapacity < myCapacity){
            for(unsigned i=0; i<newCapacity;i++){
                tempArray[i] = myArray[i];
            }
        } else if (newCapacity > myCapacity) {
            for(unsigned i=0; i<myCapacity; i++){
                tempArray[i] = myArray[i];
            }
        }
        for(unsigned i=0; i<newCapacity; i++){
            myArray[i] = tempArray[i];
        }
        delete tempArray;
    }
    myCapacity = newCapacity;
} 
}

我还编写了一个测试方法来测试我的setCapacity( )方法是否有效。

代码语言:javascript
复制
void StackTester::setCapacityTest() {
cout << "- setCapacity... " << flush;

// empty stack
Stack st7(5);
assert(st7.getSize() == 0);
assert(st7.getCapacity() == 5);
st7.setCapacity(7);
assert(st7.getCapacity() == 7);
cout << " 1 " << flush;

// partially filled stack - larger capacity
Stack st8(5);
assert(st8.getCapacity() == 5);
st8.push(3);
st8.push(4);
st8.setCapacity(7);
assert(st8.getCapacity() == 7);
assert(st8.getTop() == 4);
st8.pop();
assert(st8.getTop() == 3);
cout << " 2 " << flush;

// size larger than new capacity
try{
Stack st9(3);
st9.push(7);
st9.push(4);
st9.push(11);
assert(st9.getSize() == 3);
st9.setCapacity(2);
cerr << "setCapacity's new capacity is larger than the size";
exit(1);
} catch(StackException& se){
    cout << " 3 " << flush;
}

// partially filled stack - smaller capacity
Stack st10(5);
assert(st10.getCapacity() == 5);
st10.push(1);
st10.setCapacity(2);
assert(st10.getCapacity() == 2);
assert(st10.getTop() == 1);
cout << " 4 " << flush;

// fully filled stack - larger capacity
Stack st11(2);
assert(st11.getCapacity() == 2);
st11.push(3);
st11.push(7);
assert(st11.getTop() == 7);
st11.setCapacity(3);
assert(st11.getCapacity() == 3);
cout << " 5 " << flush;

cout << " Passed!" << endl;
}

当我通过注释其他部分来单独运行测试的每个部分时,一切都很好。测试的每个部分都通过了。但是,当我组合这些部分并尝试运行整个测试时,我会得到以下错误:

* glibc检测到*/home/./StackProject: malloc():内存损坏(快速):0x0000000001e86030 *

使用调试器,我将问题的范围缩小到在堆栈中创建myArray。例如,在我的测试中成功运行“1”之后,在“2”中在myArray (5)中创建st8将导致程序崩溃。

我感到困惑的主要原因是,每一节都是个别通过的,但并不是集体通过的。我不知道该怎么办。我的方法写错了吗?如果是这样的话,我该如何纠正呢?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-26 04:07:28

在下面的代码块中,我看到了setCapacity中的以下问题:

代码语言:javascript
复制
  if(newCapacity != myCapacity){
     Item * tempArray = new Item[newCapacity];
     if(newCapacity < myCapacity){
        for(unsigned i=0; i<newCapacity;i++){
           tempArray[i] = myArray[i];
        }
     } else if (newCapacity > myCapacity) {
        for(unsigned i=0; i<myCapacity; i++){
           tempArray[i] = myArray[i];
        }
     }

     // When newCapacity > myCapacity, myArray does not
     // have enough space for this loop.
     // Say myCapacity = 5 and newCapacity = 7
     // Accessing myArray[5] and myArray[6] is a problem.

     for(unsigned i=0; i<newCapacity; i++){
        myArray[i] = tempArray[i];
     }

     // You are deleting the newly allocated array, even though you are using
     // the wrong delete operator.
     // myArray still points to the old allocated memory.
     delete tempArray;
  }

你需要的是:

代码语言:javascript
复制
  if(newCapacity != myCapacity){
     Item * tempArray = new Item[newCapacity];
     if(newCapacity < myCapacity){
        for(unsigned i=0; i<newCapacity;i++){
           tempArray[i] = myArray[i];
        }
     } else if (newCapacity > myCapacity) {
        for(unsigned i=0; i<myCapacity; i++){
           tempArray[i] = myArray[i];
        }
     }

     // Need to delete the old array and keep the new array.

     Item* oldArray = myArray;
     myArray = tempArray;

     // Use the array delete operator, not the simple delete operator.
     delete [] oldArray;
  }
票数 4
EN

Stack Overflow用户

发布于 2014-10-26 02:43:09

你有

代码语言:javascript
复制
delete tempArray;

但是它是一个数组,所以你必须做:

代码语言:javascript
复制
delete [] tempArray;

(虽然我不知道这个函数是做什么的,也不知道为什么.)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26569250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档