Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

max size of std::string

66 views
Skip to first unread message

sachin

unread,
Jan 27, 2009, 7:16:04 AM1/27/09
to
what is the maximum number of characters std string can hold ?
my Program is crashed in assign statement when i fed him character buffer of
size 2195

Giovanni Dicanio

unread,
Jan 27, 2009, 7:36:57 AM1/27/09
to

"sachin" <sac...@discussions.microsoft.com> ha scritto nel messaggio
news:E92EE0C1-A3F9-420A...@microsoft.com...

You must have some other problem elsewhere...

This code shows how you can build a big char buffer (5000 chars) and safely
assign it to a STL string:

<code>

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main()
{
//
// Allocate a big buffer of char's and fill it with 'x's
//
const int len = 5000;
char * bigString = new char[len];
for (int i = 0; i < (len-1); i++)
{
bigString[i] = 'x';
}
bigString[len - 1] = '\0';


//
// Copy to a STL std::string
//
string s = bigString;

// Print
cout << s.c_str() << endl;

// Cleanup
delete [] bigString;

return 0;
}

</code>

Giovanni

Giovanni Dicanio

unread,
Jan 27, 2009, 9:17:55 AM1/27/09
to

"Giovanni Dicanio" <giovanniD...@REMOVEMEgmail.com> ha scritto nel
messaggio news:OAow1vHg...@TK2MSFTNGP03.phx.gbl...

>
> "sachin" <sac...@discussions.microsoft.com> ha scritto nel messaggio
> news:E92EE0C1-A3F9-420A...@microsoft.com...
>> what is the maximum number of characters std string can hold ?
>> my Program is crashed in assign statement when i fed him character buffer
>> of
>> size 2195
>
> You must have some other problem elsewhere...

For example, make sure that you put an end of string ('\0') at the end of
your buffer...

Giovanni

David Wilkinson

unread,
Jan 27, 2009, 1:29:57 PM1/27/09
to

sachin:

Maximum length of std::string is one less than the maximum value of the size_t
type, which is 2^32 (2^64 on 64 bit systems).

Use std::string::max_size() to check.

--
David Wilkinson
Visual C++ MVP

sachin

unread,
Jan 27, 2009, 11:06:01 PM1/27/09
to

thanks for quick replies .. it was useful
i have my buffer null terminated but still my program crashes at the end of
function scope probably because there is some buffer overflow
i will debug and check exact cause and update you all

thansks

0 new messages