重新分配数组的内存?

时间:2014-02-21 23:36:28

标签: c++

void IntVector::expand(){ 
    int cap2 = cap * 2; 
    int* data2 = new int[cap2];
    for (unsigned int i = 0; i < sz; i++){
        data2[i] = data[i];
    }
    delete[] data;
    data = data2;
    cap = cap2; 
}

expand将向量的容量加倍,并为动态分配的数组重新分配内存并更新容量值。

但是,由于我班级的测试工具继续在我身上崩溃并且声明存在未定义的行为,因此我在使用此功能时遇到了问题。

编辑:

void IntVector::expand(){
    if(cap == 0 || sz == 0){
        exit(1);
    }
    int cap2 = cap * 2; 
    int* data2 = new int[cap2];
    for (unsigned int i = 0; i < sz; i++){
        data2[i] = data[i];
    }
    delete[] data;
    data = data2;
    cap = cap2; 
}

void IntVector::expand(unsigned amount){
    if(cap == 0 || sz == 0){
        exit(1);
    }
    cap += amount;
    int *data2 = new int[cap];
    //copy(data, data+sz, data2);
    for (unsigned int i = 0; i < sz; i++){
        data2[i] = data[i];
    }
    delete[] data;
    data = data2;

}

这两个功能我仍然遇到很多问题。该类的测试工具不断崩溃并报告未定义的行为。我应该发布剩余的代码吗?

编辑2:

void IntVector::expand(){
    if(cap == 0){
        exit(1);
    }
    int cap2 = cap * 2; 
    int* data2 = new int[cap2];
    for (unsigned int i = 0; i < sz; i++){
        data2[i] = data[i];
    }
    if(data != NULL){
        delete[] data;
    }
    data = data2;
    cap = cap2; 
}

编辑:

标题

#ifndef INTVECTOR_H
#define INTVECTOR_H

using namespace std;
class IntVector{
private:
    unsigned sz;
    unsigned cap;
    int *data;
public:
    IntVector();
    IntVector(unsigned size);
    IntVector(unsigned size, int value);
    unsigned size() const;
    unsigned capacity() const;
    bool empty() const;
    const int & at (unsigned index) const;
    const int & front() const;
    const int & back() const;
    ~IntVector();
    void insert(unsigned index, int value);
    void erase(unsigned index);
    void push_back(int value);
    void pop_back();
    void clear();
    void resize(unsigned size);
    void resize(unsigned size, int value);
    void reserve(unsigned n);
    void assign(unsigned n, int value);
    int & at(unsigned index);
    int & front();
    int & back();
private:
    void expand();
    void expand(unsigned amount);
};

#endif

#include "IntVector.h"
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;



IntVector::IntVector(){
    sz = 0;
    cap = 0;
    data = NULL;
}

IntVector::IntVector(unsigned size){
    if(size = 0){
        cap = 1;
        size = 1;
        data = new int[sz];
        for(unsigned int i = 0; i < sz; i++){
        data[i] = 0;
        }
    }
    sz = size;
    cap = size;
    data = new int[sz];
    for(unsigned int i = 0; i < sz; i++){
        data[i] = 0;
    }
}

IntVector::IntVector(unsigned size, int value){
    if(size = 0){
        cap = 1;
        size = 1;
        data = new int[sz];
        for(unsigned int i = 0; i < sz; i++){
        data[i] = 0;
        }
    }
    sz = size;
    cap = size;
    data = new int[sz];
    for(unsigned int i = 0; i < sz; i++){
        data[i] = value;
    }
}

unsigned IntVector::size() const{
    return sz;
}

unsigned IntVector::capacity() const{
    return cap;
}

bool IntVector::empty() const{
    if(sz > 0){
        return false;
    }
    else{
        return true;
    }
}

const int &IntVector::at(unsigned index) const{
    if(index > sz){
        exit(1);
    }
    else{
        return data[index];
    }
}

const int &IntVector::front() const{
    return data[0];
}

const int &IntVector::back() const{
    return data[sz - 1];
}

IntVector::~IntVector(){
    delete[] data;
}

