473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Portable alloca() replacement?

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
Nov 14 '05 #1
9 4358
REH

"Freejack" <fr******@nowhere.net> wrote in message
news:pa****************************@nowhere.net...
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?

How about a variable length array?

Nov 14 '05 #2
Freejack wrote:
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.
...


There's no way to implement a portable replacement for 'alloca'
function. But in C99 you don't need to. C99 has variable length arrays,
which essentially are core language level equivalents of 'alloca'.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
On Tue, 18 Jan 2005 17:11:40 -0500, REH wrote:

"Freejack" <fr******@nowhere.net> wrote in message
news:pa****************************@nowhere.net...
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.

Is there any standard way of doing this?
How about a variable length array?


It's my understanding that variable length arrays, once allocated, are
fixed in size. That's not so much of a problem, but how would one go about
growing the array in a loop? Also, I understand that implementations might
vary depending on whether it's GNU source or ANSI.

I suppose I could do something like...

{
char *next;
char str[0];
while(fudge /= EOF){
/* Some Code */
char str[strlen (str) + strlen (next) + 1];
strcat(str, next);
}
}

But that looks like it might send performance all to piss. And I would
also be using it for binary data. I'll test it out.

Although it is a possibility.

Freejack
Nov 14 '05 #4
Freejack wrote:
Recently I've been spending some time googling around for ideas for an
alloca() replacement that could function in a strict ANSI C environment.


Use C99 variable size arrays instead.
Nov 14 '05 #5
REH

"Freejack" <fr******@nowhere.net> wrote in message
news:pa****************************@nowhere.net...
It's my understanding that variable length arrays, once allocated, are
fixed in size. That's not so much of a problem, but how would one go about
growing the array in a loop? Also, I understand that implementations might
vary depending on whether it's GNU source or ANSI.

Sorry, I missed the part where you wanted it to be able to grow. Anyways, I
thought you wanted something like the non-standard alloc function? As far
as I know, that doesn't create "growable" memory either. You can allocate
more space, but not grow a previous one. That will stay the same until the
function returns, and it is "destroyed."

Nov 14 '05 #6
Freejack wrote:
...
It's my understanding that variable length arrays, once allocated, are
fixed in size.
But the same is true for 'alloca'! If you need a resizable array why do
you consider 'alloca' in the first place?
That's not so much of a problem, but how would one go about
growing the array in a loop?
Growing the array in a loop? You didn't mention this in your original
message. In that case you can forget about 'alloca' and variable length
arrays. Use "regular" dynamic memory manipulation routines: 'malloc'
and/or 'realloc'.
Also, I understand that implementations might
vary depending on whether it's GNU source or ANSI.


As long as the implementation satisfies the requirements of the language
specification, why care about how it is implemented?

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7
Freejack wrote:
growing the array in a loop?


Maybe it's a job for a linked list instead?

--
pete
Nov 14 '05 #8
Freejack wrote:
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.


You forgot to mention the most obvious one: void is an incomplete type and
thus cannot be used as the type of a function parameter.
Christian
Nov 14 '05 #9
On Tue, 18 Jan 2005 14:42:50 -0800,
Andrey Tarasevich <an**************@hotmail.com> wrote:

Freejack wrote:
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.
...


There's no way to implement a portable replacement for 'alloca'
function. But in C99 you don't need to. C99 has variable length arrays,
which essentially are core language level equivalents of 'alloca'.


The sources for gcc used to come with one, which would work on all systems
to which gcc was ported. This was required because many systems didn't
have a working alloca and gcc itself used it internally. So to botstrap
a gcc compiler using the native compiler a working alloca implementation
had to be provided. That was, by the way, long before there was any C99
standard.

Villy
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: jacob navia | last post by:
Some people here have started saying that "alloca" is off topic, and they used the argument that the gcc compiler doesn't recognize it. They compile their code using the -ansi option, and (off...
6
by: Nitin Bhardwaj | last post by:
Hi all, I want to know what is wrong with alloca() ? Is it hard to implement in a platform-independent way? OR there is no need for it? AFAIK alloca() /*(used to)*/ allocate memory at...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
16
by: Noob | last post by:
Hello, I've rewritten a function (greater_or_equal) that relies on implementation-defined behavior and availability of exact-width integers, with the goal of making the new implementation...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.