I have a function which will append new data with old one by using 
realloc. But its throwing exceptions on huge hit. So what is the best 
alternative for realloc in c++. i think realloc gives me performance 
when the old data is not required to copy again. Using malloc will 
required to copy the old data to new location again. This is not good 
if i want to append one or two bytes at the end right. how std:string 
is implemented for this. I need the string in a single array of chars.

char* GetMemory(char* ptrStr,int iSize,char* newptrStr,int iNewSize)
{
   char* ptr = 0;
   try
   {
      ptr=realloc(ptrStr,iSize+iNewSize);
      if(ptr)
      {
          memcpy(ptr+iSize,newptrStr,iNewSize);
      }
   }
   catch(...)
   {
      cout<<"realloc exception occured!!"<<endl;
   }
   return ptr;
}
What is the best alternative for realloc in c++.

How std::string s;s="ABC";s+="DEF"; is handled the memory allocation?



Reply via email to