void IntVector::expand(){
    if(cap == 0){
        cap = 1;
    }
    cap = cap * 2;
    int* data_copy = new int[cap];
    for (unsigned int i = 0; i < sz; i++){
        data_copy[i] = data[i];
    }
    delete[] data;
    data = data_copy;
}

void IntVector::expand(unsigned amount){
    if(cap == 0){
        cap = 1;
    }
    cap += amount;
    int *data_copy = new int[cap];
    for (unsigned int i = 0; i < sz; i++){
        data_copy[i] = data[i];
    }
    //copy(data, data + sz, data_copy);
    delete[] data;
    data = data_copy;

}



void IntVector::insert(unsigned index, int value){
    if(index > sz || cap == 0){
        exit(1);
    }
    else{
        sz += 1;
        if(sz = cap){
            expand();
        }

        for(unsigned int i = 0; i <= sz; i++){
            if(i = index){
                data[i] = value;
            }
            if(i > index){
                data[i - 1] = data[i];
            }
        }
    }
}

void IntVector::erase(unsigned index){
    if(index > sz || cap == 0){
        exit(1);
    }
    else{
        for(unsigned int i = 0; i <= sz; i++){
            if(i = index){
                data[i] = data[i + 1];
            }
        }
        sz -= 1;
    }
}

void IntVector::push_back(int value){
    sz += 1;
    if(sz > cap){
        expand();
    }
    data[sz] = value;
}

void IntVector::pop_back(){
    if(sz < 0 || cap <= 0){
        exit(1);
    }
    sz -= 1;
}

void IntVector::clear(){
    sz = 0;
}

void IntVector::resize(unsigned size, int value){
    if(size < sz){
        sz = size;
    }
    else if(size > sz){
        int a = size - sz;
        for (unsigned int i = a; i < size; i++){
            data[i] = value;
        }
    }
    if(sz > cap){
        int b = cap * 2;
        int c = sz - cap;
        if(b > c){
            expand();
        }
        else{
            expand(c);
        }
    }
}

void IntVector::reserve(unsigned n){
    int a = cap * 2;
    int b = n - cap;
    if(a > b){
        expand(a);
    }
    else{
        expand(b);
    }
}

void IntVector::assign(unsigned n, int value){
    sz = n;
    if(sz > cap){
        int a = cap * 2;
        int b = n - cap;
        if(a > b){
            expand(a);
        }
        else{
            expand(b);
        }
    }
    data = new int[sz];
    *data = value;
}

int & IntVector::at(unsigned index){
    if(index >= sz){
        exit(1);
    }
    return data[index];
}

int & IntVector::front(){
    return data[0];
}

int & IntVector::back(){
    return data[sz - 1];
}

2 个答案:

答案 0 :(得分:0)

(来自评论)

每一行*data = *data2都应该阅读data = data2。将星号放在前面取消引用指针;这不是你想要做的。

(对编辑过的问题的回复)

默认构造函数设置data = NULL。当您尝试删除此指针时,您将收到错误。您应该始终在if (data != NULL)之前检查delete[] data

(另外,其他构造函数有*data = 0。这可能不对。如果你想把整个事情归零,你可以使用memsetZeroMemory ...或者像你的其他函数中的for循环。)

答案 1 :(得分:0)

首先,你几乎有重复的expand()函数。你应该从没有提供参数的那个中调用1参数expand函数。

void IntVector::expand() 
{
    expand(cap * 2);
}

现在你处理带有一个参数的expand()函数。

void IntVector::expand(unsigned amount)
{
   if (cap == 0 || sz == 0 || amount == 0)
     return;
   cap += amount;
   int *data2 = new int[cap];
   std::copy(data, data + sz, data2);
   delete[] data;
   data = data2;

}

此代码没有任何内在错误(请注意,我刚刚返回,而不是调用exit(1))。此外,还会检查金额以确保您确实需要更改容量。

因此,您需要发布测试工具和课程的其余部分。我们不知道你是如何使用这门课程的。

现在:

IntVector::IntVector(unsigned size)
{
    sz = size;
    cap = size;
    data = new int[sz];
    *data = 0;   // what if size is 0?
}

如果传递的大小为0,那么代码就会失败。是的,你可以在技术上分配一个大小为0的数组,但你不能像在那一行那样取消引用那个内存。你应该检查大小是否为0。