View previous topic :: View next topic |
Author |
Message |
bryant@balancebitsconsult
Joined: 21 Nov 2023 Posts: 38
|
COPY_ARRAY Macro |
Posted: Thu Dec 07, 2023 6:45 pm |
|
|
So, there seems to be a COPY_ARRAY() macro. Undocumented ?
Anyone know what it does ? How it works ? I am running into it. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Dec 08, 2023 3:03 am |
|
|
I don't think it does.
I think you are including something that defines this, and are then trying
to define a second copy.
Possibly you have included a library from some other source that defines
this?.
I Just tried creating a macro with this name on a few different projects,
and none complain that I am redefining a name, and the macros all work
OK.
What is happening?.
What error are you getting?.
I seem to remember MicroChip having a copy_array function or macro in
one of their libraries. |
|
|
bryant@balancebitsconsult
Joined: 21 Nov 2023 Posts: 38
|
|
Posted: Fri Dec 08, 2023 10:54 am |
|
|
Ttelmah wrote: | I don't think it does.
I think you are including something that defines this, and are then trying
to define a second copy.
Possibly you have included a library from some other source that defines
this?.
I Just tried creating a macro with this name on a few different projects,
and none complain that I am redefining a name, and the macros all work
OK.
What is happening?.
What error are you getting?.
I seem to remember MicroChip having a copy_array function or macro in
one of their libraries. |
I have added it to a repo to examine. I was seeing errors when disabling case insensitivity at the top of util.h (link) : https://github.com/beadon/circbuf_demo/blob/main/util.h
Now, strangely, I am unable to reproduce the errors triggered by the case change! So, I will take further steps to reproduce and reply on this thread. I have a separate issue in defining the macro - it MIGHT be related --
errors are :
Code: |
Compiling C:\Users\beadon\Documents\GitHub\circbuf_demo\main on 08-Dec-23 at 08:49
*** Error 9 "C:\Users\beadon\Documents\GitHub\circbuf_demo\util.h" Line 95(31,37): Macro identifier requires parameters
*** Error 12 "C:\Users\beadon\Documents\GitHub\circbuf_demo\util.h" Line 95(37,38): Undefined identifier val
2 Errors, 0 Warnings.
Build Failed.
|
Which, at a glance, seems trivial to resolve. Once you look at the line though, there's no "val" and the error message becomes more puzzling.
Code: | #define unsigned_mult_overflows(a, b) ((a) && (b) > maximum_unsigned_value_of_type(a) / (a)) |
Any insights are appreciated. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Dec 08, 2023 11:29 am |
|
|
Two things.
The first is what is line 95 (and a couple of lines in front)?.
The second ties to the second part of the above. The compilers fault
identifier code has a habit of misidentifying problems. It goes wrong several
lines after the actual fault, when it can no longer carry on. So an
unidentified fault gets reported as a completely different fault lots later.
This is why sometimes using a C syntax scanner tool can be a way of
finding the real problem. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Dec 08, 2023 12:43 pm |
|
|
Change the word 'unsigned' in your value there to something like 'uns'.
unsigned is a keyword, and in general keywords should not be used like this.
Looking at it more. I wonder how maximum_unsigned_value_of_type is
coded?. My guess would be that it is a macro, that possibly uses sizeof
to read a value from an array containing the size limits for the integer
types. You have a significant danger when coding macros inside macros,
that you can take the resulting line length over the maximum supported
by the compiler.
What overflow are you actually trying to test?.
Code: |
//check whether a*x could overflow
// If one number is -1 and another is INT_MIN, multiplying them we get
// abs(INT_MIN) which is 1 higher than INT_MAX
if (a == -1 && x == INT_MIN) // `a * x` can overflow
if (x == -1 && a == INT_MIN) // `a * x` (or `a / x`) can overflow
// general case
if (x != 0 && a > INT_MAX / x) // `a * x` would overflow
if (x != 0 && a < INT_MIN / x) // `a * x` would underflow
|
Using your macro for the MAX, you should bracket round this since
otherwise the expansion may not be as you expect. |
|
|
|