Is it bad practice to declare an array mid-function...
Posted
by Maximus
on Stack Overflow
See other posts from Stack Overflow
or by Maximus
Published on 2010-06-07T22:58:59Z
Indexed on
2010/06/07
23:02 UTC
Read the original article
Hit count: 182
c
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?
© Stack Overflow or respective owner