C 中的数组、函数调用和目标文件
Arrays, function calls, and object files in C
我有一些关于数组大小如何改变 C 程序中二进制文件大小的问题。
假设我有如下程序:
#include <stdio.h>
#include <stdlib.h>
void f()
{
int array[1000];
for (int i = 0; i < 1000; i++)
array[i] = i;
}
int main(void)
{
float m[1000];
for (int i = 0; i < 1000; i++)
m[i] = 2 * i;
f();
return 0;
}
这是一个愚蠢的例子,但我认为它可以帮助说明我的问题:
数组(m 和 array)会影响二进制文件的大小吗?如果是这样,如何?
如果我从不调用函数 f(),array 是否仍然存在于二进制文件中,从而影响其大小?
如果 f() 在外部(静态)库上,最终的二进制大小如何变化?
我尝试使用 size 命令进行实验,无论数组的大小如何,二进制文件的大小始终相同。
我希望我的问题足够清楚。谢谢。干杯。
Do the arrays (m and array) affect the size of the binary? If so, how?
不会,这些数组是在栈上分配的,所以只会影响栈,不会影响磁盘上的大小
If I never call the function f(), will array still exists on the binary, thus affecting its size?
和上面的答案是一样的
If f() is on an external (static) library, how the final binary size change?
它会因某些字节而异(动态链接条目的大小与函数机器代码的大小)
我有一些关于数组大小如何改变 C 程序中二进制文件大小的问题。
假设我有如下程序:
#include <stdio.h>
#include <stdlib.h>
void f()
{
int array[1000];
for (int i = 0; i < 1000; i++)
array[i] = i;
}
int main(void)
{
float m[1000];
for (int i = 0; i < 1000; i++)
m[i] = 2 * i;
f();
return 0;
}
这是一个愚蠢的例子,但我认为它可以帮助说明我的问题: 数组(m 和 array)会影响二进制文件的大小吗?如果是这样,如何? 如果我从不调用函数 f(),array 是否仍然存在于二进制文件中,从而影响其大小? 如果 f() 在外部(静态)库上,最终的二进制大小如何变化?
我尝试使用 size 命令进行实验,无论数组的大小如何,二进制文件的大小始终相同。
我希望我的问题足够清楚。谢谢。干杯。
Do the arrays (m and array) affect the size of the binary? If so, how?
不会,这些数组是在栈上分配的,所以只会影响栈,不会影响磁盘上的大小
If I never call the function f(), will array still exists on the binary, thus affecting its size?
和上面的答案是一样的
If f() is on an external (static) library, how the final binary size change?
它会因某些字节而异(动态链接条目的大小与函数机器代码的大小)