0

i want to serialize my class but i have two problem.

first, i'll show my class.

namespace CommonData
{
    enum DataType { SHPERICAL, COORDINATE, VIEW };

    class Data
    {
    public:
        virtual int serialize(char** param) = 0;

    protected:
        //every size is byte unit
        int initialize(char** param, int size);

        int fill(char** param, int start, int size, void* src);

    private:
        DataType _type;
    };

    class CoordinateData : public Data
    {
    public:
        int serialize(char** param) override;

    private:
        int _x, _y, _z;
    };
};

and this is cpp code

int Data::initialize(char** param, int size)
{
    *param = new char[size];
    cout << "size : " << size << endl;

    return fill(param, 0, sizeof(DataType), &_type);
}

int Data::fill(char** param, int start, int size, void* src)
{
    char* p = reinterpret_cast<char*>(src);

    int i = 0;
    while(i < size)
    {
        cout << "i : " << i << endl;
        cout << "*(p + i) : " << (int)*(p + i) << endl;
        *param[start + i] = *(p + i);
        cout << "*param[start + i] : " << (int)*param[start + i] << endl;
        i++;
        cout << "==============" << endl;
    }

    return start + size;
}

int CoordinateData::serialize(char** param)
{
    int end = 0;

    end = initialize(param, sizeof(CoordinateData));

    return end;
}

so if i start main code like this

int main()
{
    char* arr = nullptr;

    CommonData::CoordinateData _coor(1, 2, 3);

    int size = _coor.serialize(&arr);
}

the console output is like this.

size : 20
i : 0
*(p + i) : 1
*param[start + i] : 1
==============
i : 1
*(p + i) : 0
*param[start + i] : 0
==============
i : 2
*(p + i) : 0

my first question is why the size of CoordinateData class is 20? i was predicted it will be 16(enum DataType _type, int _x, _y, _z -> 4 * 4 = 16 byte)

and second is fill function isn't working. i was remove some print functions but when i check to access at *param[start + i = 2], it is possible with garbage value, but when i run main code, they finished like that.

i heard c++'s accessing raw memory address is possible at every code without warrant about program's safety. so it can't be throw out of memory error, and i thought that char* arr is heap memory so it wouldn't be access at memory of code or data area, but it just shut down. why this happened?

신승빈
  • 347
  • 3
  • 10
  • You must not use raw pointers for owned memory in C++. You should try to avoid ````new```` under all circumstances. If you "new", the you must "delete". Your design is not so good. Rethink. It will never work. If you get rid of the pointers, then it will be by far better. – A M Mar 29 '20 at 11:18
  • @ArminMontigny you mean i should not access raw memory with pointer? but is there any other way to reach to serializing data? "new" and "delete", i will edit to do it. thanks. and can you tell me what is the reason that sizeof(CoordinateData) is 20? – 신승빈 Mar 29 '20 at 11:25
  • 1
    First question: [sizeof class with int , function, virtual function in C++?](https://stackoverflow.com/questions/9439240/sizeof-class-with-int-function-virtual-function-in-c) – Yksisarvinen Mar 29 '20 at 11:26
  • @Yksisarvinen ah, thanks. i'm trying to understand it pertectly, but i can guess about it why this happen. – 신승빈 Mar 29 '20 at 11:38
  • @Yksisarvinen but can you tell me why the second question happened? is it problem about memory of virtual fuction? – 신승빈 Mar 29 '20 at 11:39
  • @신승빈 No idea, I never was a [two-star programmer](https://wiki.c2.com/?ThreeStarProgrammer). You get a [segmentation fault](https://wandbox.org/permlink/jAjhZq9S5NG7vNJW), which strongly suggests invalid memory access, but I don't know what is actually wrong there. – Yksisarvinen Mar 29 '20 at 11:48
  • @Yksisarvinen thank you for your advise. i will consider about segmentation fault. – 신승빈 Mar 29 '20 at 11:52

1 Answers1

1

operator[] has higher precedence then operator*.

In other words you need to do

(*param)[start + i] = *(p + i);
super
  • 12,335
  • 2
  • 19
  • 29