My question is regarding a problem I encountered recently on my code. Long story short, there was a function getting a dynamicly allocated array of hashtables as following void output(Hashtable* hash_array). Inside the function was the following piece of code int k=some_function(); int results[k];
The problem was: After running the program for a while, data inside hash_array were lost. In an attempt to debug the program, I tried printing the value of hash_array with fprintf(stderr,"%p\n",hash_array); and it turns out that hash_array had value (nil) just before the segmentation fault, even though function output was only reading data from hash_array. The problem was fixed when I changed int results[k]; into int* results=new int[k];
So I have two questions regarding this:
- What exactly does the compiler do when it encounters a static array declared with a variable size like in
int results[k];? - How did (or could)
int results[k];affect the hashtable array?