Thread: pre-allocate memory for string

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    pre-allocate memory for string

    Hello all,
    is there a way to pre-allocate memory for a string?

    so if I want to do something like this:
    Code:
    char *data;
    string value;
    
    for (i=0; i<size; i++)
    {
      value += data[i];
    }
    And I wanted to pre-allocate some memory this I could do:
    Code:
     string value[size];
    I'm not looking for an array, but just a string. Set to the right size before I start loading it for speed.

    Thanks,
    Michael

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be:
    Code:
    string value(size);
    However, looking at the context, data presumably points somewhere. As such, instead of the loop, you can just write:
    Code:
    string value(data, data + size);
    and get on with something more important.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by laserlight View Post
    It would be:
    Code:
    string value(size);
    However, looking at the context, data presumably points somewhere. As such, instead of the loop, you can just write:
    Code:
    string value(data, data + size);
    and get on with something more important.
    Everyone yelled at me when I put that. They are arguing over if I'm putting a pointer in the size section or a very large number.

    Why not just put
    Code:
    string value(data, size)
    ?

    Thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheBigOnion
    Everyone yelled at me when I put that. They are arguing over if I'm putting a pointer in the size section or a very large number.
    If they object on those grounds, then they have no clue about pointer arithmetic, or they are simply unaware that std::string has a constructor that takes two iterators that denote a range.

    Quote Originally Posted by TheBigOnion
    Why not just put
    Code:
    string value(data, size)
    ?
    That is certainly another option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by laserlight View Post
    If they object on those grounds, then they have no clue about pointer arithmetic, or they are simply unaware that std::string has a constructor that takes two iterators that denote a range.


    That is certainly another option.

    Then should it have been:
    Code:
    string value(data, data, size);
    ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, that would simply be wrong as there is no such constructor. (The third argument to these constructors is an allocator object, but the default is used if it is not provided.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Basically, the iterator version is string(start, end) where start and end are two addresses (or objects acting as addresses) which forms a range from where to copy the data into the string. As such, your 3 argument constructor makes no sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  2. Where does compiler allocate memory for Char x[] = "abc" ?
    By meili100 in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2009, 05:02 AM
  3. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  4. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM