我会得到内存泄漏.. [英] will I get Memory leak..
问题描述
大家好,
void main()
{
char * fp;
fp = malloc(26);
strcpy(fp," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
fp [10] =''\ 0'';
免费(fp);
}
请参考我的问题程序..
1.任何人都可以告诉我,如果我在中间分配''\0''
动态分配的内存区域我会得到内存泄漏.. ??
2。将免费()删除由
动态分配的所有内存,即使在其中间添加了NULL值。??
3.我怎么知道这会导致内存泄漏一个程序??任何
编译器选项都有?我可以使用夹板工具吗?
谢谢,
Ganesh
gNash< ga ********** @ gmail.comwrote:
void main()
这是错误的。它应该是
int main()
或优先选择,
int main( void)
{
char * fp;
fp = malloc(26);
strcpy(fp," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
这是错误的。你复制了27个字符(一个整个字母,加上
终止空字符)到空间,只能容纳26个字节。
fp [ 10] =''\ 0'';
free(fp);
然而,这些都绝对没问题,并且应该没有问题
一旦你修复了它们上面的行中的错误。
}
Richard
11月14日下午12:31,gNash< ganeshamu ... @ gmail.comwrote:
大家好,
void main()
{
char * fp;
fp = malloc(26);
strcpy(fp," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
fp [10] =''\ 0'';
免费(fp);
}
请参考我的问题程序..
1.任何人都可以告诉我,如果我指定''\\ \\ /''中间的
动态分配的内存区域会让内存泄漏.. ??
2。将免费()删除由
动态分配的所有内存,即使在其中间添加了NULL值。??
3.我怎么知道这会导致内存泄漏一个程序??任何
编译器选项都有?我可以使用夹板工具吗?
Richard Bos回答了你的主要问题。我将简单地将
添加为值为0的字符称为NUL而不是
NULL。为了检测内存泄漏,valgrind享有良好的声誉。
11月14日下午5:34,r ... @ hoekstra-uitgeverij.nl(Richard Bos)写道:
gNash< ganeshamu ... @ gmail.comwrote:
void main ()
这是错误的。它应该是
int main()
或优先选择,
int main( void)
{
char * fp;
fp = malloc(26);
strcpy(fp," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
这是错误的。你复制了27个字符(一个整个字母,加上
终止空字符)到空间,只能容纳26个字节。
fp [ 10] =''\ 0'';
free(fp);
然而,这些都绝对没问题,并且应该没有问题
一旦你修复了它们上面的行中的错误。
}
Richard
嗨理查德..
int main(无效)和malloc(26 + 1)我不怀疑我的怀疑
被清楚地发布..请回复。
感谢你,
Hi all,
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
fp[10]=''\0'';
free(fp);
}
Please refer the program for my questions..
1. Can any one tell me if would i assign ''\0'' at middle of
dynamically assigned memory area will i get memory leak.. ??
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?
Thanks,
Ganesh
gNash <ga**********@gmail.comwrote:
void main()This is wrong. It should be
int main()
or by preference,
int main(void)
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.
fp[10]=''\0'';
free(fp);These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.
}Richard
On Nov 14, 12:31 pm, gNash <ganeshamu...@gmail.comwrote:Hi all,
void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
fp[10]=''\0'';
free(fp);
}
Please refer the program for my questions..
1. Can any one tell me if would i assign ''\0'' at middle of
dynamically assigned memory area will i get memory leak.. ??
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?Richard Bos has answered your main questions. I will simply
add that the character with value 0 is referred to as NUL not
NULL. For detecting memory leaks valgrind has a good reputation.
On Nov 14, 5:34 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:gNash <ganeshamu...@gmail.comwrote:void main()
This is wrong. It should be
int main()
or by preference,
int main(void)
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.
fp[10]=''\0'';
free(fp);
These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.
}
Richard
Hi Richard..
the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.
Thankingyou,
这篇关于我会得到内存泄漏..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!