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

  • Home
  • SEARCH
  • 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 6755145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:21:55+00:00 2026-05-26T13:21:55+00:00

class MyString { public: MyString(int length):_ptr(alloca(length)) { } //Copy Constructor, destructor, other member functions.

  • 0
class MyString
{
  public:
  MyString(int length):_ptr(alloca(length))
  {
  }
  //Copy Constructor, destructor, other member functions.
  private:
  void* _ptr;
};

int main()
{
  MyString str(44);
  return 0;
}

Is it freed at the end of the main function or immediately after constructor is executed?
Is it a good idea to have a string class like this if the above code works as expected?

Update:

It looks like the main danger is

  1. StackOverflow
  2. Inlining of Constructor

I think I can take care of StackOverflow by using alloca for small sizes and malloc/free for large sizes. I guess there must be some non-portable compiler specific way to force the compiler to inline.

I am interested because string class is something that is widely used in any c++ project. If I get this right, I am expecting a huge performance gain as most of the allocations go inside stack which would go into heap otherwise. This will be a utility and the end user will not be aware of the internals.

  • 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-05-26T13:21:55+00:00Added an answer on May 26, 2026 at 1:21 pm

    According to the documentation of alloca, the memory is freed when the caller of alloca returns. So, the memory will be freed at the end of the constructor if the initialiser list is considered as part of the constructor by the compiler, i.e. the stack frame is created before the initialise list is executed. If the stack frame is created after the initialiser list is executed, then the allocated memory will be in the caller of the constructor and so, in this case, the memory is freed at the end of main. I don’t know the standard enough to be absolutely certain which way it’ll happen.

    However, the value of ptr will be unchanged whenever the memory is finally freed.

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

Sidebar

Related Questions

I have class like this: public class object { [Key] int number; String mystring;
I have the following code: public class AppDomainArgs : MarshalByRefObject { public string myString;
Let's say I have the following class: class ABC { private int myInt =
namespace MyNamespace { public struct MyStruct { public string MyString; public int MyInt; public
class MyString { public: MyString(const std::wstring& s2) { s = s2; } operator LPCWSTR()
public class Command { public int CommandId { get; set; } public string Title
when doing #include <string> class MyString : public std::string { public: MyString() {} };
I have an object that contains public class PositionsChannelApplicationGroups { public PositionsChannelApplicationGroups(){} private Map<MyObj1,
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class

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.