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
- StackOverflow
- 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.