473,394 Members | 1,960 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,394 software developers and data experts.

Structure sizes differ in C and C++?

I am attempting to write a program which passes an array struct from an ANSI C environment into a C++ DLL. The struct is defined as this:

Expand|Select|Wrap|Line Numbers
  1. struct device
  2. {
  3.    int ID;
  4.    char name[25];
  5. };
  6.  
  7. struct device myDevices[100];
  8.  
  9.  
I was attempting to update the structure in the C++ DLL and somehow the pointer gets off and so i have incomplete data. To my surprise this struct is sizeof 29 in C++ and 32 in C. Could this be my problem? Why are they different sizes if they are the same struct type? If this is my issue, how do i resolve it?

Confused,
Craig
Dec 7 '07 #1
3 1257
weaknessforcats
9,208 Expert Mod 8TB
I just did a sizeof your struct and compiled as C and then as C++ and got 32 both times. Are you using different compilers for C and C++??

I would expect 32 since that keeps everything aligned on a word boundary. There would be three slack bytes after the array. However, different compilers may align differently.

I hope you didnt do sizeof(var.Id) + sizeof(var.name) because that will give you 29.
Dec 7 '07 #2
I fixed my issue. .

I declared my int as a short. This made both struct sizes equal to 27 and allowed me to get the desired output.

FYI, I was using National Instruments CVI (ANSI C) and Visual Studio 2005 for C++. Still not sure why my C++ did not conform to a 32 bit word boundary but maybe its because C works on word boundaries and C++ does not? There seems to be some dissent on this topic among these forums. (see http://www.thescripts.com/forum/thread163278.html )

Thank you,
Craig
Dec 7 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Word alignment is compiler design and not part of the C ot C++ language.

Alignment on workd boundaries is more efficient and all major compilers do this. However, it is compiler depenedent.
Dec 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
6
by: MSReddy | last post by:
Hi Group, I have a structure than has other structures in it. I added 3 members to a strucuture and my program started behaving very strangly. When I printed sizes of the strucutures the values...
13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
8
by: John Dann | last post by:
Trying to declare a structure that will contain a couple of fixed-size arrays. I'm trying eg: Structure IndexRecord Dim testarray(16) as Byte etc End Structure But vb.net is refusing to let...
25
by: gamehack | last post by:
Hi all, I'm writing an application which will be opening binary files with definite sizes for integers(2 byte integers and 4 byte longs). Is there a portable way in being sure that each integer...
20
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables...
45
by: Borked Pseudo Mailed | last post by:
I have this weird problem with the JPEG library that I am using. There is a function that is being called and one of the parameters being passed is the length of a structure. Inside this...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
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...

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.