P
Pr0 Lurk3r
Hello. I was just experimenting with some uses of macros in C...
here's some code:
<code>
#include <stdlib.h>
#include <stdio.h>
#define smalloc(sz) (safe_malloc(sz, __FILE__, __LINE__))
void* safe_malloc(size_t sz, char const* file, int line) { /* malloc
which bails with helpful message */
void *ptr;
if((ptr = malloc(sz)) == NULL) {
printf("*** malloc() failed in file %s, on line %d. ***\n", file,
line);
abort(); /* or some other error handling */
}
return ptr;
} /* above may be repeated for realloc, calloc, and generalized to
other functions */
int main() { /* demo */
int *leak;
for(; leak = smalloc(10000*sizeof(int)); /* for a faster fail */
return 0;
}
</code>
I may be a bit of a n00b, but this code looks reasonable to me (I
looked at the preprocessed output of the code after gcc, and the
smalloc line gets replaced correctly); however, it fails... either the
code never reaches the printf() block (surely not, for an infinite
loop the OS will run out of memory chunks to dole out to malloc soon
enough ) or printf() fails and in the process launches some new
code which prevents the abort() from occurring... nonetheless, it (my
code running on my Ubuntu VM) simply hangs!
This leads me to a few questions because, if this is the case, then
how would one go about logging failed mallocs (I assume that this is
somewhat unrecoverable and the best one could do would likely be to
print some stuff to std(out/err))? Is my approach/code incorrect?
Furthermore, does everyone experience the same behaviour (I compiled
it using GCC 4.2.4 on a Ubuntu 8.04 VM image - perhaps it's a vmware
issue)? What could be going on here?
P.S. If you do run the code, beware! Your system might end up hanging,
if you're lucky the process will just be killed, but if not you might
have to force a reboot... so save your stuff before doing so!
here's some code:
<code>
#include <stdlib.h>
#include <stdio.h>
#define smalloc(sz) (safe_malloc(sz, __FILE__, __LINE__))
void* safe_malloc(size_t sz, char const* file, int line) { /* malloc
which bails with helpful message */
void *ptr;
if((ptr = malloc(sz)) == NULL) {
printf("*** malloc() failed in file %s, on line %d. ***\n", file,
line);
abort(); /* or some other error handling */
}
return ptr;
} /* above may be repeated for realloc, calloc, and generalized to
other functions */
int main() { /* demo */
int *leak;
for(; leak = smalloc(10000*sizeof(int)); /* for a faster fail */
return 0;
}
</code>
I may be a bit of a n00b, but this code looks reasonable to me (I
looked at the preprocessed output of the code after gcc, and the
smalloc line gets replaced correctly); however, it fails... either the
code never reaches the printf() block (surely not, for an infinite
loop the OS will run out of memory chunks to dole out to malloc soon
enough ) or printf() fails and in the process launches some new
code which prevents the abort() from occurring... nonetheless, it (my
code running on my Ubuntu VM) simply hangs!
This leads me to a few questions because, if this is the case, then
how would one go about logging failed mallocs (I assume that this is
somewhat unrecoverable and the best one could do would likely be to
print some stuff to std(out/err))? Is my approach/code incorrect?
Furthermore, does everyone experience the same behaviour (I compiled
it using GCC 4.2.4 on a Ubuntu 8.04 VM image - perhaps it's a vmware
issue)? What could be going on here?
P.S. If you do run the code, beware! Your system might end up hanging,
if you're lucky the process will just be killed, but if not you might
have to force a reboot... so save your stuff before doing so!