Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. error C2143 error C2059

error C2143 error C2059

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpadobequestion
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Flaviu

    Yes, is thre complete structures, but I guess that is the right way:

    static inline int struct_cmp(const air_t left, const air_t right)
    {
    return memcmp(&left, &right, sizeof(air_t));
    }

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    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));
    }

    // and you would then call it by something like:

    static int some_function(const geometry_t* geometry)
    {
    // assuming that part_type is an offset rather than a pointer
    if (struct_cmp(&geometry->part_type, &GEOM_TYPE_X) == 0)
    return 1;

    return 0; 
    

    }

    Perhaps you could show the definition of the geometry opbject?

    _ 1 Reply Last reply
    0
    • L Lost User

      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));
      }

      // and you would then call it by something like:

      static int some_function(const geometry_t* geometry)
      {
      // assuming that part_type is an offset rather than a pointer
      if (struct_cmp(&geometry->part_type, &GEOM_TYPE_X) == 0)
      return 1;

      return 0; 
      

      }

      Perhaps you could show the definition of the geometry opbject?

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #5

      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;
      };
      
      L 1 Reply Last reply
      0
      • _ _Flaviu

        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;
        };
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        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.

        _ 1 Reply Last reply
        0
        • _ _Flaviu

          Yes, is thre complete structures, but I guess that is the right way:

          static inline int struct_cmp(const air_t left, const air_t right)
          {
          return memcmp(&left, &right, sizeof(air_t));
          }

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #7

          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.

          _ L 2 Replies Last reply
          0
          • K k5054

            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.

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #8

            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 !

            D K 2 Replies Last reply
            0
            • K k5054

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              But surely if both structs are in the same build the padding will be the same?

              P 1 Reply Last reply
              0
              • L Lost User

                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.

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #10

                Thank you so much Richard, seem to go now this issue ... now I have to solve the rest of 1000 errors :)

                1 Reply Last reply
                0
                • L Lost User

                  But surely if both structs are in the same build the padding will be the same?

                  P Offline
                  P Offline
                  Peter_in_2780
                  wrote on last edited by
                  #11

                  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

                  L 1 Reply Last reply
                  0
                  • P Peter_in_2780

                    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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #12

                    :thumbsup:

                    1 Reply Last reply
                    0
                    • _ _Flaviu

                      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 !

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #13

                      _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 )
                      {
                          // this is not the preferred method for comparing floating-point values (but that's not the point)
                          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

                      K 1 Reply Last reply
                      0
                      • _ _Flaviu

                        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 !

                        K Offline
                        K Offline
                        k5054
                        wrote on last edited by
                        #14

                        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.

                        1 Reply Last reply
                        0
                        • D David Crow

                          _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 )
                          {
                              // this is not the preferred method for comparing floating-point values (but that's not the point)
                              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

                          K Offline
                          K Offline
                          k5054
                          wrote on last edited by
                          #15

                          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 int main()
                          {
                          double d = 0.0;
                          const double one = 1.0;

                          for(size\_t i = 0; i < 100; ++i)
                              d += 0.01;
                          
                          // we would expect d now to be 1.0, but ...
                          
                          printf("d = %8.6f\\n", d);    // output looks like 1.000000
                          printf("d == 1.0 => %d\\n", d == one);  // but comparison fails
                          printf("1.0 - d = %g\\n", one -d);  // there's a very small diff btwn d and 1.0
                          
                          return 0;
                          

                          }
                          $ ./ex
                          d = 1.000000
                          d == 1.0 => 0
                          1.0 - d = -6.66134e-16
                          $

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups