将数组传递给函数-不同的值-Segfault [英] Passing an array to a function - Different values - Segfault
问题描述
我有以下代码:
gpointer w[3];
GtkWidget *menu_item = gtk_menu_item_new();
w[0] = menu_item;
menu_item = gtk_menu_item_new();
w[1] = menu_item;
GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
w[2] = buffer;
到目前为止,一切都很好.现在连接信号:
This is all good till now. Let's now connect a signal:
g_signal_connect(w[0], "activate", G_CALLBACK(runner), w);
runner
函数声明为:
void runner(gpointer root, gpointer w[]);
在输入runner
之前测试w
数组的值,当其中的值(值)不同时,进行测试.我需要他们是一样的.我该怎么做到?为什么它们不相同?此外,还会发生段错误.
我创建了一个小程序,它是原始程序的基本内容,应该重新创建导致问题发生的条件.奇怪的是,它运行正常.
Testing the values of w
array before entering runner
and while in it shows that they (the values) are different. I need them to be the same. How can I accomplish that, and why they aren't identical? Also, segfault occurs.
I created a small program that is bare bones of the original one and that is supposed to recreate the conditions such that the problem occurs. Oddly enough, it runs fine.
#include <gtk/gtk.h>
void carry(gpointer root, gpointer a[])
{
g_print("\n");
g_print("%d\n", root);
g_print("%d\n", a[0]);
g_print("%d\n", a[1]);
g_print("%d\n", a[2]);
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *menu_item;
GtkTextBuffer *buffer;
gpointer abc[3];
menu_item = gtk_menu_item_new();
abc[0] = menu_item;
g_print("%d\t%d\n", menu_item, abc[0]);
menu_item = gtk_menu_item_new();
abc[1] = menu_item;
g_print("%d\t%d\n", menu_item, abc[1]);
buffer = gtk_text_buffer_new(NULL);
abc[2] = buffer;
g_print("%d\t%d\n", buffer, abc[2]);
g_signal_connect(abc[2], "modified-changed", G_CALLBACK(carry), abc);
gtk_text_buffer_set_modified(abc[2], TRUE);
gtk_main();
return 0;
}
这意味着还有其他问题.我现在将尝试其他操作,例如注释行并仅保留相关行.
我还没有评论任何行,但是我尝试在呼叫者和被呼叫者中都放入
g_print
.
Which means that something else is problematic. I'll try something else now, like commenting lines and leaving only the relevant ones.
I didn't comment any lines yet, but I tried putting
g_print
in both the caller and the callee.
这是输出:
1162863440 1162863440
1162864736 1162864736
1163320992 1163320992
1162863440
-2
1162668992
973486176
前三行将原始值与其在数组中的副本进行比较(从上面的代码中g_print("%d\t%d\n", menu_item, abc[0]);
的意义上来说).如您所见,所有内容均已正确分配.换一行后,我们在被叫方中检查那些相同的值.第一个参数root
始终具有正确的值.因此,这没有问题.被叫方始终中的abc[0]
的值为 -2 .说真的,每次我运行程序时,它都是 -2 .其他两个(abc[1]
和abc[2]
)始终具有一些垃圾随机值,但是每当我运行程序时,它们就会改变,与abc[0]
不同.
The first three lines compare the original values with their copies in the array (in the sense of g_print("%d\t%d\n", menu_item, abc[0]);
from the code above). As you can see, everything is assigned correctly. After a new line, we check those same values in the callee. root
, the first parameter, always has the correct value. So there's no problem with that. abc[0]
in the callee always has the value of -2. Seriously, every time I run the program it is -2. Other two (abc[1]
and abc[2]
) always have some garbage random values, but they change every time I run the program unlike abc[0]
.
我希望这将有助于诊断和解决问题.
I hope this will help in diagnosing and fixing the problem.
推荐答案
我尝试通过功能(func(arg0, arg1, ...)
而不是使用g_signal_connect()
)正常传递abc[0]
和abc
,并且任何问题.
I tried passing both abc[0]
and abc
normally through a function (func(arg0, arg1, ...)
instead of using g_signal_connect()
) and there is no problem whatsoever.
这一切仅意味着一件事:g_signal_connect
弄乱了我的价值观.它出于某些未知原因而更改了它们.
This all can mean only one thing: g_signal_connect
is messing with my values. It changes them for some unknown reason.
我想我必须使用一个结构.
I guess I'll have to use a struct.
这篇关于将数组传递给函数-不同的值-Segfault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!