Advertisement

memcpy

Started by February 02, 2004 09:53 AM
12 comments, last by Tree Penguin 21 years, 1 month ago
Is this a proper way to copy the exact data of a memory block a pointer refers to? (and yes, i know i shouldn't use malloc ) p[0].tl=(unsigned int *)malloc(sizeof(o[0].tl)); memcpy(p[0].tl,o[0].tl,sizeof(o[0].tl)); Thanks EDIT: And another question, would it be valid to pass a pointer's address (instead of the pointer itself) and if so, how can i do that ? [edited by - Tree Penguin on February 2, 2004 11:02:02 AM]
Yes, this is possible and will work.
If you pass the pointers address you''re coping the pointer and not the data!

BTW you shouldn''t use malloc... :-P

--------------------------------------------------------

There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

There is another theory which states that this has already happened...
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Advertisement
Try using the STL copy because (you guessed it) it''s type-safe!
someSlightlyMoreUsefulNameThan_p[0].someSlightlyMoreUsefulNameThan_tl.resize(someSlightlyMoreUsefulNameThan_o[0].someSlightlyMoreUsefulNameThan_tl.size());std::copy(someSlightlyMoreUsefulNameThan_o[0].someSlightlyMoreUsefulNameThan_tl.begin(), someSlightlyMoreUsefulNameThan_o[0].someSlightlyMoreUsefulNameThan_tl.end(), someSlightlyMoreUsefulNameThan_p[0].someSlightlyMoreUsefulNameThan_tl.begin()); 


Assuming someSlightlyMoreUsefulNameThan_p[0].someSlightlyMoreUsefulNameThan_tl is a vector.

Enigma
One question, what's the problem if something's not type-safe if you know for sure no one will ever use it except me and i know the correct type is always passed.

EDIT: With the other question i meant this:
instead of passing a pointer, passing the memory address (4 byte address) the pointer points to.

[edited by - Tree Penguin on February 2, 2004 11:18:46 AM]
quote:
Original post by Tree Penguin
One question, what's the problem if something's not type-safe if you know for sure no one will ever use it except me and i know the correct type is always passed.




Because debugging is MUCH easier.

quote:
Original post by Tree Penguin

EDIT: With the other question i meant this:
instead of passing a pointer, passing the memory address (4 byte address) the pointer points to.




Do you mean this:

memcpy(&my_empty_obj, &my_obj, sizeof(my_obj_class));

This will work.

[EDIT]:
@Enigma
How do I use std:copy with own classes or simple memory like copying a block of floats?

--------------------------------------------------------

There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

There is another theory which states that this has already happened...

[edited by - Corrail on February 2, 2004 11:32:43 AM]
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Thanks but this:

quote:
Original post by CorrailDo you mean this:

memcpy(&my_empty_obj, &my_obj, sizeof(my_obj_class));


is not what i meant.

I mean this:

char *p=&somechar

Now p is a pointer to the memory address of somechar. This memory address is a value (like 0x00AB7DB0 or something). Is it possible to get that value, pass it to a function and create a pointer there that points to that address again?
Advertisement
What do you mean exactly? Post some code pls

--------------------------------------------------------

There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

There is another theory which states that this has already happened...
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Corrail:
Type sourceArray[size];Type destinationArray[size];std::copy(sourceArray, sourceArray + size, destinationArray); 

or, in general
std::copy(pointerToBeginningOfSource, pointerToEndOfSourcePlusOne, pointerToBeginningOfDestination); 


Tree Penguin:
quote:
Original post by Tree Penguin
One question, what''s the problem if something''s not type-safe if you know for sure no one will ever use it except me and i know the correct type is always passed.


There''s no problem in using them, there''s just no point. They offer no real advantages over the type safe versions and it''s much better to get out of the habit (or better yet to never get into the habit) of using them. That way when you do get to writing code other people may use you won''t accidently be littering it with unsafe code. Plus, if you never use malloc you never have to worry about when to use free verses delete.

On pointers, yes it''s perfectly valid to take a pointer to a pointer:
void allocate(int** pointer){	*pointer = new int[size];}int main(){	int* pointer;	allocate(&pointer);	pointer[size - 1] = something;} 

Although in this situation a reference would be better:
void allocate(int*& pointer){	pointer = new int[size];}int main(){	int* pointer;	allocate(pointer);	pointer[size - 1] = something;} 


Enigma
Thanks a lot, Enigma!
Didn''t know that there is a std function for this too.
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Thanks

This topic is closed to new replies.

Advertisement