4

I've noticed that the string library doesn't require one to allocate space before writing the input/output. How does the string library allocate that memory dynamically, i know of the 'new' keyword in c++ and 'malloc' in c but when i try something like this, my program stops working.

char* str = new char[strlen(str) + 1];
cin>>str;

i can't get it to work that way, how does the string library work?

I also know of vectors in C++ but not a big fan of using them, i would like to know if there's a way to dynamically allocate memory without having to allocate a size before, something close but not as sophisticated as the string.h library.

How does the string library get unlimited input from user without the programmer specifying buffer size?

closed as unclear what you're asking by Bart van Ingen Schenau, Wayne Molina, user40980, Charles Salvia, gnat Aug 13 '14 at 6:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What do you mean by "the string.h library"? Do you mean the strcpy function and associates, or the std::string class? – Bart van Ingen Schenau Aug 12 '14 at 9:25
  • no, i mean how the string library can simply take unlimited input without the programmer specifying a buffer – Hawk Aug 12 '14 at 9:55
  • stelen(str) isn't going to work until str has actually been populated. Worse, you're mixing up C and C++ strings, which is never good. You should be using std::string instead. – Simon B Aug 12 '14 at 10:08
  • i'm not using the string.h library or the cstring library, i'm asking about how it works and gets unlimited input @SimonBarker – Hawk Aug 12 '14 at 10:09
  • 4
    @Hawk, people are asking what you mean by "it", and you're not answering. "The string library" is not a coherent thing. Since std::string is the only thing you haven't ruled out, I've edited the question to specify that - if that's wrong please tell us what you do mean. – Useless Aug 12 '14 at 12:02
15

How does std::string in c++ allocate memory?

as needed, transparently to the user.

i know of the 'new' keyword in c++ and 'malloc' in c but when i try something like this, my program stops working.

char* str = new char[strlen(str) + 1];
cin>>str;

Well ... yeah; strlen counts the consecutive characters at the provided address, until it encounters a zero. In your case, the specified address is not set (it will be set after strlen is evaluated) so you are effectively calling strlen with some random input - there is no way that would work.

I also know of vectors in C++ but not a big fan of using them,

You really really (really really really) should be. They are an extremely thin (read: fast) wrapper over creating dynamic contiguous memory blocks (exactly what you are trying to solve), that provide type safety, memory bounds safety and automatic memory management.

i would like to know if there's a way to dynamically allocate memory without having to allocate a size before

No.

something close but not as sophisticated as the string.h library.

The string.h is not able to allocate memory without knowing the size. It computes the size every time it needs to allocate memory, at a runtime cost (by, for example, using strlen when needed).

How does the string library get unlimited input from user without the programmer specifying buffer size?

It doesn't. What it does (in rough pseudocode) is this:

  • compute the size of the input (probably requesting it from the input steam through an API)
  • compute the new size the string should have after reading
  • allocate memory (depending on new size), if required
  • copy data in new memory
  • delete old memory and use new instead (if any old memory/value was set)
  • thanks, this is a pretty good answer..it's a bit more clear how the string library works, i guess it's more like a linked list that keeps on increasing. – Hawk Aug 12 '14 at 20:34
  • 1
    It doesn’t sound like you have the right picture in mind. There’s no magic. The standard library string functions allocate a smallish work buffer. If more space is needed, then the library reallocates to a larger buffer. It’s just as simple as it sounds. This part of the standard library pre-dates C++ so I would doubt that the internal implementation would use vector, but it could just as easily. – Bill Door Jul 28 '15 at 15:24
4

If you're not a fan of std::vector you may not want to hear this but, by default, it'll work pretty much the same as std::vector.

When a string reads from a stream, it push_back's each character. What it does when it runs out of space is actually defined by the allocator you've used for the string. The default will be to keep doubling as it does with vectors.

You can change this, of course, by changing the allocator but it sounds like that's not really your question. There's no way to pre-allocate an unknown amount of memory. You've only really got 3 options: allocate none, allocate a fixed amount or allocate an initial amount and then dynamically change it as needs be. Strings and vectors do the latter so that they do what you'd expect but with the trade off that you may over allocate memory depending on the exact length of the string.

  • so they keep dynamically allocating memory during runtime, could you please demonstrate that in c++ code or pseudo-code? – Hawk Aug 12 '14 at 11:16
  • 1
    Just read the code of your library's std::istream& operator>>(std::istream&, std::string&). It'll be in a header somewhere. – Useless Aug 12 '14 at 11:57
  • Maybe you should try implementing a simple dynamic buffer similar to a vector where you can add characters and it resizes to accommodate the new data. Basically a VERY pared down STL container (don't even use templates to focus on memory management). Such an exercise would probably do more to teach you about this than anyone else posting code. – user22815 Aug 12 '14 at 15:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.