long doesn't equal another long in C++ -


it never goes in if statment when 0 == 0 sizeof() of both function return long , regular long both 8, don't know else wrong here.

declarations:

long                        remotestep; // next packet number to-be-processed  long getlong(byte * message, const short offset) {   // long char *     return *(long*)&(message[offset]); } 

debug code:

printf("id = %d  remotestep = %d \n", getlong(packet->message, 2), remotestep); printf("id = %d  remotestep = %d \n", getlong(packet->message, 2), remotestep); printf("id = %d  remotestep = %d \n", getlong(packet->message, 2), remotestep); printf("equals = %d \n", getlong(packet->message, 2) == remotestep); printf("sizeof = %d - %d\n", sizeof(getlong(packet->message, 2)), sizeof(remotestep));         // process if expected         if (getlong(packet->message, 2) == remotestep)         {             printf("in.............\n");             ...         } 

output information:

id = 0  remotestep = 0 id = 0  remotestep = 0 id = 0  remotestep = 0 equals = 0 sizeof = 8 - 8 id = 1  remotestep = 0 id = 1  remotestep = 0 id = 1  remotestep = 0 equals = 0 sizeof = 8 - 8 

i compiled under compat-gcc-34-c++ aka g++34, can't use newer g++ compilers give warnings , errors.

with -wall -wextra

declares.h:1880: warning: int format, different type arg (arg 2) declares.h:1880: warning: int format, different type arg (arg 3) declares.h:1880: warning: unknown conversion type character 0x20 in format declares.h:1880: warning: unknown conversion type character 0x20 in format declares.h:1880: warning: many arguments format declares.h:1881: warning: unknown conversion type character 0x20 in format declares.h:1881: warning: unknown conversion type character 0x20 in format declares.h:1881: warning: many arguments format declares.h:1882: warning: unknown conversion type character 0x20 in format declares.h:1882: warning: unknown conversion type character 0x20 in format declares.h:1882: warning: many arguments format declares.h:1884: warning: int format, different type arg (arg 2) declares.h:1884: warning: int format, different type arg (arg 3) 

line 1880 1 of

printf("id = %l  remotestep = %l \n", getlong(packet->message, 2), remotestep); 

the long type should not printed %d, explicitly undefined in specification. example,

each conversion specification introduced '%' character ...
...
if conversion specification not match 1 of above forms, behavior undefined. if argument not correct type corresponding conversion specification, behavior undefined. http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html

(the documentation applies printf fprintf)


thus cannot rely on output of,

printf("id = %d  remotestep = %d \n", getlong(packet->message, 2), remotestep); 

to determine that

printf("equals = %d \n", getlong(packet->message, 2) == remotestep); //output: equals = 0 

is in fact incorrect, , need fix debug statements first:

printf("id = %ld  remotestep = %ld \n", getlong(packet->message, 2), remotestep); 

in case preferable use memcpy instead of pointer cast,

long getlong(byte * message, const short offset) {        long result;     std::memcpy(&result, &(message[offset]), sizeof(long));      return result; } 

since long* can have stricter alignment requirement on platform char*.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -