C
charlie
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;
if(nstuffs > ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_stuff, ntemp_stuffs * sizeof(int));
}
/* Use the temp stuffs for something */
memcpy(temp_stuff, stuff, nstuffs * sizeof(int));
}
Is this a horrible thing to do?
Cheers,
Charlie
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;
if(nstuffs > ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_stuff, ntemp_stuffs * sizeof(int));
}
/* Use the temp stuffs for something */
memcpy(temp_stuff, stuff, nstuffs * sizeof(int));
}
Is this a horrible thing to do?
Cheers,
Charlie