Development > Coding

memset(*this,0,sizeof....

(1/2) > >>

Duke:
Cppcheck complains: ERROR using memset on a class that has virtual functions (or similar).

Imho Cppcheck is wrong here. I don't know much about the internals of a compiler, but imho the compiler should keep the function table (and thus the virtuals) well apart from the instance data.

Can anyboy confirm/disconfirm ?

Mattn:
one should not use memset of a class with virtual methods - memset would override the vtable, too.

Duke:
then sizeof() must comprise the vtable (or at least a ptr to it). But it doesn't afaik.

Sandro:
Or overrun the next object in memory, since vtbl link in some compilers uses negative offsets. And I've did not start talking about multiple inheritance classes ...

Duke:
My point is that it simply doesn't make sense to store the vtable ptr with the data.
If the compiler did, an array of 10000 instances of class RainDrop would use up 40k bytes of memory where 4 bytes are enough.

The only exception to that is when you are using RTTI. Then it makes sense. Maybe that's where all the superstition comes from ?

Navigation

[0] Message Index

[#] Next page

Go to full version