Struct size

Started by
14 comments, last by osh 20 years, 4 months ago
I need just a brief and fast answer, how to obtain unknow struct size which I passed to function as param (as void* or char* or so...). thx
Advertisement
search for sizeof() in your help files.
It doesn''t work... It doesn''t recognize right size.
C''mon! Does anyone know? The problem with sizeof() is this:

//size of struct is 6 resp. 8 bytes
struct X{
uint16 a;
uint16 b;
uint16 c;
};
a = 20;
b = 0;
c = 1;

void func(void* ptr) {
//sizeof say 1 because X.a is non-zero on 1st byte only
len = sizeof(*(char*)ptr);
}

Please!!!! I need urgent help
The value of a sizeof is determined at runtime.
If you really want to do this then derive from a size class that has a virtual size() function and use that to determine the size of the class.

struct class_size {    virtual int size() = 0;};class SomeClass : public class_size {...public:   int size() { return sizeof(*this); };}; 
I used to be indecisive, but now I''m not so sure.
quote:Original post by osh
void func(void* ptr) {  
//sizeof say 1 because X.a is non-zero on 1st byte only
len = sizeof(*(char*)ptr);
}


No. Sizeof says 1 because the size of a char is 1. Dereferencing a char* always gives a char, which has size 1. There is no simple way to do what you want to do, but then you shouldn''t need to. If a function needs to know the size of a buffer passed to it, make it take the buffer size as an argument.
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).
ACTUALLY IT''S HANDLED BY GODZILLA YOU FAGHAT NOOB.
Ok then, but I believe there is another way to archive my goal than inherance/RTTI, isn''t there?
All this I do because I don''t want to pass in the size as parameter.
quote:Original post by Anonymous Poster
ACTUALLY IT''S HANDLED BY GODZILLA YOU FAGHAT NOOB.

Thanks loser (mobile)

This topic is closed to new replies.

Advertisement