Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

osh

Struct size

This topic is 5421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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); };
};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ACTUALLY IT''S HANDLED BY GODZILLA YOU FAGHAT NOOB.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!