運行程序時報錯“Value too large for defined data type”


下列錯誤,可能是因為在64位上跑32位程序:

Value too large for defined data type

 

此錯誤對應的出錯代碼為EOVERFLOW,原因可能是目標文件超過2GB大小。

下列代碼可能會導致這個錯誤出錯(為何說是可能,本節最后部分解釋):

// g++ -g -o x x.cpp -m32

#include <errno.h>

#include <stdio.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

#include <string>

 

int main(int argc, char* argv[])

{

  struct stat st;

 

  if (stat(argv[1], &st) != 0)

  {

    printf("stat failed: %s.\n", strerror(errno));

    return 1;

  }

  else {

    printf("%zd\n", st.st_size);

    return 0;

  }

}

 

改成下列后,運行正常:

// g++ -g -o x x.cpp -m32

#include <errno.h>

#include <stdio.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

#include <string>

 

int main(int argc, char* argv[])

{

  struct stat64 st;

 

  if (stat64(argv[1], &st) != 0)

  {

    printf("stat failed: %s.\n", strerror(errno));

    return 1;

  }

  else {

    printf("%zd\n", st.st_size);

    return 0;

  }

}

 

前面說的可能”,是因為不同機器的編譯環境(可理解為默認編譯參數)可能並不相同,因此導致結果是可能,原因是宏“-D_FILE_OFFSET_BITS=64”會影響結果,如果定義了,則效果如同最后一段代碼,否則報錯“Value too large for defined data type”。相關宏:_LARGEFILE64_SOURCE__USE_FILE_OFFSET64,相關LIBC頭文件:features.h

一些引用到的第三方庫,可能定義了FILE_OFFSET_BITS,使用時需注意,比如:

# grep "FILE_OFFSET_BITS" /usr/include/*/*.h

/usr/include/bits/environments.h:#define __ILP32_OFFBIG_CFLAGS  "-m32 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"

/usr/include/mysql/my_config_x86_64.h:#define _FILE_OFFSET_BITS 64

/usr/include/python2.7/pyconfig-64.h:#define _FILE_OFFSET_BITS 64

/usr/include/python3.4m/pyconfig-64.h:#define _FILE_OFFSET_BITS 64

 

1:查看GCC默認機器相關編譯參數

gcc -march=native -c -Q --help=target

 

2:查看GCC默認定義的宏

gcc -posix -E -dM - </dev/null

 

或:

cpp -dM /dev/null


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM