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

面对面孔

我正在使用C ++。我正在研究一种更改堆栈的最大容量的方法,但我对收到的错误感到困惑。下面是我的方法。

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()方法是否有效。

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”中的st8(5)中创建myArray会导致程序崩溃。

我感到困惑的主要根源是由于每个部分都单独通过,但它们并非共同通过。我不确定该怎么办。我的方法写错了吗?如果是这样,我应该如何纠正?

谢谢你。

萨胡

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

  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;
  }

您需要的是:

  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;
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++ malloc():内存损坏

来自分类Dev

如何修复malloc()的控制台返回:内存损坏(快速)

来自分类Dev

野牛malloc内存损坏

来自分类Dev

错误:malloc():比较函数中的内存损坏,无法进行排序

来自分类Dev

Kinect SDK损坏的内存错误

来自分类Dev

C内存分配(malloc)

来自分类Dev

C ++:损坏的双链表和内存损坏

来自分类Dev

malloc/free double free 或损坏错误

来自分类Dev

C-线程的内存损坏

来自分类Dev

C ++链表中的内存损坏

来自分类Dev

错误:双重释放或损坏(快速更新)

来自分类Dev

malloc():内存损坏在一个奇怪的地方

来自分类Dev

为什么malloc在这里引起内存损坏?

来自分类Dev

malloc():内存损坏在一个奇怪的地方

来自分类Dev

malloc() 内存损坏,仅写入特定数量的 int 数组

来自分类Dev

内存分配错误-SKAction runBlock(malloc错误)

来自分类Dev

堆损坏,C 错误

来自分类Dev

短期和快速malloc内存访问问题

来自分类Dev

释放由malloc / Valgrind错误分配的内存

来自分类Dev

Valgrind说malloc内存损坏,没有使用过malloc

来自分类Dev

C ++中的Malloc错误?

来自分类Dev

在std :: string上使用malloc / realloc / free时出现c ++内存错误

来自分类Dev

无法找出双重释放或损坏(快速停止)错误

来自分类Dev

在C中实现隔离的内存存储(malloc)

来自分类Dev

C中的Malloc内存消耗行为

来自分类Dev

目标c中的malloc和内存泄漏

来自分类Dev

在C中实现隔离的内存存储(malloc)

来自分类Dev

C-访问malloc的内存后的SIGSEGV

来自分类Dev

在C中使用malloc保留内存