|
No, the right way would be to pass the addresses of the structures to the function. Passing a structure in a function call adds a lot of extra redundant code. So the more correct way would be:
static inline int struct_cmp(const air_t* left, const air_t* right)
{
return memcmp(left, right, sizeof(air_t));
}
static int some_function(const geometry_t* geometry)
{
if (struct_cmp(&geometry->part_type, &GEOM_TYPE_X) == 0)
return 1;
return 0;
}
Perhaps you could show the definition of the geometry opbject?
|
|
|
|
|
I have tried in this way:
if (struct_cmp(&geometry->part_type, &GEOM_TYPE_X) == 0)
no changes.
Here is the geometry object:
typedef struct geometry_struct geometry_t;
struct geometry_struct
{
char name[128];
char info[128];
unsigned long long org_offset;
unsigned long long sb_offset;
unsigned int sb_size;
air_t part_type;
unsigned int geom_type;
status_type_t status;
unsigned int order;
errcode_type_t errcode;
const fnct_t *air;
};
|
|
|
|
|
I get a different error message when I try. However by changing the define of GEOM_TYPE_X to the following, it seems to work:
const air_t GEOM_TYPE_X = {0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0}};
And also that is much better than using a #define .
|
|
|
|
|
Thank you so much Richard, seem to go now this issue ... now I have to solve the rest of 1000 errors 
|
|
|
|
|
Be careful when using memcmp() to compare structures. Due to alignment issues, you may have padding bytes between members e.g.
struct foo {
short s;
double d;
};
In 64-bit mode, I get 6 bytes of padding between foo.s and foo.d, in 32-bit mode it's 2. This means that memcmp() may not return 0 even when the members are equal.
|
|
|
|
|
Thank you for your notice, I will compile this project on 32 bit, that code is inherited from that old C project … do you suggest me another safe method to compare structs ? A little code sample will be great !
|
|
|
|
|
_Flaviu wrote: do you suggest me another safe method to compare structs ? A little code sample will be great ! How about something akin to:
struct foo
{
short s;
double d;
bool equals( struct foo f )
{
return (this->s == f.s) && (this->d == f.d);
}
};
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 1-Aug-19 10:32am.
|
|
|
|
|
David Crow wrote: bool equals( struct foo f )
{
// this is not the preferred method for comparing floating-point values
return (this->s == f.s) && (this->d == f.d);
}
I use fabs(v1 - v2) < delta . Its OK to compare a floating point value against zero, but other comparisons may produce unexpected results. e.g.
$ cat ex.c
#include <stdio.h>
int main()
{
double d = 0.0;
const double one = 1.0;
for(size_t i = 0; i < 100; ++i)
d += 0.01;
printf("d = %8.6f\n", d);
printf("d == 1.0 => %d\n", d == one);
printf("1.0 - d = %g\n", one -d);
return 0;
}
$ ./ex
d = 1.000000
d == 1.0 => 0
1.0 - d = -6.66134e-16
$
|
|
|
|
|
The only way I know is to compare member by member:
struct foo {
short s;
double d;
char str[24];
};
int compare_foo(const struct foo *f1, const struct foo *f2)
{
int retval;
if( (retval = f1->s - f2->s) != 0)
return retval;
if( (retval = f1->d - f2->d) != 0)
return retval;
return strcmp(f1->str, f2->str);
}
Note that this demonstrates another reason that you should avoid memcmp() on structs: if the struct in question contains strings, the portions of the string after the terminating null byte may not be equal, so strcmp(str1,str2) might not return the same value as memcmp(str1, str2, sizeof str1) . You could, use memcmp(str1, str2, strlen(s1)+1) : the extra byte accounting for the terminating null byte, so that you do not get a false equal on e.g. "help" and "helper". But that's a silly way to compare strings: you effectively run through str1 twice, once to get its length, then again to do the comparison, assuming str1 is equal to, or an initial substring of, str2. Use strcmp() to compare strings, or strcasecmp() or strcoll() when appropriate.
modified 1-Aug-19 10:35am.
|
|
|
|
|
But surely if both structs are in the same build the padding will be the same?
|
|
|
|
|
Sure, the size/alignment will be the same, but the devil is in the padding. It's uninitialised, and quite unlikely to have the same value in the two structs being compared, so memcmp() will fail even though all fields of the structs are identical.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
|
I have REST implemented in C#, I will need to call REST from existing C++ Legacy code.
What is the best way to do this. Please point me to some working example. URL Thank you
|
|
|
|
|
Call your C# from C++.
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Was there supposed to be a link under that message?
|
|
|
|
|
Here you go.
I respond based on the level of enthusiasm displayed.
c++ cli - Calling C# code from C++ - Stack Overflow
(I expected most to lose interest in the "call C#" part).
Then there's the "indirect" methods, like message queues, etc.
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
modified 25-Jul-19 16:47pm.
|
|
|
|
|
Thanks Gerry, but it's the OP who needs the information, not me. I merely wondered whether you had tried to post a link and something had gone wrong.
|
|
|
|
|
Yeah, but you're the one that responded.
Frankly, I thought calling your own code would be obvious as is "Google".
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Shouldn't you be asleep still? Or are you on the easternmost tip?
|
|
|
|
|
I had a nap after supper. Now I need to get sleepy again.
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Is there any method to call C code from VC++/MFC project ?
I need to call some functions from a C project (pretty big project). If I include some files from that C project, I get some error:
precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa)
moreover, I get a lot of unrecongnized types ... to modify types and functions in C project is huge work ... how can I overcome this ? Maybe you have been in the same situaton like me ...
|
|
|
|
|
Maybe that can help:
SO: Compile C files in C++ project which do not use precompiled header?[^]
Top-most answer presents some simple workarounds. If there are a lot of .c files in your project, disabling precompiled-headers solution-wide seems appropriate.
enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }
|
|
|
|
|
I saw that post, I have tried all that solutions, I still have the same error ...
|
|
|
|
|
Strange. Maybe there are some leftovers from previous compilation sessions. Did you try to clean the solution and rebuild it completely? Does the error appear for all .c source files, or only for specific one(s)?
enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }
|
|
|
|
|
It may possibly depend on how you are connecting the two. In general calling C functions from MFC/C++ presents no problems and works out of the box. Maybe if you showed some of the headers that you are including and the associated error messages we may be able to figure something out.
|
|
|
|
|