473,770 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct size error

hi,
i have a struct in c# that is containing Int32 member and Double
member.
when i try to get the size of it i get size of 16 bytes, while i was
expecting to receive 12. (Int32- 4 bytes, Double - 8 bytes)

does anyone have any ideas why it occur?

May 7 '07 #1
2 1560
<gu**********@g ooglemail.comwr ote in message
news:11******** **************@ w5g2000hsg.goog legroups.com...
hi,
i have a struct in c# that is containing Int32 member and Double
member.
when i try to get the size of it i get size of 16 bytes, while i was
expecting to receive 12. (Int32- 4 bytes, Double - 8 bytes)

does anyone have any ideas why it occur?
From what I can tell dot net pads the size of a struct to be a multiple of
the largest data type. A struct like this

struct x
{
public int16 a;
public byte b;
}

will be 4 bytes and this:

struct y
{
public int32 a;
public byte b;
}

will be 8. And yours is 16. The order does not seem to be important. This
would make sense so that structs stored in arrays align to certain
boundaries.

Michael
May 7 '07 #2
<gu**********@g ooglemail.comwr ote in message
news:11******** **************@ w5g2000hsg.goog legroups.com...
hi,
i have a struct in c# that is containing Int32 member and Double
member.
when i try to get the size of it i get size of 16 bytes, while i was
expecting to receive 12. (Int32- 4 bytes, Double - 8 bytes)

does anyone have any ideas why it occur?
The size given is the size of the struct as it would be marshaled to
unmanaged code, this is not necessarily the size on the stack.

Per default, struct elements are aligned at their natural bounds in memory,
your struct could for instance look like this on the stack (32 bit OS) :

address type
0x120104 int32
0x120108 double

occupying 12 bytes, both elements are aligned at their natural bounds, but
it could also look like this:

address type
0x120108 int32
0x120110 double

and as such, occupy 16 bytes. The marshaled struct however, will per default
get laid-out like this:

0x-----0 int32
0x-----8 double

so here the size is 16 due to alignment of the double element at his natural
bound (a multiple of sizeof(double)) .
You can control the way structs are aligned and packed(when marshaled), by
means of the StructLayout attribute Pack or by applying an explicit layout
(LayoutKind.Exp licit).

Willy.

May 7 '07 #3

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

Similar topics

14
2567
by: Gunnar G | last post by:
Hello. My compiler (GCC 3.3.*) does not complain about the following: #include <iostream> #include <vector> using namespace std; struct X{ int a,b,c; vector<X> pp;
19
9237
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
6
2692
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is completed, for all declarations of that type, by ^^^ # declaring the same structure or union tag with its defining # content later in the same scope. ^^^^^ (6.2.5#23)
8
1681
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the missing fields and arrays overlap. What's shown here, though, is sufficient for explaining the error. 290 bytes of data come from a serial device and is to be placed in this struct. Hence, I want this struct to be 290 bytes in size, and, if I'm...
0
8773
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR pchMessage; // decoded message data TCHAR chCodeID; // AIM Id of symbology TCHAR chSymLetter; // HHP Id of symbology
7
5965
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR pchMessage; // decoded message data TCHAR chCodeID; // AIM Id of symbology TCHAR chSymLetter; // HHP Id of symbology
3
4842
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack str size does not match format I thought ip addresses were unsigned 32 bit integers.
9
6535
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A structure is defined in the header(.h file) as shown below struct validationResult {
1
2761
by: recherche | last post by:
Hola! I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain. Please help! Code Snippet 1:
6
5185
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t = malloc(n*sizeof(r)) and (to a degree) by the surrounding error checking.
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.