pre-allocate memory for string

This is a discussion on pre-allocate memory for string within the C++ Programming forums, part of the General Programming Boards category; Hello all, is there a way to pre-allocate memory for a string? so if I want to do something like ...

  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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,739
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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, 06: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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21