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 8335737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:45:41+00:00 2026-06-09T03:45:41+00:00

Several compilers support extensions to C++ whereby one can dynamically allocate memory on the

  • 0

Several compilers support extensions to C++ whereby one can dynamically allocate memory on the stack. For instance, g++ supports alloca() as well as VLAs. All of these extensions come with the caveat that the dynamically-allocated memory is “deallocated” at the end of the scope of the calling function. (Edit: To clarify, I use quotes around ‘deallocated’ because what’s actually happening is that the compiler is decrementing/incrementing the stack pointer, hence why alloca() requires compiler support.) This means that memory allocated using alloca() within the constructor of a class is deallocated as soon as the constructor returns.

This restriction becomes to difficult to deal with in classes where the constructor goes through some nontrivial steps to determine how much memory to allocate. The user of the class must allocate the memory in the function from which the class is constructed, exposing some internals that arguably should not be exposed. Moreover, putting workarounds in my code to be able to use alloca() or VLAs is generally inconvenient.

Can you think of some way to circumvent this restriction so that I can use alloca(), VLAs, or some other language extension, so that the memory can be allocated within the class and have class scope?

  • 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. Editorial Team
    Editorial Team
    2026-06-09T03:45:42+00:00Added an answer on June 9, 2026 at 3:45 am

    Nope, it’s the impossibles. A class can’t allocate function-local memory- that plain doesn’t make sense.

    However, you can achieve extremely fast heap allocation with a proper custom allocator, like a memory arena- to the point where it’s more than fast enough. What you need to remember is that new is the nuclear warhead of memory allocation- it must support any allocation size, and any allocation/deallocation pattern. Something more specific can operate a hell of a lot faster- to the point where it’s quite competitive with stack allocated memory. On Visual Studio 2010, I could even get it to execute faster than alloca.

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

Sidebar

Related Questions

I want to compile my .java's (several) into one .jar that are compatible with
I have several files which is causing a compile error, can the following be
It has been mentioned in several sources that C++0x will include better language-level support
I have noticed from several web pages that apparently Spring 3.0 supports @Inject from
I have fairly large C++ library with several sub-libraries that support it, and I
I know that the #warning directive is not standard C /C++, but several compilers
Background There are several different debug flags you can use with the Visual Studio
I need to make my program only one process on several platforms. I have
The gcc support for the language features of C++11 is well documented at http://gcc.gnu.org/projects/cxx0x.html
I would like to share my Oracle SQL Developer configuration across my several computers

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.