malloc里面的功能(我知道......我*先*搜索谷歌;) [英] malloc inside function (I know... I *did* search google first ;)
问题描述
main()
{
float * f;
initialize_f(f);
// ...使用f进行处理
免费(f);
}
无效initialize_f(float * f)
{
long n = 100;
f =(float *)malloc(n * sizeof(float)) ;
f [1] = ... //等在这里填充数组
}
只有在调用initialize_f()时才知道数组的大小,所以我不能在main()中分配
。请问有人能告诉我这个代码是否正确初始化了这个?谢谢:)
main()
{
float * f;
initialize_f(f);
// ...use f for processing
free(f);
}
void initialize_f(float * f)
{
long n = 100;
f = (float*)malloc(n*sizeof(float));
f[1] = ... // etc. fill f array here
}
The size of the array is only known once initialize_f() is called so I
can''t allocate it in main(). Please can someone tell me if f will be
initialized correctly by this code? Thanks :)
推荐答案
< sp ****** @ yahoo.com>在消息中写道
新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...
你在这里错过了必要的标题:
#include< stdlib.h>
你也错过了原型在main()中调用的函数,尝试:
void initialize_f(float * f);
<sp******@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
You are missing a necessary header here:
#include <stdlib.h>
You''re also missing a prototype for a funtion called in main(), try:
void initialize_f(float *f);
main()
这应该是:
int main(无效)
请参阅 http://www.faqs.org/faqs/C-faq/faq/ (
你显然在谷歌搜索时没有找到,问题#11.12a。
{
浮动* f;
为什么用(浮点)代替(双)?
initialize_f(f);
// ...使用f进行处理
免费(f);
main()返回一个int,你遗漏了类似的东西:
返回0;
}
void initialize_f(float * f)
{/ n>长n = 100;
f =(float *)malloc(n * sizeof(float));
强烈建议不要强制转换malloc()的返回值;请参阅c.l.c常见问题
#7.7。事实上,在这种情况下,如果您在编译时提示您忘记包含一个
必需的头文件,那么不必要的强制转换可能会有
。 />
f [1] = ... //等在这里填充f数组
当然,你会意识到C中的数组从索引0开始 - 你的代码中是什么?
到f [0]?
}
一旦initialize_f()是一个数组的大小这样叫我
不能在main()中分配它。有人可以告诉我这个代码是否正确初始化了f?谢谢:)
main()
This should be:
int main(void)
Please refer to the c.l.c FAQ at http://www.faqs.org/faqs/C-faq/faq/ (which
you apparently DIDN''T find while googling), question #11.12a.
{
float * f;
Why use (float) instead of (double)?
initialize_f(f);
// ...use f for processing
free(f);
main() returns an int, you are missing something like:
return 0;
}
void initialize_f(float * f)
{
long n = 100;
f = (float*)malloc(n*sizeof(float));
Casting the return value of malloc() is highly discouraged; see c.l.c FAQ
#7.7. In fact, in this case the absence of the unneccesary cast may have
given you a hint at compile time that you had forgotten to include a
required header file.
f[1] = ... // etc. fill f array here
You do realize, of course, that arrays in C start at index 0--what happens
to f[0] in your code?
}
The size of the array is only known once initialize_f() is called so I
can''t allocate it in main(). Please can someone tell me if f will be
initialized correctly by this code? Thanks :)
我没有看到初始化f的问题。在这段代码中。
但是你的initialize_f()函数应该提供n。 as
返回值,以便在main()中知道它的大小。我会写它
这样的东西:
/ *未经测试的代码如下* /
long initialize_f(float * f){
int i;
long n = 100;
f = malloc((size_t)n * sizeof * f);
for(i = 0; i< n; i ++){
f [i] = 0; / *或你想要的任何初始化值* /
} / * for i * /
返回n;
} / * initialize_f * /
-Charles
I don''t see any problems with the initialization of "f" in this code.
However your initialize_f() function should probably supply "n" as its
return value so that its size can be known in main(). I would write it
something like this:
/* Untested code follows */
long initialize_f(float *f) {
int i;
long n = 100;
f = malloc((size_t)n * sizeof *f);
for(i=0; i<n; i++) {
f[i] = 0; /* or whatever initialization value you want */
} /* for i */
return n;
} /* initialize_f */
-Charles
spasmous写道:
spasmous wrote:
main()
{
float * f;
initialize_f(f);
// ...使用f进行处理
free(f);
}
void initialize_f(float * f)
{/ n>长n = 100;
f =(float *)malloc(n * sizeof(float) ));
f [1] = ... //等在这里填充f数组
}
数组的大小只有一次初始化_ ()被调用所以我不能在main()中分配它。有人可以告诉我这个代码是否正确初始化了f?谢谢:))
main()
{
float * f;
initialize_f(f);
// ...use f for processing
free(f);
}
void initialize_f(float * f)
{
long n = 100;
f = (float*)malloc(n*sizeof(float));
f[1] = ... // etc. fill f array here
}
The size of the array is only known once initialize_f() is called so I
can''t allocate it in main(). Please can someone tell me if f will be
initialized correctly by this code? Thanks :)
不,这不会起作用,因为C是按值调用的。 ''initialize_f''
获得''f''的副本。通过调用malloc()
来更新副本,但是当函数返回时,副本中的值将丢失。
你必须发送initialize_f()一个指针到''f''。
尝试这样的东西。
它未经测试和未编译,但它得到的想法:
#include< stdlib.h>
#include< stdio.h>
void initialize_f(float ** f,long * n );
int main(无效)
{
float * f;
long n ;
initialize_f(& f,& n);
if(f == NULL)
退出(EXIT_FAILURE );
/ *使用f进行处理......
*
* if(f [0]< 3.14159 )printf(几乎pi!\ n);
* /
...
/ *全部完成,清理和退出* /
免费(f);
返回0;
}
void initialize_f(float ** f,long * n)
{
* n = 100;
* f = malloc(n * sizeof ** f);
if(* f == NULL)
{
fprintf(stderr ,malloc()失败\ nn;);
返回;
}
/ *初始化f的元素...
*(* f)[0] = 3.14159;
*(* f)[1] = ...
* ...
*(* f)[n-1] = ...
* /
}
No, that won''t work because C is call by value. ''initialize_f''
gets a copy of ''f''. The copy is updated by the call to malloc()
but when the function returns, the value in the copy is lost.
You have to send initialize_f() a pointer to ''f''.
Try something like this instead.
Its untested and uncompiled, but it gets the idea across:
#include <stdlib.h>
#include <stdio.h>
void initialize_f(float **f, long *n);
int main(void)
{
float *f;
long n;
initialize_f(&f, &n);
if (f == NULL)
exit(EXIT_FAILURE);
/* use f for processing ...
*
* if (f[0] < 3.14159) printf("almost pi!\n");
*/
...
/* All done, cleanup and quit */
free(f);
return 0;
}
void initialize_f(float **f, long *n)
{
*n = 100;
*f = malloc(n * sizeof **f);
if (*f == NULL)
{
fprintf(stderr, "malloc() failed\n");
return;
}
/* Initialize elements of f...
* (*f)[0] = 3.14159;
* (*f)[1] = ...
* ...
* (*f)[n-1] = ...
*/
}
sp******@yahoo.com 在2005年8月23日:
sp******@yahoo.com wrote on 23/08/05 :
void initialize_f(float * f)
{
long n = 100;
f =(float *)malloc(n * sizeof(float));
void initialize_f(float * f)
{
long n = 100;
f = (float*)malloc(n*sizeof(float));
您正在修改参数的值。这通常是
糟糕设计的标志。多想想,你会明白为什么。
注意:参数是用C中的值传递的。
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C库: http://www.dinkumware.com/refxc.html
它是指定的。但任何编写这样代码的人都应该将这些代码转化为蚯蚓并喂给鸭子。 - Chris Dollin CLC
You are modifying the value of a parameter. This often is the sign for
a bad design. Think more and you''ll see why.
Note: parameters are passed by value in C.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"It''s specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
这篇关于malloc里面的功能(我知道......我*先*搜索谷歌;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!