Is it bad practice to declare an array mid-function...
- by Maximus
In C, which would be more proper...
void MyFunction()
{
int* array;
int size;
//do a bunch of stuff
size = 10;
array = (int*)(sizeof(int)*size);
//do more stuff...
//no longer need array...
free(array);
}
Or is something like this okay?
void MyFunction()
{
int size;
//do a bunch of stuff
size = 10;
array[size];
//do more stuff...
}
The malloc uses the heap instead of the stack, so I suppose if you know size is going to be very large you'd want to malloc... but if you're quite certain size will be small enough, would the second method be reasonable?