app crash n burn. char arrays

Started by
7 comments, last by MotionCoil 19 years, 5 months ago
What is going on here?!?! for some reason both my mate and I cannot fathom why this is screwing up. It feels such a noob problem though. ok, i got a console kinda up and running within my engine. When the console is in focus (and a key is pressed) the function ConsoleFocus reads the key and does its respective routine. I declare line as a global char pointer as so: char *Line; In the engines initialization i then initialize it as a char array thus: Line = new char[64]; in the class CConsole I have a public variable called CurrentLine. This is decared in the public space like so: char *CurrentLine; in the CConsole::CConsole() constructor, I have it initialized as thus: CurrentLine = new char[64]; in the ConsoleFocus function I then have these lines for when 'A' is pressed: ... strcat(Line,"a"); strcpy(gConsole->CurrentLine, Line); ... at this point i get a mother of an AV error. Apparently its an access violation in strcat. I cannot in any way, shape or form see how its an AV. I appreciate any help. this problem...Its really annoying
Advertisement
You have not initialised Line. Therefore you have no guarantee that it is null terminated. strcat requires a null terminated string to append to.

That said, why on Earth are you using raw character arrays instead of std::strings?

Enigma
Quote:Original post by MotionCoil
for some reason both my mate and I cannot fathom why this is screwing up.

char *


"Doctor, it hurts when I use char-stars to represent text."
ok.
You won me round.
I am now utilizing std::string :)
I've only worked in C, but if C++ is just an addon to C, this should work.

char chrLine[64];char chrCurrentLine[64];strcat(chrLine, "a");strcpy(chrCurrentLine, chrLine);[/soure]And thats it, no fancy symbols or special keywords. [razz]
AP: As I stated before, strcat requires two null-terminated char arrays. Your code declares a 64 character stack array but does not initialise it. It therefore contains junk values from the stack and likely does not contain a terminating null.

Enigma
Quote:Original post by Enigma
AP: As I stated before, strcat requires two null-terminated char arrays. Your code declares a 64 character stack array but does not initialise it. It therefore contains junk values from the stack and likely does not contain a terminating null.

Enigma


Yes, however, my first calls on a string are usually strcpy, hence why I never have to clear it. In this case, he would indeed have to create null pointer.

Quote:chrLine[0] = '\n';
chrCurrentLine[0] = '\n';



lol, perhaps a moderator can clean up my two replies. By null pointer, I ment terminating null. And the quote was suppose to be a source bracket. Even in the early post, I spelt [/sorce] lol...
Thanks for the replies.
I'm now sorted. I've flipped to std::strings and they are working well.
Now i gotta write the rest of my console logic.

Cheers again!

This topic is closed to new replies.

Advertisement