它会泄漏内存吗? [英] Will it leak the memory?
问题描述
我有一个问题。
int main()
{
char * p =(char *)malloc(9);
strcpy(p," TajMahal");
p ++ ;
免费(p);
}
此程序是否会导致内存泄漏?
请解释。
问候,
Kiran Patil
Hi,
I do have a question.
int main()
{
char *p = (char *)malloc(9);
strcpy(p,"TajMahal");
p++;
free(p);
}
Does this program results in memory leak?
Please explain.
Regards,
Kiran Patil
推荐答案
KG写道:
KG wrote:
我有一个问题。
int main()
{
char * p =(char *)malloc(9);
strcpy(p,TajMahal);
p ++;
免费(p);
< br $>
}
这个程序会导致内存泄漏吗?
Hi,
I do have a question.
int main()
{
char *p = (char *)malloc(9);
strcpy(p,"TajMahal");
p++;
free(p);
}
Does this program results in memory leak?
导致未定义的行为。 free
的唯一合法参数是null,并且从malloc返回的指针尚未被释放。
没有办法告诉C标准做了什么。
如果你省略`p ++`,那么程序会以内存终止
仍然分配。这是否会导致内存泄漏取决于(a)你所说的内存泄漏是什么意思。 (b)当程序退出时,周围的
环境会起什么作用。是/否/指定
所有mallocated内存都返回到系统。
-
Chris 考虑嵌入式系统 Dollin
Hewlett-Packard Limited Cain Road,Bracknell,注册号:
注册办事处:Berks RG12 1HN 690597 England
It results in undefined behaviour. The only legal arguments to `free`
are null and a pointer returned from malloc that hasn''t been freed.
There''s no way of telling what that does from the C standard.
If you omit the `p++`, then the program terminates with memory
still allocated. Whether this results in a memory leak depends
on (a) what you mean by "memory leak" and (b) what the surrounding
environment does when the program exits. It is /not/ specified that
all mallocated memory is "returned to the system".
--
Chris "consider embedded systems" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
KG写道:
KG wrote:
我有一个问题。
int main()
{
char * p =(char *)malloc(9);
Hi,
I do have a question.
int main()
{
char *p = (char *)malloc(9);
你还没有为malloc包含一个有效的原型,你的演员已经隐藏了一个潜在的价值编译器诊断。
You haven''t included a valid prototype for malloc and your cast has
hidden a potentially value compiler diagnostic.
>
strcpy(p," TajMahal");
p ++;
免费(p);
>
strcpy(p,"TajMahal");
p++;
free(p);
这会导致未定义的行为。你传递给free()
的地址不是一个用malloc(),calloc()
或realloc()分配的空间块的地址。 br />
This results in undefined behaviour. The address you passed to free()
isn''t an address of a a block of space allocated with malloc(), calloc()
or realloc().
}
此程序会导致内存泄漏吗?
}
Does this program results in memory leak?
如果你很幸运,程序会崩溃。如果你非常不走运,
计算机会生长武器,生产一把刀并刺伤你。
< http://www.xkcd.com/293 />
If you''re lucky the program will crash. If you''re very unlucky, the
computer will grow arms, produce a knife and stab you.
<http://www.xkcd.com/293/>
请解释。
Please explain.
请解释为什么你觉得发布这个废话是合适的。
Please explain why you felt it was appropriate to post this nonsense.
Chris Dollin写道:
Chris Dollin wrote:
KG写道:
KG wrote:
>>
我确实有一个问题。
int main()
{* * char *(= * char *)malloc(9);
strcpy(p,& ; TajMahal);
p ++;
免费(p);
}
此程序是否会导致内存泄漏?
>>Hi,
I do have a question.
int main()
{
char *p = (char *)malloc(9);
strcpy(p,"TajMahal");
p++;
free(p);
}
Does this program results in memory leak?
导致未定义的行为。 free
的唯一合法参数是null,并且从malloc返回的指针尚未被释放。
没有办法告诉C标准做了什么。
如果你省略`p ++`,那么程序会以内存终止
仍然分配。 [snip]
It results in undefined behaviour. The only legal arguments to `free`
are null and a pointer returned from malloc that hasn''t been freed.
There''s no way of telling what that does from the C standard.
If you omit the `p++`, then the program terminates with memory
still allocated. [snip]
你真的是这个意思吗?
Did you really mean this?
这篇关于它会泄漏内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!