473,785 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler error for "wrong" sized type

The size of a struct can be affected by compiler packing. Suppose you need it to be a
specific value for some reason (e.g., in firmware). How can you get the compiler to
generate an error for the wrong size rather than assert it at run-time? Here is one way,
but I don't know if it's guaranteed to work on any compiler:
1/(sizeof(struct my_struct) == correct_size);

For me, the above produces a compile-time divide-by-zero error for the wrong size. Is
there a better way?

DW
May 12 '06 #1
15 2034
David White said:
The size of a struct can be affected by compiler packing. Suppose you need
it to be a specific value for some reason (e.g., in firmware). How can you
get the compiler to generate an error for the wrong size rather than
assert it at run-time? Here is one way, but I don't know if it's
guaranteed to work on any compiler: 1/(sizeof(struct my_struct) ==
correct_size);

For me, the above produces a compile-time divide-by-zero error for the
wrong size. Is there a better way?


Well, I don't know about "better", but I like this:

char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 - 1];

If the struct is the wrong size, this will yield a negatively sized array,
which is illegal.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 12 '06 #2
David White posted:
The size of a struct can be affected by compiler packing. Suppose you
need it to be a specific value for some reason (e.g., in firmware). How
can you get the compiler to generate an error for the wrong size rather
than assert it at run-time? Here is one way, but I don't know if it's
guaranteed to work on any compiler: 1/(sizeof(struct my_struct) ==
correct_size);

For me, the above produces a compile-time divide-by-zero error for the
wrong size. Is there a better way?

DW

To eliminate the padding (i.e. store each member exactly where you want it),
you could use pointer trickery.

For example, instead of having:

typedef struct Monkey {
int i;
char b;
int j;
};

You could have the following. (It's not pretty, but you could clean it up
and give it a nice interface).
typedef char Monkey[ 2 * sizeof(int) + 1 ];
And then you have functions to access each member:

void SetI( Monkey* const m, int const val )
{
*( (int*)m ) = val;
}

int GetI( const Monkey* const m )
{
return *( (const int*)m );
}

void SetB( Monkey* const m, char const val )
{
*( (char*)m + sizeof(int) ) = val;
}

char GetB( const Monkey* const m )
{
return *( (const char*)m + sizeof(int) );
}

void SetJ( Monkey* const m, int const val )
{
*( (int*)( (char*)m + sizeof(int) + 1 ) ) = val;
}

int GetJ( const Monkey* const m )
{
return *( (const int*)( (const char*)m + sizeof(int) + 1 ) );
}
-Tomás
May 12 '06 #3
"Tomás" <NU**@NULL.NULL > writes:
[...]
To eliminate the padding (i.e. store each member exactly where you want it),
you could use pointer trickery.

For example, instead of having:

typedef struct Monkey {
int i;
char b;
int j;
};

You could have the following. (It's not pretty, but you could clean it up
and give it a nice interface).
typedef char Monkey[ 2 * sizeof(int) + 1 ];
And then you have functions to access each member: [snip] void SetJ( Monkey* const m, int const val )
{
*( (int*)( (char*)m + sizeof(int) + 1 ) ) = val;
}

int GetJ( const Monkey* const m )
{
return *( (const int*)( (const char*)m + sizeof(int) + 1 ) );
}


This access a misaligned int. On many systems, it will cause a trap.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 12 '06 #4

Tomás wrote(in reference to structure padding):

To eliminate the padding (i.e. store each member exactly where you want it),
you could use pointer trickery.


As I read that, the following thought actually fleetingly crossed my
mind:
"That's quite elegant, and eliminates all those annoying padding
problems. Why did C ever bother with the syntactic sugar of structures
and dereferencing members by name?"

I need psychiatric assistance. :)

May 12 '06 #5
Richard Heathfield wrote:
Well, I don't know about "better", but I like this:

char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 -
1];

If the struct is the wrong size, this will yield a negatively sized
array, which is illegal.


Thanks. That's an improvement. Unfortunately, our Hi-Tech compiler doesn't pick it up
because it treats an array size of -1 as unsigned!

DW

P.S. A colleague is terribly disappointed to learn that an array cannot have a negative
size. He thinks it should be allowed, with the valid index range extending from size+1 to
zero inclusive (e.g., -9 to 0 for a size of -10). I asked him what he would expect for a
sizeof of such an array, but he can't decide whether it should be positive or negative.
May 12 '06 #6
David White wrote:
with the
valid index range extending from size+1 to zero inclusive (e.g., -9
to 0 for a size of -10).


Make that: size to -1 inclusive (e.g., -10 to -1 for a size of -10).

