Help! i try to use strcpy and it gives me an access violation error and brings up strcat.asm

Ok not exactly sure what's going on. I know my code is pretty atrocious. Basically im loading in an XML file, using tinyXML im reading it then using a struct (struct Week) that holds an array of another struct(struct Day) im getting all the data i need from the XML file and initialising a member variable of my Weather class (Week m_wCities[7]).
The XML file holds data on the weather such as mintemp, maxtemp, condition for each day. Anyway, all of that is working fine,for example if i put in a break point i can see what m_wCities[0].m_Days[0].m_DayofWeek stores. so now i need to draw it onto a a map of Australia using DrawString();.

this is the code i have written:

void Weather::DrawToScreen()
{
char acEverything[256];
strcpy(acEverything, m_wCities[0].m_Days[0].m_DayofWeek);
strcat(acEverything," ");
strcat(acEverything, m_wCities[0].m_Days[0].m_currentTemp);
strcat(acEverything,"\n");
strcat(acEverything,m_wCities[0].m_Days[0].m_MinTemp);
strcat(acEverything,"/");
strcat(acEverything,m_wCities[0].m_Days[0].m_MaxTemp);
DrawString(acEverything, MelbourneXPos ,MelbourneYPos , 20);
}

I then have

Weather DrawString;

DrawString.DrawToScreen(); in main()

When i run this with a breakpoint on the strcpy line, as soon as i go to the next line it breaks and strcat.asm appears.

What am i doing wrong. Probably everything. Im still trying to get my head around the basics of classes, so ive probably set it all up wrong. any help would be very much appreciated.


Last edited on
To really help you, we would need to see the definition of the class of m_wCities. What are the types of m_DayofWeek, m_currentTemp, etc. Are all those variables of type char*?

Other than that, I would rather use sprintf or stringstream to build that kind of string instead of strcpy/strcat combo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct Day
{
	const char* m_DayofWeek;
	const char* m_currentTemp;
	const char* m_MinTemp;
	const char* m_MaxTemp;
	const char* m_Condition;
		
};

class Weather
{

public:

	Weather(){};
	
	~Weather(){};

	Week* GetWeather(const char* DayofWeek, const char* CurrentTemp, const char* MinTemp, const char* MaxTemp, const char* Condition, Cities::eCityNames i, int j);
	void DrawToScreen(Cities::eCityNames i, int j);
	

private:
	Week m_wCities[Cities::eCityNames::AMOUNT];
	

	
};


thats my day struct and weather class. my GetWeather() function returns all the data i need which i then try put into the DrawToScreen() function.
If it helps at all i tried this in my GetWeather() function:
1
2
3
4
m_wCities[i].m_Days[j].m_DayofWeek = DayofWeek;
std::cout << m_wCities[i].m_Days[j].m_DayofWeek << std::endl;
//prints out "Tue"


while this doesnt work

1
2
3
std::cout << m_wCities[0].m_Days[0].m_DayofWeek << std::endl;

//get and access violation 

that being the spot is should have put the data.
Ok, at least the data types are correct.
This looks like a problem of data handling. Perhaps, the strings (const char*) are allocated by the XML reader, then you simply copy the pointers to your data structures, and then delete the reader (which in turns deletes the allocated strings). Then when you have dangling pointers (pointers to memory regions that were free'd) and any operation that you try to do with them logically leads to an exception (access violation).

I am pretty sure that is the problem you have, but it would really help if you show how do you fill your data structures.
Topic archived. No new replies allowed.