Jump to content

  • Log In with Google      Sign In   
  • Create Account


std::stringstream C++ SIZE


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 CyberMike   Banned   -  Reputation: 120

Like
0Likes
Like

Posted 13 January 2013 - 04:24 AM

Hi

I suspect using std::stringstream is not big enough. Could that be? In fact I need to store around 20'000 characters. How to set std::stringstream size?

Many thanks

 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8045

Like
5Likes
Like

Posted 13 January 2013 - 04:33 AM

The string stream has no size limitations.



#3 BaneTrapper   Members   -  Reputation: 1166

Like
0Likes
Like

Posted 13 January 2013 - 09:18 AM

Hi

I suspect using std::stringstream is not big enough. Could that be? In fact I need to store around 20'000 characters. How to set std::stringstream size?

Many thanks

I suspect using std::stringstream is not big enough

Care to explain what did you really mean?

 

How to set std::stringstream size?

You are not allowed to manually set size as far as i know, but you can manipulate objects stored in it so you control its size indirectly.

 

But what you can do is reserve memory for 20000 characters, to easy process of reallocation for space each time you add character.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#4 Cornstalks   Crossbones+   -  Reputation: 6974

Like
2Likes
Like

Posted 13 January 2013 - 11:23 AM

Michaels-MacBook-Pro:~ mjbshaw$ cat t.cpp
#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream ss;
    for (int i = 0; i < 20000; ++i)
        ss << i << '\n';
 
    std::string str = ss.str();
 
    std::cout << "There are " << str.size() << " characters in ss and str\n";
    std::cout << "The contents of ss and str are:\n" << str;
}
Michaels-MacBook-Pro:~ mjbshaw$ g++ t.cpp && ./a.out 
There are 108890 characters in ss and str
The contents of ss and str are:
0
1
2
3
4
[...] (I'm not going to post all 20,000 numbers)
 
Seems to work just fine for me... I got 108,890 characters in ss and str...

Edited by Cornstalks, 13 January 2013 - 11:24 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 SaurabhTorne   Members   -  Reputation: 252

Like
0Likes
Like

Posted 13 January 2013 - 11:43 PM

@Cornstalks: how many characters in str total though? Also how much would be the sizeof occupied. would it be depending on systems configuration.



#6 SaurabhTorne   Members   -  Reputation: 252

Like
1Likes
Like

Posted 13 January 2013 - 11:56 PM

I raised the loop and hehe, it did go up to 438888890 for i < 50000000.  I gueess its is has no size limitations. :)



#7 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 14 January 2013 - 12:04 AM

@Cornstalks: how many characters in str total though? Also how much would be the sizeof occupied. would it be depending on systems configuration.

Crap. I'm sorry, I tapped my mousepad and didn't realize my mouse was right over the "vote down" button and accidentally gave this post a -1. I've tried to counter-act that accident by voting up the post below it. Sorry!

 

Now to respond to your question: There is no set size limit for std::string (or any container, for that matter). The limit is your system. If std::string needs more memory, it will request more memory from the operating system. If the operating system has it, it will give it more. If it doesn't, then your program will fail (well, technically a std::bad_alloc exception will be thrown).


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS