前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >运行程序时报错“Value too large for defined data type”

运行程序时报错“Value too large for defined data type”

作者头像
一见
发布2020-02-13 16:35:36
1.8K0
发布2020-02-13 16:35:36
举报
文章被收录于专栏:蓝天蓝天

下列错误,可能是因为在64位上跑32位程序:

Value too large for defined data type

此错误对应的出错代码为EOVERFLOW,原因可能是目标文件超过2GB大小。

下列代码可能会导致这个错误出错(为何说是可能,本节最后部分解释):

// g++ -g -o x x.cpp -m32 #include #include #include #include #include #include #include   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 #include #include #include #include #include #include   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 -

或:

cpp -dM /dev/null

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档