【问题标题】:Odd malloc crash奇怪的 malloc 崩溃
【发布时间】:2013-01-17 10:28:34
【问题描述】:

我有以下代码在过去几个月一直在工作,但最近有时开始崩溃(在多线程应用程序中运行时):

struct some_struct {
    char* m_str1;
    char* m_str2;
}

struct some_struct*
set_some_struct(const char* p_str1, const char* p_str2) {
    struct some_struct* some_struct_ptr = 
        (struct some_struct*)malloc(sizeof(struct some_struct));
    if (some_struct_ptr == NULL)
        printf("malloc failed!\n");

    size_t str1_len = strlen(p_str1) + 1;
    size_t str2_len = strlen(p_str2) + 1;

    some_struct_ptr->m_str1 = malloc(str1_len);
    if (some_struct_ptr->m_str1 == NULL)
        printf("malloc failed!\n");

    some_struct_ptr->m_str2 = malloc(str2_len); // Crashes here
    if (some_struct_ptr->m_str2 == NULL)
        printf("malloc failed!\n");

    strcpy(some_struct_ptr->m_str1, p_str1);
    strcpy(some_struct_ptr->m_str2, p_str2);

    return some_struct_ptr;
}

运行它给我“0x7c81bb52”处的指令引用了“0x00000002”处的内存。无法“读取”内存。”

上面的代码有什么明显的错误,在某些情况下可能会出现异常吗?如果我在测试程序中单独运行该函数,它工作得很好,但在完整的应用程序中运行时它总是崩溃。通往第三个 malloc 的一切似乎都很好。

编辑: 进一步的调查让我相信是早先对malloc 的调用搞砸了这个问题。这样的事情甚至可能吗?如果我取消注释在set_some_struct 之前进行的函数调用并且涉及多个mallocs,那么set_some_struct 将运行得很好。

【问题讨论】:

  • 如果它编译那不是问题,但是你在 mallocs 之后缺少分号
  • 请注意,如果初始分配失败(即some_struct_ptr),代码流不会改变,它只是打印一个失败消息。这意味着,NULL 指针可能正在被取消引用。从malloc() 的返回值中移除强制转换并确保#include <stdlib.h> 存在。
  • 也许你的程序内存不足。在打印“malloc failed”后执行abort(),因此在这种情况下程序的执行不会继续。还将错误消息打印到 stderr,然后调用 fflush(stderr) 以确保消息已写入。

标签: c malloc


【解决方案1】:

好吧,当分配失败时,你所做的就是打印一个错误;也许打印被丢弃或者你错过了它?如果有多个线程运行它,输出可能会令人困惑。

其次,您没有检查 input 指针。由于崩溃是读取,并且通过指针的所有其他访问都是写入新分配的区域,我怀疑一个或多个参数是NULL 指针。你应该检查一下。

另外,你不应该在 C (see here for reasons) 中转换 malloc() 的返回值,如果你不包括 stdlib.h 这可能是隐藏错误。

如果字符串是常量,您可以通过对malloc() 进行一次调用来节省内存和速度,当然,首先将三个分配的大小相加,然后相应地设置指针。

【讨论】:

  • 我很确定我没有丢失输出。如果我在第三次调用 malloc 之前和之后插入打印,那么之前的会显示得很好。并包含 stdlib.h。不过,只需一个 malloc 即可完成这一切的酷提示!
【解决方案2】:
if (some_struct_ptr == NULL)
    printf("malloc failed!\n");

从现在开始,您将使用垃圾指针。下面的代码也会出现同样的问题。

if (some_struct_ptr->m_str1 == NULL)
    printf("malloc failed!\n");

if (some_struct_ptr->m_str2 == NULL)
    printf("malloc failed!\n");

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 2013-11-16
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2016-10-27
    相关资源
    最近更新 更多