Jump to content
  • Advertisement
Sign in to follow this  
ErUs

Strings in structs?

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

will putting strings in structs cause an error? and what about if i want to cast the struct to a (char*) and send over winsock? please fill me in.

Share this post


Link to post
Share on other sites
Advertisement
Depends on how you put the string in the struct.
If you put a string object

struct foo
{
std::string str;
};


Then you won't have any errors. Casting the struct to a char* and sending it over winsock will cause big problems. Here's why:

If you put an array in the struct, then the data from the array will be in the struct itself.

struct foo
{
char str[256];
};


The struct will actually contain room for 256 characters. Don't try to put more, that's a buffer overflow and the cause of many, many problems - including security flaws. If you cast the struct to char* and pass it to winsock, the text of the string will be sent along, because it is part of the struct.

However, if you do put a char* in the struct - as std::string does internally, then the text of the string is not in the struct. Only the pointer - the address at which the text can be found will be.

struct foo
{
char* str;
};


If you pass it to winsock, then you will be sending a pointer over. Outside of its original address space and, a fortiori, on another machine, the pointer is utterly meaningless and totally useless (you don't expect the data to also magically appear at that address do you? what if there already was something else there?)

Using char* for strings is also a source of potential problems, as you need to keeps track of memory allocations/deallocation - which if you make copies of the struct can be troublesome.

So, for common usages, you'll want to use a std::string. Struct containing such strings, like with many object types, should never be treated as raw char* data - first, you don't know what's in the std::string class, second, the compiler may add some stuff of its own in there, beyond the data members that have been declared (that's typical if you've got virtual member functions).

For serialization purposes, it is idiomatic to send the data member by member. That is, if your struct contains three std::string objects and a brace of integers, you don't just convert the struct to a char* and send it, but instead you send the three strings one after the other, followed by each of the ints. It is more to type, it represents more function calls, but it is correct.

Oh, and how do you send the contents of the strings over winsock, will you ask me? Simple, you can either use the c_str() member function if you want a null-terminated string, or the data() member function if you don't. If the string is not null-terminated and of variable length, it is useful to sent the string size immediately before sending the string itself. That way, the receiver immediately knows how big a string it is getting (and, after all, it doesn't have a priori any other way of finding this out).


struct foo
{
std::string str1;
std::string str2;
std::string str3;
int i1;
int i2;
};

foo f;

send_data( f.str1.c_str(), f.str1.length() );
send_data( f.str2.c_str(), f.str2.length() );

int size = f.str3.length();
send_data( (const char*)&size, sizeof(size));
send_data( f.str3.data(), f.str3.length() );

send_data( (const char*)&f.i1, sizeof(i1) );
send_data( (const char*)&f.i2, sizeof(i2) );


See, it's not that much more complicated.

Share this post


Link to post
Share on other sites
ok.

thanks alot you cleared that up.

but for network packets im going to use structs with char arrays. no pointers

like:

typedef struct {
int id;
char msg[200];
} chatpckt;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!