How to copy pointers of different data types

I am able to copy pointers of same data types as below:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
  string *first = new string {"Some"};
  string *second = new string {"Dome"};
  memcpy (second, first, sizeof(string));
  cout << " The new secondvalue is " << *second << '\n';
  return 0;
}

Output is "Some", as expected.
How can I do the same memcpy for different data types like:
string to char, int to char, int to string, etc.
When I do the following it gives a garbage value as output.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
  string *first = new string {"Some"};
  char *second = new char {'D'};
  memcpy (second, first, sizeof(string));
  cout << " The new secondvalue is " << *second << '\n'; // O/P = Garbage value
  return 0;
}

Please help.
Thanks
Last edited on
1
2
3
string *first = new string {"Some"};
string *second = new string {"Dome"};
memcpy( second, first, sizeof(string) );

No. Don't.

You don't know how the std::string has been implemented. Therefore, the raw memcpy can utterly corrupt the objects.

This is how you copy your strings:
1
2
3
string *first = new string {"Some"};
string *second = new string {"Dome"};
*second = *first;


How do you copy a string to a char? A very interesting challenge. Lets say that we have this string:
Therefore, the raw memcpy can utterly corrupt the objects.
Which one character can hold the information within all those characters? One character is only 8 bits.

Int to char is almost trivial, if the value of int is within [0..256). Anything outside will not fit.

A different int to char is conversion of one-digit integral value into single printable character, e.g. 7 into '7'.

Int to string is similar for more digits. For that, use: http://www.cplusplus.com/reference/string/
Thank you Keskiverto. I found that strcpy can be used to copy a string to a char* like this:
1
2
3
4
5
  string *first = new string {"invite"};
   char *second = new char [sizeof(first)];
   strcpy(second, first->c_str());

   cout << " The new secondvalue is " << second << '\n';      // The output is: invite 

My doubt is, it is a correct way of doing the copy.
Also if I want to copy a struct pointer to a char* like this below, it does not work
1
2
3
4
5
6
7
8
9
10
struct mystruct {
string name;
};

  mystruct *first = new mystruct {"invite"};
   char *second = new char [sizeof(first)];
   strcpy(second, first->c_str());

   cout << " The new secondvalue is " << second << '\n';  
 

The error: 'struct main()::mystruct' has no member named 'c_str'

Any possible way to solve this?
Thanks

Last edited on
I found that strcpy can be used to copy a string to a char* like this:
1
2
3
string *first = new string {"invite"};
char *second = new char [sizeof(first)];
strcpy(second, first->c_str());

No. That's going to corrupt memory. Line 2 only allocates 4 bytes of memory (on a 32 bit compiler). "invite" is 6 bytes. i.e. sizeof(first) returns the size of a pointer.

My doubt is, it is a correct way of doing the copy.

No.

1
2
3
4
5
6
7
8
9
struct mystruct {
string name;
};

mystruct *first = new mystruct {"invite"};
char *second = new char [sizeof(first)];
strcpy(second, first->c_str());

cout << " The new secondvalue is " << second << '\n'; 

Again, you're going to corrupt name. And again you have only allocated 4 bytes.
keskiverto made clear above that std::string is a complex data type. You do not know it's implementation. You can't overwrite it and expect it to work.

The error: 'struct main()::mystruct' has no member named 'c_str'

c_str() is a member function of std::string. mystruct is not a std::string and therefore has no c_str() function.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.




Last edited on
Your 'first' is a raw pointer. The size of a pointer is enough to store a memory address. Nothing more, nothing less. The size of non-pointer types has no relation to the size of pointers.

The std::string is a complex type. Its implementation can be different on different compilers. The std::string does store some text, but the size of the stored text is different from the size of the std::string object.

I found that strcpy can be used

The memcpy is a C function.
The strcpy is a C function.
The std::string is a C++ type.
C functions were not made to handle C++ constructs.
C functions operate on raw pointers and plain arrays.

The proper header to include the definition of std::string is <string>.

The header <cstring> contains C library's C functions.


You have a fixation for pointers and manual dynamic allocation (new). While they are good to know and understand, they are less convenient and more error-prone than C++ Standard Library constructs, like std::string and smart pointer types.

This you could strive for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using std::string;

struct Mystruct {
  string name;

  Mystruct( const string & name )
  : name ( name )
  {}

  Mystruct() = default;
};

int main ()
{
  string first {"Some"};
  string second {"Dome"};
  second = first;
  std::cout << " The new secondvalue is " << second << '\n';

  Mystruct third {"invite"};
  second = third.name;
  std::cout << " The new secondvalue is " << second << '\n';

  return 0;
}
Thanks Annon and Keskiverto.
What can be done when we have multiple data members in the struct?
Also the problem for me is that, I have a setFunction():
1
2
3
4
(void Message::setData(const char* Data)
{
this->Data_var = Data;
}

and a getFunction():
1
2
3
4
const char* Message::getData() const
{
return Data_var.c_str();
}

So I want some way of conversion where my input data to the setFunction should be the struct members and the the ouptut data from the getFunction should also be the same struct members. The input what I am sending should be exactly the same which I will receive.
What can be done when we have multiple data members in the struct?

What do you want to be done?


Why do you have all those char*? Your Message::Data_var looks like it could be a std::string.

my input data to the setFunction should be the struct members

That is up to the caller (if I understood you correctly).
Topic archived. No new replies allowed.