@james and others,
First off, Yes, sorry, brain fart, should have been sizeof(FOO) not sizeof(dest) though sizeof(*dest) would do just as well.
Unfortunatly, obfucatsed by my fat fingers but the point about *using the compiler to associate sizes with pointers at compile time* holds true.
This implies that you would *know* if the assert(sizeof(FOO) >= sizeof(BAR)) is true at compile time meaning memcpy(dest,src,sizeof(*dest)) is allways safe provided dest/src are non-null.
That said, nothing about any language protects you from typo related bugs or copying data to the wrong place, so I must assume your comment about languages is an attempt to impune my work, in response I Fart In Your General Direction!
Yeah the src buffer too small issue is an edge case that has to be solved by context,
I think that limiting the write length with truncation as needed, and using declartive checking to avoid the short-copy issue is the way to go, YMMV.
And as for the various people pointing out sizeof(FOO) != sizeof(BAR), that may or may not be relevant but if i was assigning one FOO to another, structure assignment would be the method to use rather then memcpy.
The article refers specifically to a memcpy variant with the capacity for differing src/dest sizes so
the use of different types is implicitly a nod to the same techinque but portably done.
@Anonymous Coward Posted Friday 15th May 2009 12:10 GMT
Interestingly you both invoke efficency and scala in the same post, no contradiction there.
Tuck the little tin god efficency back in the box.
As far as flavour of the month languages go, I prefer ruby and erlang myself.
1) When your app is time critical, you don't use scala.
2) Offsets cause buffer overflows do they? Two words, Region Allocators.
here a link to maybe lift that cloud from your worldview scala boy.
From the linked article :
Region-based memory management offers several important potential advantages over garbage collection, including real-time performance, better data locality, and more efficient use of limited memory. Researchers have advocated the use of regions for functional, imperative, and object-oriented languages.