Re: Runtime stack allocation
- From: Stephen Sprunk <stephen@xxxxxxxxxx>
- Date: Mon, 30 Jun 2008 11:47:45 -0500
kid joe wrote:
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.
Here's an example:
void doSomeStuff(unsigned size) {
char* rawData;
//INLINED (Intel p4 assembly)
//sub esp, dword ptr [size]
//mov dword ptr [rawData], esp
//do some stuff with the temporary buffer, using rawData to access
it
//INLINED (Intel p4 assembly)
//add esp, dword ptr [size]
}
This should just work, right? Or are there any odd quirks that I should be
aware of that I may not have thought of?
Well, assuming you want to limit your code to only working on a particular platform or processor, and you know your code won't conflict with the compiler's optimizations...
malloc()/free() should be fast enough, even if it doesn't "feel" like the right tool for the job. If not, can you use a VLA? If not, does your system offer alloca()? Any of those are better than subverting the compiler (and portability) and confusing people who will read your code later by using inline assembly.
S
.
- Follow-Ups:
- Re: Runtime stack allocation
- From: Keith Thompson
- Re: Runtime stack allocation
- References:
- Runtime stack allocation
- From: kid joe
- Runtime stack allocation
- Prev by Date: Re: OOP
- Next by Date: Re: problem passing pointer array
- Previous by thread: Re: Runtime stack allocation
- Next by thread: Re: Runtime stack allocation
- Index(es):
Relevant Pages
|