C:数组声明期间的内存状态

发布于 2025-01-08 10:27:29 字数 1187 浏览 2 评论 0原文

我最近为一项作业提交了一个小程序,其中包含以下两个函数和一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2025-01-15 10:27:30

这是一个 C99 可变长度数组。它是在运行时(而不是由编译器)在堆栈上分配的,基本上相当于

 char *arr = alloca(num_bits);

在这种情况下,因为您可以知道函数的上界,并且它相对较小,所以最好使用

 char arr[sizeof(int)*CHAR_BIT]; 

这个数组其大小在编译时已知,始终适合您需要的所有内容,并且可以在不支持 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

 char *arr = alloca(num_bits);

In this case, since you can know the upper bound of the function, and it is relatively small, you'd be best off with

 char arr[sizeof(int)*CHAR_BIT]; 

This array has a size known at compile time, will always fit everything you need, and works on platforms without C99 support.

锦上情书 2025-01-15 10:27:30

应该没问题,它只会进入堆栈。
唯一的危险是毁掉堆栈。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文