Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I encountered such code:

for(int i=0;i<count;i++)
CustomStruct obj;                memcpy(&obj,Variable[index].get_blob().data(sizeof(CustomStruct )*i),sizeof(CustomStruct ));


as I understood memcpy part here, is basically doing a binary copy of CustomStruct data structures (starting at index sizeof(CustomStruct )*i) to the obj variable right?
Updated 2-Jul-13 3:01am

1 solution

Yes, it is just doing such a binary copy.
Share this answer
Dan page 2-Jul-13 9:20am    
hi, thanks also actually there is another trick in the code. like: `memcpy(&obj,Variable[index].get_blob().data(sizeof(OtherCustomStruct )*i),sizeof(OtherCustomStruct ));` obj is again of type CustomStruct, however OtherCustomStruct is basically similar as CustomStruct just they differ on one element, OtherCustomStruct seems to be kind of subset of CustomStruct. And the copy is still working, seems it should be like that right?
JackDingler 2-Jul-13 12:31pm    
memcpy() doesn't care what the data means.

It simply copies a range of bytes from one location to another.
CPallini 2-Jul-13 14:24pm    
It looks a (an ugly) trick, basically a copy of the CustomStruct part of OtherCustomStruct. It works, provided OtherCustomStruct 'extends' (that is contains as first memory block) CustomStruct.
[update]It is the other way around, as correctly pointed out by the OP[/update]
Dan page 2-Jul-13 17:04pm    
Hi CPallini, thanks, just as I remember it was other way around, meaning in my example, say CustomStruct contains four elements first three int-s and one char. Then, OtherCustomStruct would contain just those first three int-s.
CPallini 2-Jul-13 17:15pm    
You are right (and my previous remark was wrong: I overlooked the third parameter of memcpy (I wrongly assumed it was sizeof(CustomStruct)). It should work, setting just the first three ints of CustomStruct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900