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
15 2035
David White wrote: 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.
Wow! You're a colleague of Paul Dirac?
Hmmm: Dirac's ideas could revolutionize memory management in
C-- (or would it be --C?). If "no memory at all" spontaneously
split into a chunk of allocated memory and a corresponding chunk
of unallocated anti-memory, a program could store data in the
allocated area without needing to call malloc(). Of course, one
would need to be careful: pointer aliasing could cause the memory
and anti-memory to come together and undergo mutual annihilation,
with the resulting, er, Gamma ray constituting an implementation-
defined signal. The task of keeping the allocation separated
from the anti-allocation is probably best left to Maxwell's Nasal
Demon.
--
Eric Sosman es*****@acm-dot-org.invalid
--
Eric Sosman es*****@acm-dot-org.invalid
David White wrote:
[...] 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. http://astronomy.swin.edu.au/~pbourk...ry/minus1.html
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
David White wrote: 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!
does it allow multiple case statements with the same value?
switch (0) {
case sizeof(struct my_struct) == correct_size:
case 0:
}
tedu <tu@zeitbombe.o rg> wrote: David White wrote: 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!
does it allow multiple case statements with the same value?
switch (0) { case sizeof(struct my_struct) == correct_size: case 0: }
Nit: a label reqires a following statement; make it:
switch (0) {
case sizeof(struct my_struct) == correct_size: ;
case 0: ;
}
(Otherwise it's a nice trick, I liked it.)
--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
S.Tobias wrote: tedu <tu@zeitbombe.o rg> wrote: David White wrote: 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!
does it allow multiple case statements with the same value?
switch (0) { case sizeof(struct my_struct) == correct_size: case 0: } Nit: a label reqires a following statement; make it: switch (0) { case sizeof(struct my_struct) == correct_size: ; case 0: ; }
(Otherwise it's a nice trick, I liked it.)
"case 0: ;" is a statement, so the first semicolon isn't necessary. The
second indeed is though.
"tedu" <tu@zeitbombe.o rg> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. . does it allow multiple case statements with the same value?
I don't know. I'll find out next week.
switch (0) { case sizeof(struct my_struct) == correct_size: case 0: }
Good idea. Incidentally, I had a problem with this rule once. I allowed two
different characters in encoded text to have the same meaning, e.g.,
#define TOKEN_CHAR '#'
#define ALT_TOKEN_CHAR 't'
Since they have the same meaning, the switch that processed them looked like
this:
switch(ch)
{
....
case TOKEN_CHAR:
case ALT_TOKEN_CHAR:
/* process */
break;
....
}
But in some cases I didn't want to allow both characters, so I did this:
#define TOKEN_CHAR '#'
#define ALT_TOKEN_CHAR '#'
The switch then wouldn't compile, but it could have, since there's no clash.
DW This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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
|
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
| |
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
|
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 "
|
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 ?
|
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
|
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
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |