[fpc-other] Microsoft to ban Memcpy() :)

Jonas Maebe jonas.maebe at elis.ugent.be
Sat May 16 12:56:49 CEST 2009


On 16 May 2009, at 12:44, Vinzent Höfler wrote:

> "Vinzent Höfler" <JeLlyFish.software at gmx.net>:
>
>> Time will tell, if memcpy_s() is actually "safer". If the programmer
>> didn't think about the destination buffer's size before, why should  
>> he
>> now? :->

Because the API forces him to think about it now.

> The C(++)-folks fix:
>
> -- 8< --
> inline void memcpy_s(void *dest, size_t sizeInBytes, const void  
> *src, size_t count)
> {
> if (count > sizeInBytes)
> throw InvalidArgument("memcpy_s: buffer overflow");
> memcpy(dest, src, count);
> }
> -- 8< --
>
> LMAO. Adding unnecessary bounds checking on each single call.

I don't think it's unnecessary. Moreover, in the trivial cases (where  
you wouldn't use a move() in Pascal) the check will be optimized away  
(since all size-related arguments will be constants), and in most  
other cases the time spent on actually copying will probably dwarf any  
overhead added by that extra check.

> So much for the speed advantage of a low-level language. A pascal  
> compiler knows the correct sizes beforehand, no need for extra  
> bounds checking...

A Pascal compiler has no idea about any actual variable sizes inside  
the move() procedure.


Jonas


More information about the fpc-other mailing list