Arraym malloc()和free()问题 [英] Arraym malloc() and free() question
问题描述
你好,
我有关于malloc和free的问题。
这里我的代码示例:
int main( )
{
/ *为数组分配动态内存* /
int * array =(int *)malloc(5 * sizeof(int ));
/ * ...程序代码...... * /
array =(int *)malloc(4 * sieof (int));
免费(数组);
}
现在我的问题:第二次我为数组分配了
内存,我用第一个malloc获得的
地址会怎么样?
是否会自动释放?
Martin
Hello,
I have a question regarding malloc and free.
Here my code sample:
int main()
{
/* allocating dynamic memory for array */
int* array = (int*) malloc(5 * sizeof(int));
/* ... program code ... */
array = (int*) malloc(4 * sieof(int));
free(array);
}
Now my question: The second time I allocate
memory for array, what happens to the
address that I got with the first malloc?
Is it freed automatically?
Martin
推荐答案
ma *********** @ gmx.de (Martin Andert)写道:
ma***********@gmx.de (Martin Andert) writes:
int main()
{
/ *为数组分配动态内存* /
int * array =(int *)malloc(5 * sizeof(int));
我不建议转换malloc()的返回值:
* ANSI C中不需要强制转换。
*转换它的返回值可以掩盖#include
< stdlib.h>的失败,这会导致未定义的行为。
*如果你偶然输入了错误的类型,奇怪的失败可能会导致
结果。
其他一些人不同意,比如PJ Plauger(见文章
< 9s ***************** @nwrddc01.gnilink.net>)。
调用malloc()时,我建议在
上使用sizeof运算符,而不是类型。例如,
*不要写这个:
int * x = malloc(128 * sizeof(int)); / *不要这样做! * /
相反,请这样写:
int * x = malloc(128 * sizeof * x);
这样做有几个理由:
*如果你改变了'x'指向的类型,它'''更改malloc()调用也不需要
。
这在大型程序中更是一个问题,但它仍然是
方便小一点。
*考虑一个对象的大小使得编写语句
不易出错。您无需查看声明即可验证sizeof语法是否正确无误。
/ * ...程序代码... * /
array =(int *)malloc(4 * sieof(int));
免费(数组);
}
现在我的问题:我第二次为数组分配内存,第一个malloc的
地址会发生什么变化?
是否自动释放?
int main()
{
/* allocating dynamic memory for array */
int* array = (int*) malloc(5 * sizeof(int));
I don''t recommend casting the return value of malloc():
* The cast is not required in ANSI C.
* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.
* If you cast to the wrong type by accident, odd failures can
result.
Some others do disagree, such as P.J. Plauger (see article
<9s*****************@nwrddc01.gnilink.net>).
When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don''t* write this:
int *x = malloc (128 * sizeof (int)); /* Don''t do this! */
Instead, write it this way:
int *x = malloc (128 * sizeof *x);
There''s a few reasons to do it this way:
* If you ever change the type that `x'' points to, it''s not
necessary to change the malloc() call as well.
This is more of a problem in a large program, but it''s still
convenient in a small one.
* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
/* ... program code ... */
array = (int*) malloc(4 * sieof(int));
free(array);
}
Now my question: The second time I allocate
memory for array, what happens to the
address that I got with the first malloc?
Is it freed automatically?
编号如果您没有保存原始指针的副本,则
内存将被浪费(泄露)。
-
给我们所有人的教训:即使在琐事中也有陷阱。
--Eric Sosman
No. If you didn''t save a copy of the original pointer, the
memory is wasted ("leaked").
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
>我有关于malloc和free的问题。
>I have a question regarding malloc and free.
这里我的代码示例:
int main()
{
/ *分配动态数组的内存* /
int * array =(int *)malloc(5 * sizeof(int));
/ * ...程序代码...... * /
array = (int *)malloc(4 * sieof(int));
免费(数组);
}
现在我的问题:我第二次分配<对于数组的内存,我用第一个malloc获得的地址会发生什么变化?
你有内存泄漏(假设你没有在其他地方保存地址
)。这是一个坏事(tm)。
内存泄漏不应该导致正确编写的程序崩溃
(检查malloc()是否返回NULL之前使用结果!)但是它可能会导致malloc()失败,特别是如果泄漏处于循环中。
是否自动释放?
Here my code sample:
int main()
{
/* allocating dynamic memory for array */
int* array = (int*) malloc(5 * sizeof(int));
/* ... program code ... */
array = (int*) malloc(4 * sieof(int));
free(array);
}
Now my question: The second time I allocate
memory for array, what happens to the
address that I got with the first malloc?
You have a memory leak (assuming you didn''t save the address
elsewhere). This is a BAD THING (tm).
A memory leak should not cause crashing in a properly written program
(CHECK if malloc() returns NULL before using the result!) but it
may cause malloc() to fail, especially if the leak is in a loop.
Is it freed automatically?
使用malloc()分配的内存不会自动释放,除非
,当程序调用exit()时。
Gordon L. Burditt
Memory allocated with malloc() is not freed automatically, except
perhaps when the program calls exit().
Gordon L. Burditt
Ben Pfaff写道:
Ben Pfaff wrote:
cat main.c
int main(int argc ,char * argv []){
//为数组分配动态内存
// ...程序代码......
array =(int *)malloc(4 * sizeof(int)); / /错误:内存泄漏!
免费(数组);
返回0;
}
gcc -Wall -std = c99 -pedantic -o main main.c
main.c:在函数`ma在'':
main.c:3:警告:隐式声明函数`malloc''
main.c:9:警告:隐式声明函数`free ''
我不建议使用malloc()的返回值:
* ANSI C中不需要强制转换。
> *转换它的返回值可以掩盖#include< stdlib.h>的失败,这会导致未定义的行为。
cat main.c int main(int argc, char* argv[]) {
// allocating dynamic memory for array
int* array = (int*)malloc(5*sizeof(int));
// ... program code ...
array = (int*)malloc(4*sizeof(int));// error: memory leak!
free(array);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c main.c: In function `main'':
main.c:3: warning: implicit declaration of function `malloc''
main.c:9: warning: implicit declaration of function `free''
I don''t recommend casting the return value of malloc():
* The cast is not required in ANSI C.
* Casting its return value can mask a failure to
#include <stdlib.h>, which leads to undefined behavior.
废话!
只有劣质编译器无法警告
隐式声明函数`malloc''
Nonsense!
Only inferior compiler fail to warn about
implicit declaration of function `malloc''
这篇关于Arraym malloc()和free()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!