Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 179705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:27:15+00:00 2026-05-11T14:27:15+00:00

How does one implement alloca() using inline x86 assembler in languages like D, C,

  • 0

How does one implement alloca() using inline x86 assembler in languages like D, C, and C++? I want to create a slightly modified version of it, but first I need to know how the standard version is implemented. Reading the disassembly from compilers doesn’t help because they perform so many optimizations, and I just want the canonical form.

Edit: I guess the hard part is that I want this to have normal function call syntax, i.e. using a naked function or something, make it look like the normal alloca().

Edit # 2: Ah, what the heck, you can assume that we’re not omitting the frame pointer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:27:16+00:00Added an answer on May 11, 2026 at 2:27 pm

    implementing alloca actually requires compiler assistance. A few people here are saying it’s as easy as:

    sub esp, <size> 

    which is unfortunately only half of the picture. Yes that would ‘allocate space on the stack’ but there are a couple of gotchas.

    1. if the compiler had emitted code which references other variables relative to esp instead of ebp (typical if you compile with no frame pointer). Then those references need to be adjusted. Even with frame pointers, compilers do this sometimes.

    2. more importantly, by definition, space allocated with alloca must be ‘freed’ when the function exits.

    The big one is point #2. Because you need the compiler to emit code to symmetrically add <size> to esp at every exit point of the function.

    The most likely case is the compiler offers some intrinsics which allow library writers to ask the compiler for the help needed.

    EDIT:

    In fact, in glibc (GNU’s implementation of libc). The implementation of alloca is simply this:

    #ifdef  __GNUC__ # define __alloca(size) __builtin_alloca (size) #endif /* GCC.  */ 

    EDIT:

    after thinking about it, the minimum I believe would be required would be for the compiler to always use a frame pointer in any functions which uses alloca, regardless of optimization settings. This would allow all locals to be referenced through ebp safely and the frame cleanup would be handled by restoring the frame pointer to esp.

    EDIT:

    So i did some experimenting with things like this:

    #include <stdlib.h> #include <string.h> #include <stdio.h>  #define __alloca(p, N) \     do { \         __asm__ __volatile__( \         'sub %1, %%esp \n' \         'mov %%esp, %0  \n' \          : '=m'(p) \          : 'i'(N) \          : 'esp'); \     } while(0)  int func() {     char *p;     __alloca(p, 100);     memset(p, 0, 100);     strcpy(p, 'hello world\n');     printf('%s\n', p); }  int main() {     func(); } 

    which unfortunately does not work correctly. After analyzing the assembly output by gcc. It appears that optimizations get in the way. The problem seems to be that since the compiler’s optimizer is entirely unaware of my inline assembly, it has a habit of doing the things in an unexpected order and still referencing things via esp.

    Here’s the resultant ASM:

    8048454: push   ebp 8048455: mov    ebp,esp 8048457: sub    esp,0x28 804845a: sub    esp,0x64                      ; <- this and the line below are our 'alloc' 804845d: mov    DWORD PTR [ebp-0x4],esp 8048460: mov    eax,DWORD PTR [ebp-0x4] 8048463: mov    DWORD PTR [esp+0x8],0x64      ; <- whoops! compiler still referencing via esp 804846b: mov    DWORD PTR [esp+0x4],0x0       ; <- whoops! compiler still referencing via esp 8048473: mov    DWORD PTR [esp],eax           ; <- whoops! compiler still referencing via esp            8048476: call   8048338 <memset@plt> 804847b: mov    eax,DWORD PTR [ebp-0x4] 804847e: mov    DWORD PTR [esp+0x8],0xd       ; <- whoops! compiler still referencing via esp 8048486: mov    DWORD PTR [esp+0x4],0x80485a8 ; <- whoops! compiler still referencing via esp 804848e: mov    DWORD PTR [esp],eax           ; <- whoops! compiler still referencing via esp 8048491: call   8048358 <memcpy@plt> 8048496: mov    eax,DWORD PTR [ebp-0x4] 8048499: mov    DWORD PTR [esp],eax           ; <- whoops! compiler still referencing via esp 804849c: call   8048368 <puts@plt> 80484a1: leave 80484a2: ret 

    As you can see, it isn’t so simple. Unfortunately, I stand by my original assertion that you need compiler assistance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The difference here is that char *s = "Hello world";… May 12, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer Instances don't have names. By the time the global name… May 12, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer Check out FileVersionInfo class might be helpful for you. var… May 12, 2026 at 9:21 pm

Related Questions

I have a series of policy objects which I thought would be convenient to
In short: I want to have two fullscreen views, where I can switch between
I have for the most part successfully embedded firefox/xulrunner into our c# application, but
How does one implement a parser (in Python) for a subset of wikitext that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.