在 64 位和 32 位系统上运行程序

发布于 2024-11-15 12:29:40 字数 833 浏览 1 评论 0原文

在 64 位系统上完美运行的 C++ 程序在 32 位系统上崩溃有什么原因吗?我有一个程序在两台服务器上测试。一个是 64 位,另一个是 32 位。该程序没有特定的命令。最初它在两者中都起作用,直到我做了一个改变并添加了一个结构并调用它。当第一次调用这个结构体对象时,程序崩溃了。但是如果我在崩溃之前打印 1 行元素的值,这些值就在那里。顺便说一句,我的意思是整数,没有指针或其他有趣的东西。 我尝试将这些整数启动为 uint32_t 和此类实验。但却遇到了死胡同。

结构是这样的

struct info {
    int id1, id2;
    string test;
};

map<string, info> allInfo
vector<string> temp;
/* temp populated */

info details = {atoi(temp[0].c_str()),atoi(temp[2].c_str()),temp[3].c_str()};
allInfo[temp[1].c_str()] = details; 

/*somewhere after this it is accessed */

map<string, info>::iterator i;
/* printing the values here seems ok.. */
cout << (*i).second.id1 << endl << (*i).second.id2 << endl;
string first_id = "idOne : " + (*i).second.id1; 
string second_id = "idTwo: " + (*i).second.id2;

Is there any reason why a c++ program that runs perfectly on a 64 bit system crashes on a 32 bit system? I have a program tested on 2 servers. one a 64bit and other 32bit. The program doesnt have bit specific commands. Initially it was working in both until i made a chage and added a structure and called it. The program crashed when this structure object was called for the first time. But if i print the value of the elements 1 line before the crash it, the values are there. Btw, the by value i mean integers and no pointers or other funny stuff.
I tried initiating these integers as uint32_t and such experiments. But to meet a dead end.

the structure is like this

struct info {
    int id1, id2;
    string test;
};

map<string, info> allInfo
vector<string> temp;
/* temp populated */

info details = {atoi(temp[0].c_str()),atoi(temp[2].c_str()),temp[3].c_str()};
allInfo[temp[1].c_str()] = details; 

/*somewhere after this it is accessed */

map<string, info>::iterator i;
/* printing the values here seems ok.. */
cout << (*i).second.id1 << endl << (*i).second.id2 << endl;
string first_id = "idOne : " + (*i).second.id1; 
string second_id = "idTwo: " + (*i).second.id2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红颜悴 2024-11-22 12:29:40

有很多事情可能会出错,如果没有调试器,找出问题将是一场噩梦。

选项 1:说服拥有服务器管理员权限的人安装 gdb

选项 2:在各处添加 print 语句,以准确找出在此处发布的段错误发生在哪一行(或者尝试自行找出)。

There's multiple things that could go wrong, and without a debugger it's going to be a nightmare to find out.

Option 1: Convince whoever has admin rights to the server to install gdb.

Option 2: Add print statements everywhere to figure out exactly which line the segfault occurs on a post it here (or try and figure it out from that on your own).

ζ澈沫 2024-11-22 12:29:40

一个简单的原因是未定义的行为。

A simple reason would be Undefined Behavior.

污味仙女 2024-11-22 12:29:40

只是一个猜测。你是否在 64 位机器上编译了它?
可能有一些操作码在 32 位机器上不可用。

Just a guess. Did you compile it on/for 64bit machines?
It may be that there is some opcode in there that is not available in 32bit machines.

桃酥萝莉 2024-11-22 12:29:40

因此,没有强制转换,没有动态分配,并且在 32 位构建中会崩溃,而在 64 位构建中则不会。我的第一个猜测是访问其范围之外的数组。

So no cast, no dynamic allocation and a crash in 32 bit build and not in 64 bit build. My first guess would be accessing an array outside their range.

月寒剑心 2024-11-22 12:29:40

您将整数添加到字符数组中:

string first_id = "idOne : " + (*i).second.id1; 
string second_id = "idTwo: " + (*i).second.id2;

可能是您的测试集不同,并且在非抛出系统上您意外地获得了分配给字符串变量的有效地址。

you're adding integers to char-arrays in:

string first_id = "idOne : " + (*i).second.id1; 
string second_id = "idTwo: " + (*i).second.id2;

may be you're test-sets differs and on the non-throwing system you accidently get a valid address to assign to the string variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文