朋友们,我正在尝试从指针数组中释放内存:
const gchar *strings[21];
strings[0] = malloc(strAuth)
strings[0] = strAuth
....
....
int j=0;
while(j < 20){
if (strlen(strings[j]) != 0) {
g_free((char*)strings[j]);
g_print("Cleaned:%d\n",j);
}
j++;
g_print("%d",j);
}
//g_free((char*)strings);
j 最多打印 20,然后给出
$ ./mkbib
Cleaned:0
1Cleaned:1
2Cleaned:2
34Cleaned:4
56789101112Cleaned:12
1314151617181920*** glibc detected *** ./mkbib: malloc(): memory corruption (fast): 0x0000000000a14e10 ***
任何解释(对 C 新手)?
编辑 1对不起,愚蠢的信息,我避免 strAuth 是因为它涉及 gtk 库(我在 clc 中询问特定库相关问题的经验很糟糕)。所以真正的代码看起来:
strings[0] = g_malloc(strlen(gtk_entry_get_text(GTK_ENTRY(e->entry1))));
strings[0] = gtk_entry_get_text(GTK_ENTRY(e->entry1));
where gtk_entry_get_text
is of type const gchar *
可能我在最初的帖子上浪费了你的时间。请帮忙。
编辑 2
const gchar *strings[21];
strings[0] = g_malloc(strlen(gtk_entry_get_text(GTK_ENTRY(e->entry1))));
strings[0] =g_strdup(gtk_entry_get_text(GTK_ENTRY(e->entry1)));
........
int i=2;
while (i < 21) {
if (strlen(strings[i]) != 0) {
g_string_append_printf(tstring, ",\n\t%s=\"%s\"",
keyword[i], strings[i]);
g_free((char*)strings[i]);
strings[i]=NULL;
g_print("Cleaned:%d\n",i);
}
i++;
}