在使用calloc
的代码片段中,您创建了27个数组:自动存储中的一个指针数组[1],堆上的26个字符array.
在使用memset
的代码片段中,您创建了指针数组,仅此而已.您try 修改26个字符数组,但从未创建它们.
以下代码等同于calloc代码段,但仅使用自动存储.
char string00[ 3 ] = { 0 };
char string01[ 4 ] = { 0 };
char string02[ 5 ] = { 0 };
char string03[ 6 ] = { 0 };
char string04[ 7 ] = { 0 };
char string05[ 8 ] = { 0 };
char string06[ 9 ] = { 0 };
char string07[ 10 ] = { 0 };
char string08[ 11 ] = { 0 };
char string09[ 12 ] = { 0 };
char string10[ 13 ] = { 0 };
char string11[ 14 ] = { 0 };
char string12[ 15 ] = { 0 };
char string13[ 16 ] = { 0 };
char string14[ 17 ] = { 0 };
char string15[ 18 ] = { 0 };
char string16[ 19 ] = { 0 };
char string17[ 20 ] = { 0 };
char string18[ 21 ] = { 0 };
char string19[ 22 ] = { 0 };
char string20[ 23 ] = { 0 };
char string21[ 24 ] = { 0 };
char string22[ 25 ] = { 0 };
char string23[ 26 ] = { 0 };
char string24[ 27 ] = { 0 };
char string25[ 28 ] = { 0 };
char *string_array[ 26 ] = {
string00, string01, string02, string03, string04,
string05, string06, string07, string08, string09,
string10, string11, string12, string13, string14,
string15, string16, string17, string18, string19,
string20, string21, string22, string23, string24,
string25,
};
现在,如果我们假设char
没有对齐限制,我们可以将上面的简化如下:
char buffer[ 403 ] = { 0 }; // 3+4+5+...+28 = 403
char *string_array[ 26 ];
for ( size_t j=0, i=0; j<26; ++j ) {
string_array[ j ] = buffer + i;
i += j + 3;
}
最后,正如@Ted Lyngmo指出的那样,一些编译器在自动存储中提供了alloca
/_alloca
进行分配.
char *string_array[ 26 ];
for ( size_t j=0; j<26; ++j ) {
string_array[ j ] = alloca( j + 3 ); // This is what you were missing.
memset( string_array[ j ], 0, j + 3 );
}
C没有堆栈和堆的概念.C标准并没有规定对象存储在哪里,只规定了它们可以被访问的持续时间.这就是所谓的storage duration.
因此,以下内容不是基于标准,但几乎是普遍适用的:
objects with automatic storage duration |
objects with allocated storage duration |
On the stack, in a register, or optimized away |
On the heap |
Needs less time/CPU to allocate |
Needs more time/CPU to allocate |
Needs less space/memory to allocate |
Needs more space/memory to allocate |
Limited control over lifespan of the allocated block |
Lifespan can extend beyond the function that allocated the memory |
Uncatchable dire consequences to over-allocating |
Over-allocating can be detected and handled |
Somewhat limited resource |
Vast resource |
Always available |
Not available on some systems (e.g. many embedded systems) |
Can be hard/impossible to allocate complex structures unless the compiler provides non-standard alloca or similar. |
Simple to allocate complex structures |
这里没有介绍两个存储持续时间:静态和线程.
- 不能保证使用堆栈.但没错,这可能会是一大堆.