gpt4 book ai didi

c - "malloc"在 C/MPI 中起什么作用?

转载 作者:行者123 更新时间:2023-11-30 14:26:41 25 4
gpt4 key购买 nike

Possible Duplicate:
What is malloc doing in this code?

谁能解释一下这段代码的作用,特别是“malloc”?我在 C 中将其与 MPI 一起使用...

vector = ( double * ) malloc ( size_of_vector * sizeof ( double ) );

for ( i = 0; i < size_of_vector; i++ ) {
vector[i] = (double) ( i + 0.211 );
}

我知道 malloc 是这样的:

The function malloc() returns a pointer to a chunk of memory of size size, or NULL if there is an error. The memory pointed to will be on the heap, not the stack, so make sure to free it when you are done with it.

最佳答案

它分配足够的内存来存储 size_of_vector double ,然后初始化该内存以保存 size_of_vector double ,每个 double 根据其在 vector 中的位置计算出一个值。

malloc 调用分配内存。

关于c - "malloc"在 C/MPI 中起什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8763658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com