2
Is it possible to clone a structure pointer? Follow the code:
typedef struct{
int numero;
}estrutura;
int main(int argc, char *argv[]){
estrutura *i = (estrutura *)malloc(sizeof(estrutura));
estrutura *d;
i->numero = 5;
printf("%d", i->numero);
}
I want the structure *d
for example be the copy of the structure *i
, so that if I change the value of i->numero
, does not interfere with d->numero
.
I did it this way, but I didn’t allocate memory to *d, I did the direct memcpy, it could cause some problem ?
– Pierre Campos
Where did you copy the object then?
– Maniero
I did the following, memcpy(d, i, sizeof(structure));, without using malloc in *d
– Pierre Campos
That doesn’t answer the question I asked.
– Maniero
From what I understand, when I used memcpy, I copied what was in the structure i to d. But you asked me to do a malloc on the structure d before using the memcpy command. I made the memcpy without making this malloc for structure d and it worked, I wonder if it can cause any problem ?
– Pierre Campos
You still haven’t answered the question I asked. What is the location of the memory where this object was copied?
– Maniero
Sorry I don’t know if I understood your question correctly, it would be in the heap ?
– Pierre Campos
And how can you put an object in heap? Or else, where the heap?
– Maniero
Making the allocation, right ?
– Pierre Campos
Exactly! And if you don’t make the allocation you go where?
– Maniero