C:数组声明期间的内存状态
我最近为一项作业提交了一个小程序,其中包含以下两个函数和一个 main 方法:
/**
* Counts the number of bits it
* takes to represent an integer a
*/
int num_bits(int a)
{
int bitCount = 0;
while(a > 0)
{
bitCount++;
a = a >> 1; //shift to the right 1 bit
}
return bitCount;
}
/**
* Converts an integer into binary representation
* stored in an array
*/
void int2bin_array(int a, int *b)
{
//stopping point in search for bits
int upper_bound = num_bits(a);
int i;
for(i = 0; i < upper_bound; i++)
{
*(b+i) = (a >> i) & 1; //store the ith bit in b[i]
}
}
int main()
{
int numBits = num_bits(exponent);
int arr[numBits]; //<- QUESTION IS ABOUT THIS LINE
int2bin_array(exponent, arr);
//do some operations on array arr
}
当我的导师返回该程序时,他对我上面标记的行写了一条评论,说由于 numBits< 的值/code> 直到运行时才知道,将数组初始化为
numBits
大小是一个危险的操作,因为编译器不知道要分配给数组 arr
多少内存>。
我想知道是否有人可以:
1)验证这是一个危险的操作
2)解释当我初始化这样的数组时内存方面发生了什么,编译器如何知道要分配什么内存?有没有办法确定分配了多少内存?
任何意见将不胜感激。
I recently submitted a small program for an assignment that had the following two functions and a main method inside of it:
/**
* Counts the number of bits it
* takes to represent an integer a
*/
int num_bits(int a)
{
int bitCount = 0;
while(a > 0)
{
bitCount++;
a = a >> 1; //shift to the right 1 bit
}
return bitCount;
}
/**
* Converts an integer into binary representation
* stored in an array
*/
void int2bin_array(int a, int *b)
{
//stopping point in search for bits
int upper_bound = num_bits(a);
int i;
for(i = 0; i < upper_bound; i++)
{
*(b+i) = (a >> i) & 1; //store the ith bit in b[i]
}
}
int main()
{
int numBits = num_bits(exponent);
int arr[numBits]; //<- QUESTION IS ABOUT THIS LINE
int2bin_array(exponent, arr);
//do some operations on array arr
}
When my instructor returned the program he wrote a comment about the line I marked above saying that since the value of numBits
isn't known until run-time, initializing an array to size numBits
is a dangerous operation because the compiler won't know how much memory to allocate to array arr
.
I was wondering if someone could:
1) Verify that this is a dangerous operation
2) Explain what is going on memory wise when I initialize an array like that, how does the compiler know what memory to allocate? Is there any way to determine how much memory was allocated?
Any inputs would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 C99 可变长度数组。它是在运行时(而不是由编译器)在堆栈上分配的,基本上相当于
在这种情况下,因为您可以知道函数的上界,并且它相对较小,所以最好使用
这个数组其大小在编译时已知,始终适合您需要的所有内容,并且可以在不支持 C99 的平台上运行。
That's a C99 variable length array. It is allocated at runtime (not by the compiler) on the stack, and is basically equivalent to
In this case, since you can know the upper bound of the function, and it is relatively small, you'd be best off with
This array has a size known at compile time, will always fit everything you need, and works on platforms without C99 support.
应该没问题,它只会进入堆栈。
唯一的危险是毁掉堆栈。
malloc 将是正常的方式,然后您知道您是否有足够的内存,并且可以就下一步做什么做出明智的决定。但在许多情况下,可以假设您不能在堆栈上放置太大的对象。
但严格来说,如果没有足够的空间,这将会严重失败。
It should be ok, it will just go on the stack.
The only danger is blowing out the stack.
malloc would be the normal way, then you know if you have enough memory or not and can make informed decisions on what to do next. But in many cases its ok to assume you can put not too big objects on the stack.
But strictly speaking, if you don't have enough space, this will fail badly.