0

I have a piece of code and its generating different output when run on centOS and RHEL systems and both are little-endian machines. RHEL: result is 0 Cent OS: result is -4

#include <stdio.h>
#include <string.h>

typedef unsigned short U16;
typedef unsigned char U8; 
int main(void)
{
    U16 num1 = 1;
    U16 num2 = 5;
    short int ret;

    U8* s1 = (U8*)&num1;
    U8* s2 = (U8*)&num2;
    ret = memcmp((void*)s1, (void*)s2, (size_t) sizeof(U16));
    printf("result is %d\n", ret);
    return 0;
}

In both cases I am expecting output non-zero but its not. Also If I change 'ret' type to 'int' from 'short int' then output is non-zero for both cases. So, what's wrong when 'ret' declared as 'short int' ?

user3899508
  • 35
  • 2
  • 7
  • 1. Tag your question with a programming language. 2. Show the exact command line you used for compiling. If using an IDE, then reproduce the problem using the command-line toolset first. – Mike Nakis Apr 12 '23 at 10:07
  • onlinegdb.com gives -4. – Mike Nakis Apr 12 '23 at 10:12
  • programiz.com gives -4. – Mike Nakis Apr 12 '23 at 10:15
  • ideone.com gives -4. – Mike Nakis Apr 12 '23 at 10:17
  • compiled both as .c and .cpp for both gcc and g++ compilers # g++ sample.cpp #./a.out result is -262144 # gcc sample.c #./a.out result is -262144 – user3899508 Apr 12 '23 at 17:29
  • and did you run the exact same binaries on each machine, or did you recompile them on each machine? – Mike Nakis Apr 12 '23 at 22:46
  • recompiled on each machine separately. One machine installed with CentOS and another one with RHEL. Output on CentOS is -4 which is expected but on RHEL it's -262144 ( 0xfffc0000) which is strange and how to interpret this result ? – user3899508 Apr 13 '23 at 03:17
  • I revised my above comment: recompiled on each machine separately. One machine installed with CentOS and another one with RHEL. Output on CentOS is -4 which is expected but on RHEL it's 0 but if I change ret to 'int' then the output is -262144 ( 0xfffc0000) which is strange and how to interpret this result ? – user3899508 Apr 13 '23 at 03:27

0 Answers0