Archived

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

Struct size

This topic is 5116 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
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
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
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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites