473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Determining the size of a __gc struct

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[i]);
}
}

Thanks in advance
Michael


Nov 17 '05 #1
3 1573
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.
--------------------

Nov 17 '05 #2
Thank you very much for your workaround

-----Original Message-----
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 theSystem::Runtime::InteropServices::Marshal::SizeOf () method cannot return anunmanaged size of your dbheader.

So if you persist on using the struct dbheade as managed object, perhapsyour program may not count on the size of the dbheader, the followingcode 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) ofthe pDfheader
for(int i=0;i<3*IntSizei++) //Int occupies 4 bytes, unsigned occupies 4bytes
{
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.--------------------

.

Nov 17 '05 #3
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.
--------------------

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
2
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as...
12
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
1
by: Bern McCarty | last post by:
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked like this: __property System::UInt32 get_MyProperty(void) { System::Byte __pin * pinBytes = &m_byteArray; // entire...
2
by: microsoft | last post by:
Hi All, I am fairly new to Managed C++ extensions. I started trying to implement a interface defined in a C# project, in C++. The problem was with passing value arrays from c# to c++. I found...
1
by: Ian Lazarus | last post by:
Hello, I tried to confirm that two different heap allocation calls were being made, i.e., one for __gc and one for __nogc. However, the addresses of the calls (as seen in disassembly) are not...
5
by: kchui | last post by:
I have a C# interface as: public interface IBar{ void TestParam(ref StringCollection a, ref int b); } And I need to implment it in Managed C++, and I try to implement it as: public __gc...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
8
Hackles
by: Hackles | last post by:
Hello, Is it possible to retrieve the size of allocated memory from a pointer (more specifically, a pointer returned my malloc/realloc). This question may have already been asked countless times....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.