If I have to use 128/64 byte char array in c. Which one is more efficient static allocation of char array or dynamic memory allocation and why? Is this also valid if I have to allocate memory for 1kb or mare.
如果我必须在c中使用128/64字节字符数组。哪一个更有效的静态分配char数组或动态内存分配为什么?如果我必须为1kb或母马分配内存,这也是有效的。
I want to know which one is more efficient 1 or 2.
1. char array[128] or
2. char *abc = malloc(128);
我想知道哪一个更高效1或2. 1. char array [128]或2. char * abc = malloc(128);
2 个解决方案
#1
Static allocation, by which I assume you mean using the keyword static
, takes place at link time. At runtime it is essentially zero cost, apart from a possible effect on program load time.
静态分配,我假设你的意思是使用关键字static,发生在链接时。在运行时,除了可能对程序加载时间产生影响外,它基本上是零成本。
You can't get any more efficient than that.
你不能比这更有效率。
EDIT
Stack allocation takes place by incrementing and decrementing a register. It is less efficient than static
but considerably more efficient than using runtime memory allocation libraries.
堆栈分配通过递增和递减寄存器来进行。它比静态效率低,但比使用运行时内存分配库效率更高。
#2
Stack allocation is more efficient than malloc() as it is simply an increment of the stack pointer, whereas malloc() involves the operating system and behind the scenes processing we don't have control of. For relatively small variables use the stack unless there is some other reason not to, like you need the memory to exist after your function goes out of scope. Use dynamic allocation for very large variables to avoid stack overflow problems. What is very large? Depends on your CPU and stack size. For windows work I put anything less than a few K on the stack. For a small embedded CPU like a AVR, this can be 255 or less -- again depending on your allocated stack size.
堆栈分配比malloc()更有效,因为它只是堆栈指针的增量,而malloc()涉及操作系统和我们无法控制的幕后处理。对于相对较小的变量,使用堆栈除非有其他原因没有,就像你需要在函数超出范围后存在内存。对非常大的变量使用动态分配以避免堆栈溢出问题。什么是非常大的?取决于您的CPU和堆栈大小。对于Windows工作,我在堆栈上放了少于几K的东西。对于像AVR这样的小型嵌入式CPU,可以是255或更小 - 再次取决于您分配的堆栈大小。
#1
Static allocation, by which I assume you mean using the keyword static
, takes place at link time. At runtime it is essentially zero cost, apart from a possible effect on program load time.
静态分配,我假设你的意思是使用关键字static,发生在链接时。在运行时,除了可能对程序加载时间产生影响外,它基本上是零成本。
You can't get any more efficient than that.
你不能比这更有效率。
EDIT
Stack allocation takes place by incrementing and decrementing a register. It is less efficient than static
but considerably more efficient than using runtime memory allocation libraries.
堆栈分配通过递增和递减寄存器来进行。它比静态效率低,但比使用运行时内存分配库效率更高。
#2
Stack allocation is more efficient than malloc() as it is simply an increment of the stack pointer, whereas malloc() involves the operating system and behind the scenes processing we don't have control of. For relatively small variables use the stack unless there is some other reason not to, like you need the memory to exist after your function goes out of scope. Use dynamic allocation for very large variables to avoid stack overflow problems. What is very large? Depends on your CPU and stack size. For windows work I put anything less than a few K on the stack. For a small embedded CPU like a AVR, this can be 255 or less -- again depending on your allocated stack size.
堆栈分配比malloc()更有效,因为它只是堆栈指针的增量,而malloc()涉及操作系统和我们无法控制的幕后处理。对于相对较小的变量,使用堆栈除非有其他原因没有,就像你需要在函数超出范围后存在内存。对非常大的变量使用动态分配以避免堆栈溢出问题。什么是非常大的?取决于您的CPU和堆栈大小。对于Windows工作,我在堆栈上放了少于几K的东西。对于像AVR这样的小型嵌入式CPU,可以是255或更小 - 再次取决于您分配的堆栈大小。