Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:06:16+00:00 2026-05-30T05:06:16+00:00

I recently submitted a small program for an assignment that had the following two

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:06:17+00:00Added an answer on May 30, 2026 at 5:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

recently I'm writing some functions that I take from Haskell and translate into Java.
I recently submitted a small app working on Silverlight framework. But my app got
Recently in a project, I had a multiprocessing Process that crashed. A child process
Recently I had to develop a SharePoint workflow, and I found the experience quite
I recently submitted my app for the ipad, and it was rejected for not
I recently noticed that I collected 22GB worth of IntelliTrace recordings. Sadly, I was
Recently I found a C library that I want to use in my C++
I recently wrote a Vector 3 class, and I submitted my normalize() function for
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
I recently updated my iPhone only app to an universal app and successfully submitted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.