Archived

This topic is now archived and is closed to further replies.

GekkoCube

the size in memset and memcpy

Recommended Posts

suppose i have an array, List[100]; is i use memset or memcpy, what exactly should the size be? i tried printing out the copied or cleared list and both of the following ways seem to do the trick.
memset(List, 0, 100);

memset(List, 0, sizeof(int) * 100);

Share this post


Link to post
Share on other sites
the second way is correct.

If you''re using C++, you should probably not use memset. Use std::fill instead.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
the second way is correct.



This is an even better way because you don''t have to keep track of the size of the array or the type of the elements.

memset( List, 0, sizeof( List ) );

Share this post


Link to post
Share on other sites
quote:
Original post by GekkoCube
why do you say that I should not use memset, but rather std::fill?

because i still use sprintf, printf, and memcpy, memset...etc


memset is <anti-flame> often </anti-flame> implemented as a simple loop assigning the value to each byte.
std::fill is a template, so the compiler will automatically use the right type, and you can use values other than bytes (objects, for example).

Share this post


Link to post
Share on other sites
quote:
memset is often implemented as a simple loop assigning the value to each byte.
std::fill is a template, so the compiler will automatically use the right type, and you can use values other than bytes (objects, for example).


... and if you have a list of classes with virtual functions memset() will overwrite the vtable pointer.

~nz

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by JohnBolton
This is an even better way because you don''t have to keep track of the size of the array or the type of the elements.

memset( List, 0, sizeof( List ) );


Except that it doesn''t work for dynamically allocated arrays.

Share this post


Link to post
Share on other sites