Determining the size of a __gc struct

M

Michael Geier

Hello,

suppose the following structure:

__gc struct dfheader
{
int var1;
int var2;
unsigned var3;
};


How can I determine the size of the structure during
runtime?
The sizeof-operator does'nt work with gc-classes.

I need the size, because I want to do this:

{
__gc struct dfheader* pDfheader=new __gc struct
dfheader();
pDfheader->var1=1;
pDfheader->var2=2;
pDfheader->var3=3;

dfheader __pin* pData=pDfheader;
BYTE* pByteData=static_cast<BYTE*>(pData);

for(int i=0;i<sizeof(__gc struct dfheader);i++)
// sizeof generate error
{
pFileStreamObject->WriteByte(pByteData);
}
}

Thanks in advance
Michael
 
G

Gary Chang

Hi Michael

Thanks for you posting in the group!
How can I determine the size of the structure during
runtime?
The sizeof-operator does'nt work with gc-classes.

Yes, there is no general way to get a managed object's size, and also the
System::Runtime::InteropServices::Marshal::SizeOf() method cannot return an
unmanaged size of your dbheader.

So if you persist on using the struct dbheade as managed object, perhaps
your program may not count on the size of the dbheader, the following
code snippet will be a workaround(by manually trace the memory allocation):

const int IntSize = 4;
...
dfheader __pin* pData=pDfheader;
BYTE* pByteData=reinterpret_cast<BYTE*>(pData);

pByteData+=4; //ignore the first 4 bytes(System::Object reference) of
the pDfheader
for(int i=0;i<3*IntSizei++) //Int occupies 4 bytes, unsigned occupies 4
bytes
{
pFileStreamObjectfs->WriteByte(*pByteData++);
}
...


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
M

Michael Geier

Thank you very much for your workaround

-----Original Message-----
Hi Michael

Thanks for you posting in the group!


Yes, there is no general way to get a managed object's size, and also the
System::Runtime::InteropServices::Marshal::SizeOf() method cannot return an
unmanaged size of your dbheader.

So if you persist on using the struct dbheade as managed object, perhaps
your program may not count on the size of the dbheader, the following
code snippet will be a workaround(by manually trace the memory allocation):

const int IntSize = 4;
...
dfheader __pin* pData=pDfheader;
BYTE* pByteData=reinterpret_cast<BYTE*>(pData);

pByteData+=4; //ignore the first 4 bytes (System::Object reference) of
the pDfheader
for(int i=0;i<3*IntSizei++) //Int occupies 4 bytes, unsigned occupies 4
bytes
{
pFileStreamObjectfs->WriteByte(*pByteData++);
}
...


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gary Chang

Hi Michael,

Thanks for your quickly response!

I am glad to know it works for your problem.
If you have any more questions on your program, please feel free to post it
in the group. I am standing by to be of assistance.


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top