0

I am working with a class I created that has a function addClass which allow the user to add A an Instance of Class to a dynamically allocated array.

Here is the code of the class and a simple test:

Class.h Listing

#ifndef CLASS_H
#define CLASS_H

#include<iostream>

class Class {
public:
    Class(std::string text);
    Class(const Class& orig);
    virtual ~Class();
    Class(std::string text, Class * name, int size);
    std::string toString();
    void addClass(Class * name, int size = 1);
    Class getClass(int index);
private:
    Class * classArray;
    std::string value;
    int size;

};

#endif  /* CLASS_H */

Class.c Listing

#include "Class.h"
#include <cstdlib>


Class::Class(std::string text) {
    classArray = NULL;
    value = text;
    size = 0;
}

Class::Class(const Class& orig) {/*...*/}

Class::~Class() {}

Class::Class(std::string text, Class * name, int size){
    value = text;
    this->size = size;
    if(size == 1)
        classArray = name;
    else{
        int i;
        classArray = (Class*)malloc(size*sizeof(Class));
        for(i = 0; i < size; i++){
            classArray[i] = name[i];
        }
    }
}

std::string Class::toString(){
    return value;
}

void Class::addClass(Class * name, int size){
    int i;
    Class * tmp = (Class*)malloc((this->size+size)*sizeof(Class));
    for(i = 0; i < this->size-1; i++){
        tmp[i] = classArray[i];
    }
    if(size == 1)
        tmp[size-1] = name[0];//assignement method is the problem!!!??
    else{
        for(i = this->size; i < this->size+size-1; i++){
            tmp[i] = name[i];
        }
    }
    this->size += size;
    free(classArray);
    classArray = tmp;
}

Class Class::getClass(int index){
    return classArray[index];
}

test.c Listing

#include<iostream>
#include "Class.h"

using namespace std;

int main(int argc, char** argv) {

    Class * objectA = new Class("objectA");
    Class * objectB = new Class("objectB");

    cout << objectA->toString() << endl;

    objectA->addClass(objectB);
    //never gets here :'(
    cout << objectA->toString() << endl;

    return 0;
}

The problem is the test never gets past the objectA->addClass(objectB) instruction. I tried to debug and what I found was that the problem comes from the assignement instruction of the addClass() method. I also tried memcpy it didn't work. Does anyone have a solution for this please. Thanks.

4

3 回答 3

3

不要在 C++ 对象上使用 malloc,使用 new 和 new[] 以及 delete 和 delete[]。C++ 中 malloc 的问题在于它不调用对象的构造函数,而 free 不调用析构函数。new、new[]、delete 和 delete[] 都可以。你会因为你分配给未构造的对象而崩溃,你得到它是因为你没有使用 new。

并不是说这是您的代码的唯一问题,但这是显而易见的问题。

于 2013-09-28T22:08:15.087 回答
2

一个基本的解决方案是更喜欢newand 而delete不是mallocand free。更好的解决方案是使用标准容器,例如std::vector将元素保存在Class::addClass(). 让计算机负责所有的内存管理;您将节省大量的开发和调试时间。

于 2013-09-28T22:07:51.243 回答
1

请注意,在您的代码中,您Class(const Class&)为您的类定义了一个自定义复制构造函数,但您似乎没有定义复制赋值运算符 Class& operator=(const Class&)。请注意,在您的代码中,您使用复制分配 ( operator=) 来复制您的类,但您没有正确实现它。

此外,在 C++ 中,您应该更喜欢使用new[]/delete[]而不是 C 的malloc()/ free(),甚至更好地将std::vector容器用于数组。

您也可以有一个std::vector< SomeSmartPointer >(例如std::vector<std::shared_ptr<SomeClass>>,或std::vector<std::unique_ptr<SomeClass>>)。一般来说,考虑std::vector一些智能指针,但不要使用std::vector拥有原始指针(std::vector<SomeClass*>)。

于 2013-09-28T22:23:09.547 回答