473,399 Members | 2,478 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,399 software developers and data experts.

Why didn't ANSI/ISO make alloca() standard?

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 run-time from the
function stack, so that the programmer need not call free() to
de-allocate it.Since it automatically got freed when the function
returns, Right?

So what is wrong with implementing such a function..why doesn't the
Standard provide this function?

Thanks.
Nov 14 '05 #1
6 4021
ni*************@hotmail.com (Nitin Bhardwaj) wrote:
Is it hard to implement in a platform-independent way?
OR there is no need for it?


Both. RTFAQ: <http://www.eskimo.com/~scs/C-faq/q7.32.html>.

Richard
Nov 14 '05 #2
In <17**************************@posting.google.com > ni*************@hotmail.com (Nitin Bhardwaj) writes:
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 run-time from the
function stack, so that the programmer need not call free() to
de-allocate it.Since it automatically got freed when the function
returns, Right?

So what is wrong with implementing such a function..why doesn't the
Standard provide this function?


Whenever you have such questions, look up the (freely available) C
standard rationale document, first. ONLY if you find nothing there
ask here.

Some implementations provide a function (often called alloca) which
allocates the requested object from automatic storage; the object is
automatically freed when the calling function exits. Such a function
is not efficiently implementable in a variety of environments, so it
was not adopted in the Standard.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40****************@news.individual.net...
ni*************@hotmail.com (Nitin Bhardwaj) wrote:
Is it hard to implement in a platform-independent way?
OR there is no need for it?


Both. RTFAQ: <http://www.eskimo.com/~scs/C-faq/q7.32.html>.


alloca can be tricky to implement correctly, as the FAQ points out. The
compilers I've used that did implement it all did implement it correctly,
even if there were already things on the stack like partial function
arguments. alloca is pretty handy for fast allocation of temporary buffers -
it's thread safe, exception safe, and the automatic cleanup is nice when a
function has complex logic with multiple exits.

I've had to port some code that used alloca to a system that couldn't
support it. The result usually was an uglification of the code, as a bunch
of code needed to be added to keep track of all the allocated bits so they
could be properly cleaned up.

Fortunately, I don't have to use that miserable system any more <g>, and I
regularly use alloca. It's portable to all the compilers I need to use. I
like alloca, and put it in the D programming language as well.

Sure, alloca is not in the C standard. But that standard specifies a minimum
standard, the realities of the compiler business are that alloca will be
supported if it is reasonably feasible to get it to work on the system.

-Walter
www.digitalmars.com free C/C++/D compilers
Nov 14 '05 #4
In <KBCJc.81990$JR4.74655@attbi_s54> "Walter" <wa****@digitalmars.nospamm.com> writes:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40****************@news.individual.net...
ni*************@hotmail.com (Nitin Bhardwaj) wrote:
> Is it hard to implement in a platform-independent way?
> OR there is no need for it?


Both. RTFAQ: <http://www.eskimo.com/~scs/C-faq/q7.32.html>.


alloca can be tricky to implement correctly, as the FAQ points out. The
compilers I've used that did implement it all did implement it correctly,
even if there were already things on the stack like partial function
arguments. alloca is pretty handy for fast allocation of temporary buffers -
it's thread safe, exception safe, and the automatic cleanup is nice when a
function has complex logic with multiple exits.

I've had to port some code that used alloca to a system that couldn't
support it. The result usually was an uglification of the code, as a bunch
of code needed to be added to keep track of all the allocated bits so they
could be properly cleaned up.

Fortunately, I don't have to use that miserable system any more <g>, and I
regularly use alloca. It's portable to all the compilers I need to use. I
like alloca, and put it in the D programming language as well.

Sure, alloca is not in the C standard. But that standard specifies a minimum
standard, the realities of the compiler business are that alloca will be
supported if it is reasonably feasible to get it to work on the system.


And, as you learned yourself, code relying on it will not be portable to
systems where alloca is not reasonably feasible. If none of the
implementors in the C committee had any problems supporting alloca, I am
reasonably convinced that it would have been part of the standard C
library. There was plenty of existing practice for alloca, at the time
C89 was drafted.

Likewise, the rather strange and arbitrary conditions imposed by the
standard to the usage of the setjmp macro (even H&S got them wrong,
in their example code) are the result of requests made by implementors,
who needed them in order to get this macro to work on their supported
platforms.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Nitin Bhardwaj wrote:

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?


In addition to the other answers, it is effectively implemented
in C99, by the use of 'adjustable arrays'.

--
Replies should be to the newsgroup
Replace this line to publish your e-mail address.
Nov 14 '05 #6
LibraryUser <de**********@made.invalid> writes:
Nitin Bhardwaj wrote:

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?


In addition to the other answers, it is effectively implemented
in C99, by the use of 'adjustable arrays'.


I think you mean variable length arrays (VLAs). The term 'adjustable
arrays' would imply that the length can be changed after the object is
created, a feature that VLAs don't support.

There are some differences. A VLA can only be created in a
declaration; alloca() can be called in the middle of an expression
(which can cause problems in function calls). Also, the number of
VLAs created in a given scope is fixed, whereas alloca() can be called
an arbitrary number of times. For example, a function using could
create a linked list of N alloca()-allocated nodes, where N is a
parameter; that can't be done with VLAs. (Well, maybe with some ugly
tricks involving recursion, but I'm not going to figure out how to do
it).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

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

Similar topics

100
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We...
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...
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...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
4
by: Luke Wu | last post by:
I am just wondering what the following terms usually mean: 1) "Standard C" 2) "K&R C" 3) "ANSI C" I am pretty sure "ANSI C" usually refers to the C89 standard, but what
83
by: sunny | last post by:
Hi All What is C99 Standard is all about. is it portable, i mean i saw -std=C99 option in GCC but there is no such thing in VC++.? which one is better ANSI C / C99? can i know the major...
127
by: bz800k | last post by:
Hi Does this code satisfy ANSI C syntax ? void function(void) { int a = 2; a = ({int c; c = a + 2;}); /* <<-- here !! */ printf("a=%d\n", a);
41
by: jaysome | last post by:
It's been almost eight years since ISO/IEC approved ISO/IEC 9899:1999. Does anyone know if ANSI has approved it? A Google search shows arguably confusing answers as to whether ANSI has...
8
AmberJain
by: AmberJain | last post by:
HELLO, Is it necessary for a C programmer to have an ANSI C standard or it's sufficient to own Kernigham and Rithie's The C programming language? I know that the ritchie's book is quite brief...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.