DW
May 12 '06 #7
David White wrote:
The size of a struct can be affected by compiler packing. Suppose you need it to be a
specific value for some reason (e.g., in firmware). How can you get the compiler to
generate an error for the wrong size rather than assert it at run-time? Here is one way,
but I don't know if it's guaranteed to work on any compiler:
1/(sizeof(struct my_struct) == correct_size);

For me, the above produces a compile-time divide-by-zero error for the wrong size. Is
there a better way?


normally I speak against the Evil of "#prgama pack" and its ilk. Many
compilers
have extensions that tell the compiler to remove packing from specified
structs.
Such extensions are inherently non-portable and you should be very wary
you
don't assign packed to unpacked or vice versa. But for access to
firmware
especially embedded systems I think it can be justified. Embedded
compilers
may be more likely to support such extensions.
--
Nick Keighley

May 12 '06 #8
David White said:
I asked him what he would expect for a sizeof of such an array, but
he can't decide whether it should be positive or negative.


The size of an array is equal to the number of elements times the size of
each element. Thus, even an array with completely negative indices (were
such an array legal) would have a positive size. Your plane doesn't
suddenly acquire negative weight when you fly across the equator. (It may,
however, flip upside down, depending on who wrote your fbws!)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 12 '06 #9
"Nick Keighley" <ni************ ******@hotmail. com> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .

normally I speak against the Evil of "#prgama pack" and its ilk. Many
compilers
have extensions that tell the compiler to remove packing from specified
structs.
Such extensions are inherently non-portable and you should be very wary
you
don't assign packed to unpacked or vice versa. But for access to
firmware
especially embedded systems I think it can be justified. Embedded
compilers
may be more likely to support such extensions.


I also direct my compiler to use zero padding if I need to create literally
millions of instances of a small struct, e.g., in a recursive puzzle-solving
algorithm that generates new states exponentially and the number needed is
unknown, and may exceed available memory. Undesirable as they may be, such
extensions are useful or necessary in some cases (as are other quite nasty
non-standard practices on occasion).

DW

May 12 '06 #10

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

Similar topics

1
4644
by: J. Muenchbourg | last post by:
I'm getting an "Arguments are of the wrong type, out of acceptable range, in conflicts with each other " error , pointing to the sql statement in this block: Dim rstime Set rstime = Server.CreateObject("ADODB.Recordset") rstime.Open "SELECT * FROM TimestarResorts where resortname='" & request.querystring("resortname") & "' and roomsize='" & request.querystring("roomsize") & "'", dsn, 1, 3
7
50955
by: Tim Gaunt | last post by:
Hi, I'm hoping that someone will be able to help me with this one, I'm developing an application which basically inserts a load of data into the database ready for validation at a later date, I wrote an INSERT statement that works fine however today I decided that as part of the application upgrade it should be return the record ID so I wrote the following which I admit is a little messy at places but it works. However when I added all...
15
7220
by: Erica | last post by:
I'm getting the following error: ADODB.Recordset error '800a0bb9' Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. /shop/results.asp, line 15 line 15 is the recordset open line. Can someone please help
1
2946
by: Jack | last post by:
Hi, I am trying to add a new record to a main page. This page is the processing page to a form. However, I am getting the following error message: Error Type: ADODB.Recordset (0x800A0BB9) Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. /gwisnewcon/test/successconfirmation_ori.asp, line 41
8
1472
by: the other john | last post by:
Here's my project info first... DB: Access 2000 server: IIS 2003 Here is the error I'm getting... ADODB.Recordset error '800a0bb9' Arguments are of the wrong type, are out of acceptable range, or are in
1
3593
by: iam247 | last post by:
Hi I have a web page which receives information from a form (using request.form) and also attempts to look at an Access query to read in recoeds to a variable named rsGroup. When I have the following line commented, so it does not activate, I can successfully print all of the response.write's below: rsGroup.Open strSQL, adoCon
1
12595
by: Pradeep83 | last post by:
Hi All, Good Morning Here I am jotdowning my problem I am compiling a .c file in Free BSD 6.0 and exceuting there itself. and i have taken that a.out file to properiarty routing operating system which is varaint of Free BSD , when i excute that a.out file there uisng the command ./a.out its displaying following error message.. " Exec format error. Wrong Architecture "
4
2041
by: ravi | last post by:
Hi all, I written and compiled a c++ program using g++ no errors or warning are reported. But when I run it , reporting an error : ERROR: Wrong magic number. What is the reason for this error? anybody had an idea ?
5
7131
by: Sheldon | last post by:
Hi Everyone, I have defined a function: struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct Transient retv); and in the code: struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
1
3554
by: thecubemonkey | last post by:
Hi everyone, I'm getting the following error: ADODB.Recordset error '800a0bb9' Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. /newsite/faq.asp, line 57 Can you look at the code below and let me know if the problem is my
0
9484
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
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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
10097
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
9957
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
8983
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
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2887
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.