J
Jorge Peixoto de Morais Neto
I was reading the code of FFmpeg and it seems that they use malloc
just too much. The problems and dangers of malloc are widely known.
Malloc also has some overhead (although I don't know what is the
overhead of automatic variable sized arrays, I suspect it is smaller
than that of malloc), although I'm not too worried about it.
I was thinking that, with C99's variable length arrays, malloc
shouldn't be needed most of the time. But I'm not sure if it is
appropriate to declare everything as automatic variables, specially
for huge variables.
1-) When is it appropriate to use automatic variables?*
2-) If the problem is size, then what is the threshold?**
*I already know that we should be aware of the duration of the
variable, such as not to use an automatic variable as a buffer inside
a function, and then return the buffer. By the time the function
returns, the buffer is gone.
** Of course, I know this varies. But, given a reasonable system (say,
my Athlon XP 2600+ with 512 MB of RAM) and a reasonable OS (such as
GNU/Linux, and let's also include MS Windows because it is common),
can you give me an order of magnitude?
PS: I have googled first, and searched within this newsgroup before,
and I only found one previous discussion. It missed the point. People
kept arguing that automatic variables are fixed size, which is not
true anymore. People also kept arguing that malloc tells you when
there is not enough memory (by returning NULL) and allows you to do
something. I don't care much about it, since in the vast majority of
cases I just close the program when malloc fails. I have even written
a wrapper, so I don't have to check the pointer:
inline void *smalloc(size_t size){
void * const ptr=malloc(size);
if (ptr==NULL){
fputs("Function ",stderr);
fputs(__func__,stderr);
fputs(" called malloc and malloc returned NULL.\n",stderr);
perror(NULL);
exit(ENOMEM); // exit ou abort?
} else {
return ptr;
}
}
By the way, in the above case should I have used abort() or is exit()
ok? From the respective manual pages, I can't tell.
just too much. The problems and dangers of malloc are widely known.
Malloc also has some overhead (although I don't know what is the
overhead of automatic variable sized arrays, I suspect it is smaller
than that of malloc), although I'm not too worried about it.
I was thinking that, with C99's variable length arrays, malloc
shouldn't be needed most of the time. But I'm not sure if it is
appropriate to declare everything as automatic variables, specially
for huge variables.
1-) When is it appropriate to use automatic variables?*
2-) If the problem is size, then what is the threshold?**
*I already know that we should be aware of the duration of the
variable, such as not to use an automatic variable as a buffer inside
a function, and then return the buffer. By the time the function
returns, the buffer is gone.
** Of course, I know this varies. But, given a reasonable system (say,
my Athlon XP 2600+ with 512 MB of RAM) and a reasonable OS (such as
GNU/Linux, and let's also include MS Windows because it is common),
can you give me an order of magnitude?
PS: I have googled first, and searched within this newsgroup before,
and I only found one previous discussion. It missed the point. People
kept arguing that automatic variables are fixed size, which is not
true anymore. People also kept arguing that malloc tells you when
there is not enough memory (by returning NULL) and allows you to do
something. I don't care much about it, since in the vast majority of
cases I just close the program when malloc fails. I have even written
a wrapper, so I don't have to check the pointer:
inline void *smalloc(size_t size){
void * const ptr=malloc(size);
if (ptr==NULL){
fputs("Function ",stderr);
fputs(__func__,stderr);
fputs(" called malloc and malloc returned NULL.\n",stderr);
perror(NULL);
exit(ENOMEM); // exit ou abort?
} else {
return ptr;
}
}
By the way, in the above case should I have used abort() or is exit()
ok? From the respective manual pages, I can't tell.