문제

I'm trying to learn about pointers and first class objects in C++. I'm sure the problem exists in my pointer assignments or calls. I was wondering if someone could help me to better understand pointers as well as using static variables/methods.

FirstClass.h

#pragma once

class FirstClassObject {

public: FirstClassObject();
    FirstClassObject(int);
    FirstClassObject(int, FirstClassObject);
    static void next_attr();
    static int attribute;
    int num;
    FirstClassObject *buffer;
    FirstClassObject *next;

    ~FirstClassObject();
};

FirstClassObject.cpp

#include "FirstClass.h"
#include <stdlib.h>
#include <string>

using namespace std;

FirstClassObject::FirstClassObject(){
num = attribute;
next_attr();
};

FirstClassObject::FirstClassObject(int attr){
    num = attr;
    next_attr();
}

FirstClassObject::FirstClassObject(int attr, FirstClassObject object){
    num = attr;
    next_attr();
    buffer = (FirstClassObject*) malloc(5);
    memcpy(buffer,&object,1);
    next = buffer;
}

void FirstClassObject::next_attr(){
    attribute++;
}

FirstClassObject::~FirstClassObject(){
    free(buffer);
    free(next);
}

FirstClassObject_test.cpp

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

using namespace std;

int FirstClassObject::attribute = 0;

FirstClassObject get_next_object(FirstClassObject object){
    FirstClassObject next_object;
    next_object.buffer = object.next;
    return next_object;
}

int main(){

    FirstClassObject object;
    FirstClassObject otherobject(4, object);

    cout << get_next_object(otherobject).num << "these numbers should be the same " << object.num << '\n';

    return 0;
}

Thanks in advance.

도움이 되었습니까?

해결책

First and foremost, this is wrong:

buffer = (FirstClassObject*) malloc(5);
memcpy(buffer,&object,1);

malloc() is not the same as new[].

Your FirstClassObject type is a non-POD type since it has a non-trivial destructor. This means you cannot properly construct it using malloc(). All malloc() does is allocate memory, and that's it. You need to actually construct a FirstClassObject object, and to do that using dynamically, you use new[ ].

Secondly, malloc() requires the number of bytes to allocate. What is sizeof(FirstClassObject)? I bet it isn't 5 (the argument you gave to malloc()). But the main point is that even if you gave malloc() the correct number of bytes, you aren't properly constructing your objects by using it.

Third, because FirstClassObject is non-POD, usage of memcpy() is also not good. In short, memcpy() does not copy objects. To copy an object, you invoke the copy constructor.

It looks like you're reading C language books and/or reading C language resources, not C++ books and resources. If you are, put down the C for a bit and learn C++ from the proper sources. If you attempt to mix C with C++ (without the proper experience), you wind up with issues such as your example.

다른 팁

First:

buffer = (FirstClassObject*) malloc(5)

This allocates a buffer of size 5, not 5 FirstClassObject. To do this, you need to do something like:

buffer = (FirstClassObject*) malloc(5*sizeof(FirstClassObject));

Second:

memcpy(buffer,&object,1);

This copies only 1 byte out of your object struct. Not a complete object. Here, sizeof is your friend as well although it is dangerous as receiving buffer is not a fully constructed object.

Then this line:

next_object.buffer = object.next;

would do member to member copy(providing it compiled as you are trying to affect an object to a pointer) effectively writing pass the boundary of your previously under-allocated buffer giving you the assert.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top