I recently introduced a small assignment program that had the following two functions and the main method inside it:
int num_bits(int a) { int bitCount = 0; while(a > 0) { bitCount++; a = a >> 1;
When my teacher returned the program, he wrote a comment about the line noted above, and said that since the value of numBits not known until runtime, initializing an array of size numBits is a dangerous operation because the compiler does not know how much memory is allocated to arr .
I was wondering if anyone could:
1) Make sure this is a dangerous operation
2) Explain what happens in memory when I initialize such an array as the compiler knows which memory to allocate? Is there any way to determine how much memory has been allocated?
Any inputs would be appreciated.
source share