C: deep copy - void pointer structure

I have the following structure

struct teststruct
{
int *a;
void *data;      
};

Is it possible to make a deep copy of a structure that contains a pointer to a void? I guess I can’t say how many bytes pointer indicates data? Therefore, I cannot malloc specify the number of bytes and make memcpy. I'm right?

+3
source share
4 answers

No. Since you don't know the type that void * points to, a deep copy is out of the question.

In addition, you could not even make a deep copy a, since it can point to one of intthe array of them.

, C , , . :

struct teststruct {
    int a_sz;
    enum voidType vt;
    int *a;
    void *data;      
};

a_sz , , a, data, , d_sz.

, data , , :

typedef struct {
    enum voidType vt;
    union {
        int i;
        float f;
        double d;
    }
} tVoidType;
+8

, void *data, , .

+5

, , data. , data malloc() ed memory; - , .

Even if you can find out the size of the data, you still cannot know the structure of the internal data, which means that a proper “deep copy” will not be possible. A deep copy does not stop at the first depth of the pointer.

+5
source

You are right, but please distinguish: you can make a deep copy, but you cannot determine how much the byte pointer points to.

0
source

Source: https://habr.com/ru/post/1722769/


All Articles