Struct size

Started by
14 comments, last by osh 20 years, 4 months ago
If you were using c++, you could use a template function
Advertisement
quote:Original post by glassJAw
quote:Original post by Wormy Hellcar
The value of a sizeof is determined at runtime.

Are you sure? I thought the compiler handled sizeof()s (or at least most of them).


You are correct. All sizeof() is handled at compile-time.

Colin Jeanne | Invader''s Realm
Ok, let me give the obvious, easy answer. Pass in the size of the struct as a parameter to the function.

struct MyStruct{    ...};void MyFunction(void *whatevah, int size){    ...}Blah(){    MyStruct ms;    MyFunction(&ms, sizeof(ms));} 
quote:Original post by Invader X
quote:Original post by glassJAw
Are you sure? I thought the compiler handled sizeof()s (or at least most of them).


You are correct. All sizeof() is handled at compile-time.

In C++ all sizeof() expressions are handled at compile time. In C (as of the 1999 revision of the standard) some sizeof() expressions, those performed on VLAs, are handled at runtime.
quote:Original post by glassJAw
quote:Original post by Wormy Hellcar
The value of a sizeof is determined at runtime.

Are you sure? I thought the compiler handled sizeof()s (or at least most of them).

True (all of them, in C++), but I think what he meant is that the desired size (id est, the size of whatever structure is passed to the function) may not be determined until runtime, which is correct.
quote:Original post by Miserable
quote:Original post by glassJAw
quote:Original post by Wormy Hellcar
The value of a sizeof is determined at runtime.

Are you sure? I thought the compiler handled sizeof()s (or at least most of them).

True (all of them, in C++), but I think what he meant is that the desired size (id est, the size of whatever structure is passed to the function) may not be determined until runtime, which is correct.

Of course, but then all you have is a pointer to the data, and sizeof() can only give you the pointer size (in the function the data is being passed to, the function caller should ideally know the size of the struct).

This topic is closed to new replies.

Advertisement