memcopy of structs to std::string failed 
Author Message
 memcopy of structs to std::string failed

Hi,

I am trying to send with tcp structs. I tried to copy the struct to some
buffer, then to initial std::string with this buffer.
In the other side i am writing the std::string to a buffer then copy to
target struct.
I am using memcpy and doing memset to the structs.
i am using MSVC 6.0 sp5 on windows 2000.

I tried do it in the following example, which doesnt work properly. what is
the problem?

struct alpha{
 long al;
 char c;
 double k;

Quote:
};

struct alpha a1;
 memset(&a1, 0, sizeof(struct alpha));
 a1.al = 5;
 a1.c = 'f';
 a1.k = 9.5;

 char buf1[sizeof(struct alpha)];
 memset(buf1, 0, sizeof(struct alpha));
 memcpy((void*)buf1, (void*)&a1, sizeof(struct alpha));

 std::string str = buf1;

 char buf2[sizeof(struct alpha)];
 memset(buf2, 0, sizeof(struct alpha));
 struct alpha a2;
 memset(&a2, 0, sizeof(struct alpha));
 memcpy((void*)buf2, (void*)str.c_str(), sizeof(struct alpha));
 memcpy((void*)&a2, buf2, sizeof(struct alpha));

Thanks



Mon, 10 May 2004 04:44:47 GMT  
 memcopy of structs to std::string failed
std::string constructor you use expects NULL-terminated C-style string as a
parameter. buf1 in your example has the second byte equal to 0, so the
string will be initialized with one character.

Why do you need to put binary data into std::string anyway?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat, and
wrong." H.L. Mencken


Quote:
> Hi,

> I am trying to send with tcp structs. I tried to copy the struct to some
> buffer, then to initial std::string with this buffer.
> In the other side i am writing the std::string to a buffer then copy to
> target struct.
> I am using memcpy and doing memset to the structs.
> i am using MSVC 6.0 sp5 on windows 2000.

> I tried do it in the following example, which doesnt work properly. what
is
> the problem?

> struct alpha{
>  long al;
>  char c;
>  double k;
> };

> struct alpha a1;
>  memset(&a1, 0, sizeof(struct alpha));
>  a1.al = 5;
>  a1.c = 'f';
>  a1.k = 9.5;

>  char buf1[sizeof(struct alpha)];
>  memset(buf1, 0, sizeof(struct alpha));
>  memcpy((void*)buf1, (void*)&a1, sizeof(struct alpha));

>  std::string str = buf1;

>  char buf2[sizeof(struct alpha)];
>  memset(buf2, 0, sizeof(struct alpha));
>  struct alpha a2;
>  memset(&a2, 0, sizeof(struct alpha));
>  memcpy((void*)buf2, (void*)str.c_str(), sizeof(struct alpha));
>  memcpy((void*)&a2, buf2, sizeof(struct alpha));

> Thanks



Mon, 10 May 2004 05:01:27 GMT  
 memcopy of structs to std::string failed
I think that using the pointer that .c_str() or .data()
methods return is not safe (it works in MSVC6.0 but is not
guaranteed to work in other STL implementations)

Try this:

struct S {
   int x;
   float y;

Quote:
};

S s;

std::string str;
//-- The following line is equivalent to "str = s;"
str.assign ((char*)&s, sizeof(s));
//-- or --
str.assign ((const char*)&s, sizeof(s));

//-- The following line is equivalent to "s = str;"
str.copy ((char*)&s, sizeof(s));

//-- The following line is equivalent to "std::string str2
= s;"
std::string str2 ((char *)&s, sizeof(s));
//-- or --
std::string str2 ((const char*)&s, sizeof(s));

Some people use std::string for binary data, see the
Crypto++ library (http://www.eskimo.com/~weidai/cryptopp).

I use basic_string<unsigned char> for binary data (for
several purposes it is better that vector<unsigned char>),
but I had to write an char_traits class (like the
char_traits class that is defined for the std::string
class)  to speed it up.

Quote:
>-----Original Message-----
>Hi,

>I am trying to send with tcp structs. I tried to copy the
struct to some
>buffer, then to initial std::string with this buffer.
>In the other side i am writing the std::string to a
buffer then copy to
>target struct.
>I am using memcpy and doing memset to the structs.
>i am using MSVC 6.0 sp5 on windows 2000.

>I tried do it in the following example, which doesnt work
properly. what is
>the problem?

>struct alpha{
> long al;
> char c;
> double k;
>};

>struct alpha a1;
> memset(&a1, 0, sizeof(struct alpha));
> a1.al = 5;
> a1.c = 'f';
> a1.k = 9.5;

> char buf1[sizeof(struct alpha)];
> memset(buf1, 0, sizeof(struct alpha));
> memcpy((void*)buf1, (void*)&a1, sizeof(struct alpha));

> std::string str = buf1;

> char buf2[sizeof(struct alpha)];
> memset(buf2, 0, sizeof(struct alpha));
> struct alpha a2;
> memset(&a2, 0, sizeof(struct alpha));
> memcpy((void*)buf2, (void*)str.c_str(), sizeof(struct
alpha));
> memcpy((void*)&a2, buf2, sizeof(struct alpha));

>Thanks

>.



Mon, 10 May 2004 20:28:17 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. convert non std::string to std::string

2. convert between std::string and std::wstring

3. Warnings for std::vector<std::string>

4. typedef std::vector<std::string> Vector_String

5. std::hash_map with std::string as a key

6. basic std::stringstream and std::string questions

7. int to std::string to System::String

8. static std::string exposed through System::String *

9. String to std:string

10. System::String to std::string

11. System.String to std::string conversion

12. null-terminated string to std::string

 

 
Powered by phpBB® Forum Software