casting a class to a string and vice versa

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
irruser
Posts: 34
Joined: Thu Jul 26, 2007 10:18 am
Location: Scotland

casting a class to a string and vice versa

Post by irruser »

my current code is

Code: Select all

struct Data {
	int i;
	int y;
	Data(){i,y=0;}
};

	char * chr ="";

	Data * data;

	chr = (char *)&data;

	Data * d = new Data();

	d->i = 0;

	printf("%s\n",chr);

	d = reinterpret_cast<Data*>(&chr);

	printf("I = %i\n",d->i); // error here
when i print the d->i , i get strange values. is there any other way to do this?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You are not casting a class to a string at all. Your code dangerous at best.
is there any other way to do this?
I think a better question would be... what are you trying to do? I'm sorry, but there are so many things wrong with that code I don't even know where to start.

Travis
irruser
Posts: 34
Joined: Thu Jul 26, 2007 10:18 am
Location: Scotland

Post by irruser »

:? im trying to convert the class Data to a char array so i can send it or write it to a file then convert it back to class Data for modifying.
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

... Why?


Nobody told you about serialization, did they?

Save the data into a file, with a field showing what class it is, then on load, create a new instance of the class with the saved data.

You cannot cast a class to a char* and expect to get anything useful. Plus I highly doubt you will ever get that to load correctly on any other machine, or even your own machine after you close and restart the application.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It sounds like he is trying to serialize the object, just in a very primitive way. Converting to a string is not the answer. There will be all sorts of problems because the first null byte would terminate the string.

If your objects are pod (plain old data), then you can just write them out (using fwrite() or send()), making sure to handle endian issues as necessary.

You should probably take a look at the scene node serialization code (ISceneNode::serializeAttributes() and its ilk) which use xml.

Travis
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

vitek wrote:there are so many things wrong with that code I don't even know where to start.
Ditto,

Code: Select all

d = reinterpret_cast<Data*>(&chr); 
Here you are casting a char** (Note the double pointer!) that eventually leads to another double pointer (Data**), that eventually leads to an uninitialized Data pointer! In all honesty, simply keeping track of all the absurdities is giving me a headache...

How about we start from scratch?

Code: Select all

struct Data 
{
   int i;
   int y;
   Data() : i(0), y(0){} // Use a member initialization list here, it's a little faster. (Although for primitive data types like integers that's a moot point.)
};

   // Put the class in a char buffer.
   char* buffer = new char[sizeof(Data)];
   Data* data = new Data();
   memcpy(buffer, data, sizeof(Data));

   // Read it back.
   Data* anotherData = new Data();
   memcpy(anotherData, buffer, sizeof(Data));

   printf("I = %d\n",anotherData->i);

   // DELETE EVERYTHING!!!
   delete data;
   delete anotherData;
   delete [] buffer; // Make sure to use [] here!, since it is an array.
Cheers, this should do the trick...

EDIT: Posted at the same time as Vitek!, I fully agree with what he's saying, simply putting them in a string isn't doing anything useful, since you have to copy the memory to the file/binary socket whatever eventually anyway, it's better to do it in one go. Eg. fwrite(data, sizeof(Data), 1, file);

For converting between endianness check out htonl() and ntohl() (Host to net long and net to host long.).
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
torleif
Posts: 188
Joined: Mon Jun 30, 2008 4:53 am

Post by torleif »

Code: Select all

core::string<char> myString = "Hello world!\n"
People seem to forget that the hard parts of programming have been taken care of

If this is a learning exercise, I apologize, and recommend you experiment more. it's the best way of learning
irruser
Posts: 34
Joined: Thu Jul 26, 2007 10:18 am
Location: Scotland

Post by irruser »

thanks very much :D . and sorry for the very bad coding I'm still a noob :oops: .
Post Reply