skip to Main Content

Three environment, run the same code with wcsncmp in it, but the result is different.

ENv 1 mac(arm):

mingmingwanng@hostname TDengine % uname -a
Darwin hostname 20.4.0 Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:41 PDT 2021; root:xnu-7195.101.2~1/RELEASE_ARM64_T8101 arm64

mingmingwanng@hostname TDengine % cc --version
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Env 2 linux docker on arm Arch:

root@efe8ae4d51c6:/home/TDinternal/community# uname -a
Linux efe8ae4d51c6 4.15.0-70-generic #79-Ubuntu SMP Tue Nov 12 10:36:10 UTC 2019 aarch64 GNU/Linux

root@efe8ae4d51c6:/# gcc --version
gcc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Env 3 linux:

root@573cd1810acf:/home/TDinternal# uname -a
Linux 573cd1810acf 5.11.0-43-generic #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021 x86_64 GNU/Linux

root@573cd1810acf:/home/TDinternal# gcc --version
gcc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The source code:

#include <wchar.h>
#include <stdio.h>
 
int main(){
    wchar_t w1="试试";
    long long w2 = 0xFFFFFFFF;
    int ret = wcsncmp((wchar_t *)&w2, &w1, 1);
    printf("ret:%dn", ret);
    return 0;
}

The result:

Env 1: ret: -75956121
Env 2: ret:1
Env 3: ret:-1

I have found why the result is -75956121 in environment 1 form the source code:https://github.com/apple-open-source/macos/blob/master/Libc/string/FreeBSD/wcsncmp.c.

but why the result is so big different in environment 2 and environment 3 ?

2

Answers


  1. Chosen as BEST ANSWER

    As the pictures below. wchat_t is unsigned, so 0xFFFFFFFF = 4294967295 in Env 2. where in env 3, wchat_t is signed, so 0xFFFFFFFF = -1.

    enter image description here

    enter image description here


  2. Poorly constructed code

    wchar_t w1="试试"; takes the address of the string "试试" and assigns that to w1.

    No reason to expect the same address in different compilations nor executions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search