Skip to main content
gamedev.net
🔒 Locked

Strange Allocation Problem

Started by Bagpuss Apr 11, 2002 at 3:07 AM 2 replies 100+ views
Original Post
Bagpuss
Bagpuss
I am just debugging some code I have written and have noticed that some of my output isn''t what I would expect. I have a struct called Header that contains various bits of info in a file header. When the header is created I can watch my variables get assigned to the values I want, then one of them gets changed by another line of code without actually being referenced. I assume it''s a memory / address problem, and it is something I am doing, but can anyone see what and tell me how to get round it ?
  
//Write Data to the File Structure Header

			
		
	Header.cMajorV = intVersionMajor;
	Header.cMinorV = intVersionMinor;

	char *buffer;

	strcpy(buffer, "F");
	Header.cFlags = *buffer;
			
	//Somehow, this code is Overwriting cMajorV

	strcpy((char*)Header.cSignature, "DATA");
  
I have gone into the code and monitored it line by line, and it is definitely the strcpy that is doing this. As an experiment I tried doing the same thing with a buffer area (see the lines above) and that doesn''t seem to have the same problem, but why should using an intermediary in this manner solve my problem ? TIA Bp
LessBread
LessBread
Well, it would probably be helpful to show how the header structure is defined. Judging from the cast, I would guess that signature isn''t a char array with 4 elements and rather than fill an int with 4 bytes your wiping out 4 members of the struct. You might try using memcpy instead.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.