Discussion:
Error! E1020: Dimension cannot be 0 or negative
(too old to reply)
Jens Staal
2014-11-07 09:30:32 UTC
Permalink
As I try to compile open source programs I encounter this issue from time to
time

Often in expressions like this one:

char BUG_header[sizeof(header) == 8 ? 1 : -1];

My usual "solution" is to temporarily wrap it in an #if defined __WATCOMC__
and change the "0" or "-1" to "1"

I have a strong feeling that this is a BAD THING and that there must be a
more appropriate way of handling these issues. Usually I have very little
understanding of the code that I am trying to compile.

Is there a flag to pass to owcc to make it accept 0 or negative values?
Hans-Bernhard Bröker
2014-11-07 14:48:44 UTC
Permalink
This post might be inappropriate. Click to display it.
Wilton Helm
2014-11-18 15:55:54 UTC
Permalink
For an added and slightly different perspective on the subject, I frequently
put similar code in some of my projects. I write embedded code and there
often are structures or arrays (often of structures) that are kept in
non-volatile memory. It isn't always possible to directly access the
non-volatile memory, so some allocation scheme and paging have to be done.
The data in the non-volatile memory is placed in known, fixed locations.

But the content of the structure may change over time. The number of
structures in an array may change. The size of arrays in the structure may
change. Those changes can be determined by taking sizeof of the structure
or array, but sizeof cannot be used with #if because of the order in which C
evaluates things. However sizeof can be used to compute the allocation of
an automatic array, which is done at compile time. Arranging it such that
the expression involving sizeof becomes negative if the structure exceeds
the amount of room allocated for it in non-volatile memory causes a compile
time error of I (or another programmer maintaining the code) change
something that makes it bigger and forget to increse the allocation. A
programmer can write comments all day, but sooner or later someone will
overlook them. It is good defensive practice to have a mechanism that makes
the program unable to compile if something like that gets overlooked.
Ideally symbolic constants would be used to insure that things track, but
that isn't always possible.

And yes, the technique has saved my neck a few times.

Wilton



---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

Continue reading on narkive:
Loading...