-2

The below peice of code does not work when run in codepad.org. I searched online to find what exactly will happen on memcpy of structures with C++ string.

#include <iostream>
using namespace std;

typedef struct {    
    int i;
    std::string data;
} ST_INFO;

int main ()
{
    ST_INFO stInfo1, stInfo2;
    stInfo1.i = 1;
    stInfo1.data.assign("test");

    memcpy(&stInfo2, &stInfo1, sizeof(stInfo2));
    cout << "data" << stInfo2.data.c_str();   
   return 0;
}

I couldn't get succint answers. While searching for answers I ended up with more questions.

Additional Question:

1) How the memory allocation and deallocation happen for strings. 2) what will happen on memset of structures with C++ string is done.

Could anyone here please help me?

Karuppiah
  • 41
  • 4
  • Something horrible will probably happen. Don't do it. Use C++ idioms in C++ code. – Robinson Mar 13 '15 at 14:00
  • `I couldn't get succint answers.` That *is* the answer. There is no "succint" answer. – PaulMcKenzie Mar 13 '15 at 14:08
  • There is no need for `typedef struct` in a C++ program. That is a holdover from `C`. Maybe your confusion and question stems from you not knowing that C++ has `non-POD` types, something that doesn't exist in the `C` world. Using `memset` and `memcpy` on non-POD types is undefined behavior. Better drop the `C` habits and style, as it could get you in trouble in writing proper C++ code (not saying you can't be both a C and C++ programmer, but you must be aware that they are two different languages with major differences, as you can see here). – PaulMcKenzie Mar 13 '15 at 15:34

2 Answers2

3

When you copy an object by memcpy you do the shallow copy. So lets say the string has its data stored in the data allocated dynamically (stored by pointer) and the only thing that will be copied is the pointer to the data. This is why it is not a good practice to use memcpy on a object!

W.F.
  • 13,888
  • 2
  • 34
  • 81
2

No-one can say exactly what happens when you use memcpy on classes that aren't trivially copyable, because it gives undefined behaviour.

You can copy this correctly using its automatically generated copy constructor or copy-assignment operator:

stInfo2 = stInfo1;         // copy assignment
ST_INFO stInfo3 = stInfo1; // copy initialisation (using copy constructor)

1) How the memory allocation and deallocation happen for strings.

Allocation happens automatically when you make the string grow beyond its current capacity, or when you call reserve to increase the capacity. Deallocation happens automatically when the string is destroyed, when you call shrink_to_fit and (possibly) when the string is cleared.

2) what will happen on memset of structures with C++ string is done.

Undefined behaviour. You can only furtle with the storage bytes of trivial types (and even then the behaviour isn't always well-defined); std::string is not trivial.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thanks Sir for your response!! Could you please tell more about 'Allocation happens automatically'? Does it internally use 'new' for allocation of memory? Any poiners or link about this will be helpful. – Karuppiah Mar 13 '15 at 14:42
  • @Karuppiah: It internally uses its allocator which (if you didn't supply a non-default one) uses `operator new`. Usually, you don't need to worry about exactly what it's doing; it just makes sure enough memory is allocated, as long as you use it correctly. A [good book](http://stackoverflow.com/questions/388242) or [online reference](http://en.cppreference.com/w/cpp/string/basic_string) will cover the details, if you need them. – Mike Seymour Mar 13 '15 at 14:49
  • @Karuppiah Even though your question and responses concern `std::string`, using functions such as `memcpy` and `memset` on any non-trivial type results in undefined behavior. So it isn't a `std::string` issue -- it is an issue with any non-POD type. – PaulMcKenzie Mar 13 '15 at 15:18