Thread: std::string and memory

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    std::string and memory

    How do I do memcpy with std::string?
    I tried:

    Code:
    std::string test;
    test.append();
    But it doesnt work..

    I want to do something like:

    Code:
    char h[4];
    size_t size = 12;
    memcpy(&h, &size, 4 );

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Append reference

    Basically append takes parameters to know what to append.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by anon
    Append reference

    Basically append takes parameters to know what to append.
    I did:
    string.append(&h, 0, 4);

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You need to use pointers to the first and last element of the range. The name of an array acts like a pointer to the first element of an array, so 'h' is a pointer. On the other hand, something like h[3] is a character. However, you can use pointer arithmetic to get a pointer to h[3].
    Code:
    char h[] = "cruel world";
    
    string str = "hello";
    str.append(h+4, h+10);
    cout<<str<<endl;
    Or, if you want to append the whole char array, you can simply do this:
    Code:
    char h[] = "cruel world";
    
    string str = "hello ";
    str.append(h, 12);
    cout<<str<<endl;
    Last edited by 7stud; 01-17-2007 at 10:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you free memory allocated by a std::string?
    By BrianK in forum C++ Programming
    Replies: 17
    Last Post: 05-15-2008, 11:57 AM
  2. Huge memory leak
    By durban in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:10 PM
  3. std::string - memory issue
    By ChadJohnson in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2005, 05:54 PM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM