问题描述
大家好,
我正在用gcc编译器编写一个包含很多
函数的程序。在主要的我创建了一个变量的内存,并尝试在另一个函数中释放
。但是我在某些时候因为分段错误而导致错误,或者在某些时候glibc free无效指针。您可以帮我解决任何一个
。但我必须释放数据。
A(){
mem =(char *)malloc(sizeof(char));
mem = B(mem);
}
B(字符* K)
{
char * j;
memcpy(j,K,长度);
....
免费(K);
返回j;
}
这个会给你一个想法。
感谢大家
再见
Raghu
Hello Every one,
I am writing a program in gcc compiler which contains many
functions. In main I created memory for a variable and tried to free
in another function. But I am getting error as segmentation fault at
some times or glibc free Invalid Pointer at some times. Can any one of
you help me out. But I must free the data.
A(){
mem= (char *)malloc(sizeof(char));
mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
....
free(K);
return j;
}
This one will give you an idea.
Thanking you all
Bye
Raghu
推荐答案
放弃演员。
请注意,sizeof(char)定义为1.所以你要分配单个
字节的内存。你不能在一个字节中存储很多。
Drop the cast.
Note that sizeof(char) is defined to be 1. So you''re allocating a single
byte of memory. You can''t store much in one byte.
j'的值是不确定的,所以你写的任何东西都是错的。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
电子邮件:rjh在上述域名, - www。
j''s value is indeterminate, so anything you write through it is wrong.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
我已经建议你放弃演员。如果你忽略了好的建议,为什么要求它呢?
// n字节。
I have already suggested that you drop the cast. If you ignore good
advice, why ask for it?
// n bytes.
我已经解释过j是不确定的,所以任何通过
写的都是无效的。如果你忽略好建议,为什么要求它?
I have already explained that j is indeterminate, so any writing through
it is invalid. If you ignore good advice, why ask for it?
在这个阶段,程序是如此无可救药地充斥着错误,以至于它无法用任何保证来预测它的行为。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
电子邮件:rjh在上述域名中, - www。
By this stage the program is so hopelessly riddled with bugs that it is
simply impossible to predict its behaviour with any assurance.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于关于免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!