英文:
Is there any way to quickly initialize memory to known values after malloc()?
问题
我有一个用C编写的程序,需要使用初始值初始化数组。
这个程序有两个版本,一个是使用静态分配的内存,另一个是使用malloc()
动态分配的内存(在每种情况下,大小和初始值都是预先已知的)。
在第一种情况下,数组是通过包含它们定义的头文件进行初始化,如下所示:
float test_data[FEATURES][N][SAMPLES_BATCH] = {-0.715339, -0.50792, -0.307726, ...};
而对于第二种情况,我想知道是否有一种快速初始化一次分配的内存的方法,否则的话,另一种选择就是从文件中加载它们。
英文:
I have a program in C for which I need to initialize arrays with initial values.
This program has been done in two versions, one with statically allocated memory and the other with dynamically allocated memory via malloc() (in each case both the size and the initial values are known a priori).
In the first case the arrays are initialized via an header that contain their definition such as.
float test_data[FEATURES][N][SAMPLES_BATCH] = {-0.715339, -0.50792, -0.307726, ...,};
For the second case, on the other hand, I was wondering if there was a way to initialize the memory once allocated quickly otherwise the alternative would be to load them from files.
答案1
得分: 2
你可以拥有一个包含初始值的静态数组,并使用 memcpy
将其复制到分配的内存中。
static float init_data[] = { 1.0, 2.0, 3.0 };
float *test_data = malloc(sizeof init_data);
memcpy(test_data, init_data, sizeof init_data);
英文:
You can have a static array that contains the initial values, and memcpy
it to the allocated memory.
static float init_data[] = { 1.0, 2.0, 3.0 };
float *test_data = malloc(sizeof init_data);
memcpy(test_data, init_data, sizeof init_data);
答案2
得分: 0
使用malloc()动态分配内存(在每种情况下,大小和初始值都是先验已知的)。
那么您根本不需要动态分配内存。但您有一些选项:
- 从文件中读取数据。
- 用值初始化数组,然后使用
memcpy()
将其复制到分配的块中,如@dbush的答案中所示。 - 使用
mmap()
将文件映射到内存中。
英文:
> with dynamically allocated memory via malloc() (in each case both the
> size and the initial values are known a priori).
Then you do not need to dynamically allocate memory at all. But you do have a few options:
- Read the data from a file.
- Initialize an array with the values and then copy it to the allocated chunk with
memcpy()
, as shown in @dbush's answer. - Map the file into memory with
mmap()
.
答案3
得分: 0
如果大小和初始值是事先已知的话,最高效的初始化方法是使用数据段中的全局变量进行初始化,这些全局变量在声明时已经初始化。这样,程序将使用实际值初始化.data
段,一旦程序加载,变量已经具有初始值,无需复制或执行代码来进行初始化。生成程序文本段的相同过程(exec(2)
调用)用于初始化全局变量。
如果在本地上下文中声明变量(在函数内部作为自动变量)并提供初始化程序(就像您在发布的代码中所做的那样,假设变量是自动的),则初始化程序需要代码来实际初始化本地变量。这可以减少到在函数前言中对memcpy()
的隐式调用,以从初始化数据段中的静态内存区域初始化值。这不如上面的最后一段效率高。
在动态分配内存的情况下,可以使用相同的方法,并通过从静态存储中的静态系列常量值复制初始值来初始化数据。
extern struct my_data_structure my_data_initials; /* 存储初始值的地方 */
struct my_data_structure *my_var = malloc(sizeof *my_var);
*my_var = my_var_initials; /* 在C中允许结构分配 */
英文:
> (in each case both the size and the initial values are known a priori).
If the size and initial values is known a priori, the most efficient way to initialize it is with global variables in the data segment, initialized in the declaration. This makes the program to initialize the .data
segment with the actual values, and as soon as the program is loaded, the variables already have the initial values stored, no need to copy or execute code to initialize them is done. The same process (the exec(2)
call) that generates the segments for the program text is used to initalize the global variables.
If you declare a variable in a local context (inside a function as an automatic variable) and you provide an initializer (as you do in the code you post, assuming the variables are automatic) the initializer requires code to actually initialize the local variables. This can be reduced to a hidden call to memcpy()
in the function preamble to initialize the values from a static memory area in the initialized data segment. This is not as efficient as the last paragraph above.
In the case of dynamically allocated memory, you can use the same approach and initialize the data by copying the initial values from a static series of constant values, stored in static storage.
extern struct my_data_structure my_data_initials; /* where the inital values are stores */
struct my_data_structure *my_var = malloc(sizeof *my_var);
*my_var = my_var_initials; /* structure assignment is permitted in C */
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论