检测到 glibc,但我没有使用动态内存?

发布于 2024-11-02 17:14:18 字数 908 浏览 0 评论 0原文

嘿,大家。我正在用 C++ 编写一个十六进制计算器。这些操作必须“手动”完成,因为要求程序能够处理 40 位操作数(并且 16^40 比 long long 可以处理的大得多)。

不过,我遇到了一点问题。在我的 Mac (OS X 10.6) 上,我可以在终端中使用 Xcode 和 g++ 编译并运行良好。我什至有一个在 Windows 上使用 Dev C++ 的朋友说它运行得很好。然而,如果我通过 SSH 连接到学校的 Sun 集群,我会使用 g++ 并且应用程序会运行,但经过几次操作后它就会崩溃:

"*** glibc detected *** ./a.out: free(): invalid pointer: 0xb786e6f4 ***".

我猜 Sun 集群是我的教授使用的,因此我失去了分数。

我不确定为什么会发生这种情况,因为我没有明确使用内存分配,并且从我读到的有关此错误的所有内容来看,这通常(并非总是)是尝试删除不是用新内存分配的内容的结果操作员。我想知道这是否归功于我正在进行的来回字符串/字符转换,但它仍然令我困惑,它在某些情况下运行良好然后就死掉了。

基本上,该程序的作用是询问文件名,读取文件并根据需要完成的操作解释每个字符串,然后进行数学运算直到文件末尾。

这是我的代码: http://pastebin.com/1DW5pd6p

这是我正在使用的数据文件(再次注意我的程序在第二次操作后死掉了): http://pastebin.com/xx59eQsu

我已经修改了一些字符串与 char 数组,这似乎让我得到这两个操作,但我很困惑。谢谢大家的关注。感谢所有回复。

Hey, everyone. I'm writing a hexadecimal calculator in C++. The operations have to be done "by hand" because the requirement is for the program to be able to handle 40 digit operands (and 16^40 is much larger than a long long can handle).

I am having a bit of a problem, though. On my Mac (OS X 10.6) I can compile and run fine using both Xcode and g++ in the Terminal. I even had a friend on Windows using Dev C++ saying it runs fine. If I SSH to my school's Sun cluster, however, I g++ and the application runs, but it craps out after a few operations with:

"*** glibc detected *** ./a.out: free(): invalid pointer: 0xb786e6f4 ***".

I'm guessing the Sun cluster is what my professor used and thus why I lost points.

I'm not sure why this is occurring as I am not explicitly using memory allocation, and from all I've read on this error, it's usually (not always) the result of trying to delete something that wasn't made with the new operator. I'm wondering if it is thanks to the back-and-forth string/char conversions I am doing, but it still is puzzling to me that it runs fine for a few cases then dies.

Basically what this program does is ask for a filename, reads the file and interprets each string according to what needs to be done, and then does the math until end of file.

Here is my code: http://pastebin.com/1DW5pd6p

Here is the data file I was using (again note my program dies after the second operation): http://pastebin.com/xx59eQsu

I've already retrofitted some strings with char arrays and that seem to let me get those two operations in, but man am I confused. Thanks for the look, everyone. All responses are appreciated.

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

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

发布评论

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

评论(1

旧夏天 2024-11-09 17:14:18
string str;
char chr[1];
...
sprintf(chr, "%u", sum);

您溢出了 chr,这很可能会通过覆盖 std::string 使用的某些内部指针变量来影响 str。当字符串被破坏时,它会尝试删除这个无效的指针。

确保chr足够大 - 例如char chr[32];应该足以打印积分。

string str;
char chr[1];
...
sprintf(chr, "%u", sum);

You're overflowing chr, this most likely affects str by overwriting some internal pointer variable used by std::string. When the string is destructed, it tries to delete this invalid pointer.

Make sure chr is large enough - e.g. char chr[32]; should be plenty for printing your integrals.

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