F
Freejack
Recently I've been spending some time googling around for ideas for an
alloca() replacement that could function in a strict ANSI C environment.
As we all know, alloca() is system/arch dependant.
I'm thinking of trying to implement something like "local allocate",
lallocat(), for temporary variables that should be automatically freed
when they go out of scope(function or block).
For simplicity sake, I'm considering something like this...
{
char *mystr = lallocat((char)mystr, (sizeof(mystr) + k));
}
void *lallocat(void foo, size_t size){
void *newfoo[size];
memcpy(newfoo, foo, size);
return newfoo;
}
As you can see, this code has several obvious problems.
As soon as the lallocat() function returns, does it return a pointer
to an array, or a NULL pointer, or undefined. Could be any one of the
three. size_t may have different values depending on the system. Lots of
things.
I cant depend on non-standard concepts such as a stack frame. But I need
to be able to dynamically allocate memory which is local to the scope of
the function or block, which when upon leaving the scope should
automatically be freed for reuse(or possibly returned to the host system.)
Is there any standard way of doing this?
Freejack
alloca() replacement that could function in a strict ANSI C environment.
As we all know, alloca() is system/arch dependant.
I'm thinking of trying to implement something like "local allocate",
lallocat(), for temporary variables that should be automatically freed
when they go out of scope(function or block).
For simplicity sake, I'm considering something like this...
{
char *mystr = lallocat((char)mystr, (sizeof(mystr) + k));
}
void *lallocat(void foo, size_t size){
void *newfoo[size];
memcpy(newfoo, foo, size);
return newfoo;
}
As you can see, this code has several obvious problems.
As soon as the lallocat() function returns, does it return a pointer
to an array, or a NULL pointer, or undefined. Could be any one of the
three. size_t may have different values depending on the system. Lots of
things.
I cant depend on non-standard concepts such as a stack frame. But I need
to be able to dynamically allocate memory which is local to the scope of
the function or block, which when upon leaving the scope should
automatically be freed for reuse(or possibly returned to the host system.)
Is there any standard way of doing this?
Freejack