c++ - Return string read from buffer and function without dynamic allocation? -


how go returning string built buffer within function without dynamically allocating memory?

currently have function consider:

    // reads null-terminated string buffer in instance of buffer class.     // uint16 :: unsigned short     // ubyte :: unsigned char     ubyte* readstr( void ) {         ubyte* result = new ubyte[]();          for( uint16 = 0; < bytesize; ++ ) {             result[ ] = buffer[ byteindex ];             byteindex ++;              if ( buffer[ byteindex - 1 ] == ubyte( 0 ) ) {                 byteindex ++;                 break;             };         };          return result;     }; 

while can return built string, can't without dynamic allocation. becomes problem if consider following usage:

// instance of buffer class "buffer" calling readstr():     cout << buffer.readstr() << endl;     // or...     ubyte string[] = buffer.string(); 

usages similar call result in same memory leak data not being deleted via delete. don't think there way around this, not entirely sure if it's possible.

there @ least 3 ways reimplement method avoid direct allocation new.

the good:

use std::vector (this allocate heap memory):

std::vector<ubyte> readstr()  {     std::vector<ubyte> result;     (uint16 = 0; < bytesize; i++)      {         result.push_back(buffer[byteindex]);         byteindex++;          if (buffer[byteindex - 1] == ubyte(0))          {             byteindex++;             break;         }     }     return result; } 

the bad:

force caller provide output buffer , possibly size avoid overflows (does not directly allocate memory):

ubyte* readstr(ubyte* outputbuffer, size_t maxcount)  {     (uint16 = 0; < bytesize; i++)      {         if (i == maxcount)             break;          outputbuffer[i] = buffer[byteindex];         byteindex++;          if (buffer[byteindex - 1] == ubyte(0))          {             byteindex++;             break;         }     }     return outputbuffer; } 

the ugly:

use internal static array , return reference it:

ubyte* readstr()  {     enum { max_size = 2048 }; // decide max size...     static ubyte outputbuffer[max_size];      (uint16 = 0; < bytesize; i++)      {         if (i == max_size)             break;          outputbuffer[i] = buffer[byteindex];         byteindex++;          if (buffer[byteindex - 1] == ubyte(0))          {             byteindex++;             break;         }     }     return outputbuffer; } 

be aware last option has several limitations, including possibility of data races in multithreaded application , inability call inside recursive function, among other subtle issues. otherwise, closest looking , can used safely if take precautions , make assumptions calling code.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -