重新分配从函数返回的值会产生段错误

发布于 2024-11-27 01:00:11 字数 985 浏览 5 评论 0原文

我有一个 C 语言的 LDA 代码,我正在尝试调试它,并且我已经绞尽脑汁有一段时间了。

lda_model *model = NULL;
model = malloc(sizeof(lda_model));
model = quiet_new_lda_model(corpus->num_terms, NTOPICS);
printf ("%f\n", model->alpha); // Segfaults here

如果我们查看模型创建函数,

lda_model* quiet_new_lda_model(int num_terms, int num_topics) {
    int i;
    lda_model* model;

    model = malloc(sizeof(lda_model));
    model->num_topics = num_topics;
    model->num_terms = num_terms;
    model->alpha = 1.0;
    printf ("%f\n", model->alpha); // Prints 1.0
    model->log_prob_w = malloc(sizeof(double*)*num_topics);
    for (i = 0; i < num_topics; i++)
    {
        model->log_prob_w[i] = malloc(sizeof(double)*num_terms);
    memset(model->log_prob_w[i],0,sizeof(double)*num_terms);
    }
    printf ("%f\n", model->alpha); // Prints 1.0
    return(model);
}

quiet_new_lda_model 函数和原始调用者之间的事务可能存在什么问题?

谢谢!

I have a LDA code in C that I'm trying to debug and I've been banging my head for quite some time now.

lda_model *model = NULL;
model = malloc(sizeof(lda_model));
model = quiet_new_lda_model(corpus->num_terms, NTOPICS);
printf ("%f\n", model->alpha); // Segfaults here

if we look at the model creation function

lda_model* quiet_new_lda_model(int num_terms, int num_topics) {
    int i;
    lda_model* model;

    model = malloc(sizeof(lda_model));
    model->num_topics = num_topics;
    model->num_terms = num_terms;
    model->alpha = 1.0;
    printf ("%f\n", model->alpha); // Prints 1.0
    model->log_prob_w = malloc(sizeof(double*)*num_topics);
    for (i = 0; i < num_topics; i++)
    {
        model->log_prob_w[i] = malloc(sizeof(double)*num_terms);
    memset(model->log_prob_w[i],0,sizeof(double)*num_terms);
    }
    printf ("%f\n", model->alpha); // Prints 1.0
    return(model);
}

What could be the problem in the transaction between the quiet_new_lda_model function and the original caller?

Thanks!

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

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

发布评论

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

评论(5

萌︼了一个春 2024-12-04 01:00:11

好吧,首先,调用者中的 malloc 是不必要的,并且会导致内存泄漏。尝试将这四行替换为

lda_model *model = quiet_new_lda_model(corpus->num_terms, NTOPICS);
printf ("%f\n", model->alpha); // Segfaults here

但这不太可能是段错误的原因。接下来我要问的是,quiet_new_lda_model 的原型在调用者中是否可见?如果不是,您的指针可能会被截断为 int。您可以通过将所有三行 printf 行更改为读取来找出答案。

printf("%p\n", model);

如果第三行 printf 打印的值与前两行不同,则问题可能是缺少原型。

编辑:判断您是否缺少原型的另一种方法是查找如下警告消息:

test.c:4: warning: initialization makes pointer from integer without a cast

根据上下文和您使用的编译器,措辞可能会有所不同。如果您使用的是 GCC,请使用 -Wall 命令行开关来获得更强的提示:

test.c:4: warning: implicit declaration of function ‘quiet_new_lda_model’
test.c:4: warning: initialization makes pointer from integer without a cast

“隐式声明”意味着“您没有告诉我有关此函数的任何信息,因此我将假设它接受任意数量的参数并返回int。”正如您可以想象的那样,这几乎总是错误的。

(作为一般规则,如果您使用 GCC,则命令行上应该始终-Wall。)

Well, first off, the malloc in the caller is unnecessary and is causing a memory leak. Try replacing those four lines with

lda_model *model = quiet_new_lda_model(corpus->num_terms, NTOPICS);
printf ("%f\n", model->alpha); // Segfaults here

But that's very unlikely to be the cause of the segfault. The next thing I would ask is, is a prototype of quiet_new_lda_model visible in the caller? If not, your pointer may be being truncated to int. You can find out by changing all three of the printf lines to read

printf("%p\n", model);

If the third printf prints a different value than the first two, a missing prototype is likely to be the problem.

EDIT: Another way to tell if you have a missing prototype is to look for warning messages like this one:

test.c:4: warning: initialization makes pointer from integer without a cast

The phrasing may be different depending on context and which compiler you're using. If you're using GCC, use the -Wall command-line switch to get a stronger hint:

test.c:4: warning: implicit declaration of function ‘quiet_new_lda_model’
test.c:4: warning: initialization makes pointer from integer without a cast

"Implicit declaration" means "you didn't tell me anything about this function so I am going to assume it takes an arbitrary number of parameters and returns int." As you can imagine, this is nearly always wrong.

(As a general rule, if you're using GCC, you should always have -Wall on the command line.)

吾家有女初长成 2024-12-04 01:00:11

这不是解决方案,但你有内存泄漏;您不应该在第一个代码块中分配模型,因为它是在函数中分配的。

我怀疑真正的根本原因是@Zack 所建议的 - 你没有原型,因此返回的地址被截断(我假设你使用的是 64 位系统)。

使用。

lda_model* quiet_new_lda_model(int, int); 

在第一个代码块的顶部

This isn't the solution but you have a memory leak; you shouldn't allocate model in the first block of code since it's allocated in the function.

I suspect the real root cause is what @Zack suggests - you don't have a prototype so the address which is returned is getting truncated (I am assuming you are on a 64 bit system).

use

lda_model* quiet_new_lda_model(int, int); 

at the top of your first block of code.

首先,在quiet_new_lda_model函数中执行malloc时存在内存泄漏,而没有释放之前声明的资源。您不需要在调用者中执行 malloc。其次,lda_model 在哪里/如何定义?头文件是否包含在调用该函数的文件中?

First of all, there is a memory leak doing malloc in the quiet_new_lda_model function without freeing the previously claimed resource. You shouldn't need to do the malloc in the caller. Second, where/how is the lda_model defined? Is the header file included in the file where you call the function?

情徒 2024-12-04 01:00:11

您最近是否更改了 lda_model 的定义?更改后您是否重新编译了所有使用它的源文件?

Have you changed the definition of lda_model recently? Have you recompiled all source files that use it after the change?

风吹过旳痕迹 2024-12-04 01:00:11

修复代码中明显的错误。您有内存泄漏需要修复。第二,再次调试代码,在更高的函数中执行类似这样的代码 if(model) printf("%f", model->alfa);应该可以帮助你前进。它无法预测,但有时,堆栈断言可能会产生段错误。

更新:尝试使用“%ld”而不是“%f”进行更改。也许会有帮助

Fix the obvious bug in the code. You've a memory leak which needs fixing. 2ndly to debug your code again, in the higher function do a code like this if(model) printf("%f", model->alfa); Should help you move forward. It cant be predicted but, sometimes, a stack assertion can create a segfault.

Update : Try using '%ld' as opposed to '%f' for a change..Maybe it will help

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