调用localtime函数后,*邮票发生了什么?

时间:2019-02-22 02:19:17

标签: c pointers parameters

  • 问题:尚未访问指针,但其成员的值已更改
  • 环境

    • Linux发行版(uname -a):
    Linux parallels-Parallels-Virtual-Platform 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
    
    • gcc -v

      Using built-in specs.
      COLLECT_GCC=gcc
      COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
      OFFLOAD_TARGET_NAMES=nvptx-none
      OFFLOAD_TARGET_DEFAULT=1
      Target: x86_64-linux-gnu
      Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.3.0-27ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
      Thread model: posix
      gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)
      
    • cmake定义集:

      add_definitions(-std=c11 -g -D_DARWIN_C_SOURCE -Wall -Wformat=0 -O1 -D_XOPEN_SOURCE=700 -D_GNU_SOURCE)

  • 详细信息

    这是图章结构:

    typedef struct _zx_str {
      int len;    // data length
      int size;   // data allocted
      char *data;
    }zx_str;
    

    这是我的功能:

    void callfunc() {
      zx_str *stamp = calloc(1, sizeof(zx_str));
      stamp->data = calloc(1, 32+1);
      stamp->len  = 0;
      stamp->size = 33;
      stamp->data[0] = 0;
    
      zx_time_cur_timestamp(stamp);   
    }
    
    int zx_time_cur_timestamp(zx_str* restrict stamp, TM_FMT fmt) {
    
      if (stamp == NULL) {
        goto fail;
      }
    
      time_t t = time(NULL);
      struct tm *st;
    
      // before:*stamp = {len = 0, size = 33, data = 0x55555577bb90 ""}
      st = localtime(&t);
      // after: *stamp = {len = 93824994491248, size = 3, 
      // data = 0x555500544d4c <error: Cannot access memory at address 0x555500544d4c>}
    
    
      // ...
    }
    
  • 努力:

    1. 删除restrict个关键字
    2. 删除O1选项
    3. localtime替换为localtime_r
    4. 将代码推送到centos7服务器(而非虚拟机)后,没有发生

我想知道为什么会发生以及如何解决。有人可以帮助我吗?非常感谢!

0 个答案:

没有答案