Re: Runtime stack allocation



kid joe <spamtrap@xxxxxxxxxxxxxxxx> writes:
I'm using a temporary buffer to transfer some data and would rather
not allocate it on the heap. The problem is that the size of the
buffer is only known upon entry into the function that utilizes it and
I'd rather not waste space or dynamically allocate on the heap (since
it doesn't need to persist). Now I'm planning on utilizing inline
assembly to modify the stack pointer directly to allocate the space I
need.
[...]

My advice: allocate it using malloc() (on the "heap") anyway, and call
free() when you're done with it.

There is no portable and reliable way to do temporary allocations on
the "stack". (The C standard doesn't even guarantee that there is a
"stack" in the sense you probably mean.)

By using assembly language, you limit yourself to a single platform.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)
  • Re: "Heap" (was Re: static, global variable memory allocation)
    ... common to refer to the area used to allocate automatic objects ... Can a perverse but conforming implementation use the "heap" for ... used a linked list of save areas instead of a stack. ... CPU-specific region of memory that grows linearly in one direction, ...
    (comp.lang.c)
  • Re: Stack vs. Heap
    ... When you define an object (class instance) on the stack, ... Instead, when you allocate on the heap, you must pay attention to properly ...
    (microsoft.public.vc.mfc)
  • Re: stack and heap question
    ... > "When you allocate primitives they aren't allocated on the heap but on ... > the stack." ... it's allocated on the heap. ...
    (comp.lang.java.programmer)