Sign in to follow this  

hmm struct mem error

This topic is 3872 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

i am writing a struct into a bin file then reading it but i keep getting errors with strcat.asm when i try to do // keep getting errors here strcpy(myData.pDriverToDriver->opponent, "blah2"); myData.pDriverToDriver->respect = 0.2f;
#include <stdio.h>
#include <string.h>
 
struct _DriverToDriver 
{
	char opponent[32];
	float respect;
};
struct _DriverData 
{
	float skill;
	char name[32];
	_DriverToDriver *pDriverToDriver;
};
 
void writeBinary(const struct _DriverData);
void readBinary(struct _DriverData *);
 
void main() 
{
	struct _DriverData myData, inData;
	struct _DriverData *pDriverData;
 
	/* the memory address we will send to read binary */
	pDriverData = &inData;
	/* assign some dummy values */
	myData.skill = 0.1f;
	strcpy(myData.name, "blah1");
 
	// keep getting errors here
	strcpy(myData.pDriverToDriver->opponent, "blah2");
	myData.pDriverToDriver->respect = 0.2f;
 
	/* Write the struct myData and then read it back in */
	writeBinary(myData);
	readBinary(pDriverData);
}
 
void writeBinary(const struct _DriverData j) 
{
	FILE *outFile;
	/* open the file we are writing to */
	outFile = fopen("binout.txt", "w");
	/* use fwrite to write binary data to the file */
	fwrite(&j,sizeof(struct _DriverData),1,outFile);
	fclose(outFile);
}
 
void readBinary(struct _DriverData *b) 
{
	FILE *inFile;
	inFile = fopen("binout.txt", "r");
	fread((struct _DriverData *)b,sizeof(struct _DriverData),1,inFile);
	fclose(inFile);
}

Share this post


Link to post
Share on other sites
A char array is really just a pointer to an array in memory. I'm pretty sure the sizeof your struct is just 8 (4 bytes for the float, and 4 bytes for the pointer). Thus, when you are writing it to the file, you are writing out the memory address where the string is located rather than the string itself. To write it you need to do something like:


void writeBinary(const struct _DriverData j)
{
FILE *outFile;
/* open the file we are writing to */
outFile = fopen("binout.txt", "w");
/* use fwrite to write binary data to the file */

//write the float
fwrite(&j.respect,sizeof(float),1,outFile);

//write the string (note no '&' character here)
fwrite(j.opponent,sizeof(char),32,outFile);

fclose(outFile);
}





then do the reverse to read.

-me

Share this post


Link to post
Share on other sites
thanks for that but the error happens on the lines i said before that function

Share this post


Link to post
Share on other sites
Quote:
Original post by Prog101
i am writing a struct into a bin file then reading it but i keep getting errors with strcat.asm when i try to do
// keep getting errors here
strcpy(myData.pDriverToDriver->opponent, "blah2");
myData.pDriverToDriver->respect = 0.2f;


*** Source Snippet Removed ***


If i'm not mistaken you get an error because you're trying to write to a uninitialized piece of memory. You have a pointer to a variable of struct _DriverToDriver. When you create a variable of type _DriverData a pointer to type _DriverToDriver is created but not initialized. You need to initialize it like you initialized pDriverData. That's why you can:


strcpy(myData.name, "blah1");






because it's just a char-array, but you can't do this:


strcpy(myData.pDriverToDriver->opponent, "blah2");






because myData.pDriverToDriver isn't pointing anywere.

Hope this helps.

EDIT:

This should work:



struct _DriverToDriver
{
char opponent[32];
float respect;
};
struct _DriverData
{
float skill;
char name[32];
_DriverToDriver *pDriverToDriver;
};

void writeBinary(const struct _DriverData);
void readBinary(struct _DriverData *);

void main()
{
struct _DriverData myData, inData;
struct _DriverData *pDriverData;

// ADDED THESE TWO LINES
struct _DriverToDriver myDriverToDriver;
myDate.pDriverToDriver = &myDriverToDriver;

/* the memory address we will send to read binary */
pDriverData = &inData;
/* assign some dummy values */
myData.skill = 0.1f;
strcpy(myData.name, "blah1");

// keep getting errors here
strcpy(myData.pDriverToDriver->opponent, "blah2");
myData.pDriverToDriver->respect = 0.2f;

/* Write the struct myData and then read it back in */
writeBinary(myData);
readBinary(pDriverData);
}





But this is ugly coding. You should have a constructor within your _DriverData struct in which you initialize the _DriverToDriver pointer. Think of what i did as an ugly quick fix, don't use this code in reallife code. I just tried to make a point ok? ;)

EDIT 2:

Oh shoot, here's what i described above:


struct _DriverToDriver
{
char opponent[32];
float respect;
};
struct _DriverData
{
float skill;
char name[32];
_DriverToDriver *pDriverToDriver;

_DriverData() {
pDriverToDriver = new _DriverToDriver();
}
};

void writeBinary(const struct _DriverData);
void readBinary(struct _DriverData *);

void main()
{
struct _DriverData myData, inData;
struct _DriverData *pDriverData;

/* the memory address we will send to read binary */
pDriverData = &inData;
/* assign some dummy values */
myData.skill = 0.1f;
strcpy(myData.name, "blah1");

// keep getting errors here
strcpy(myData.pDriverToDriver->opponent, "blah2");
myData.pDriverToDriver->respect = 0.2f;

/* Write the struct myData and then read it back in */
writeBinary(myData);
readBinary(pDriverData);
}



There you go, this is a lot cleaner than before and it works, i've tested it a second ago.

[Edited by - Oguz286 on June 11, 2007 6:42:19 PM]

Share this post


Link to post
Share on other sites

This topic is 3872 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this