odd C++ bug(?)

Started by
24 comments, last by NukeCorr 14 years, 1 month ago
Quote:Original post by NukeCorr
Are there some critical errors/mistakes in them?

Yes. That code will invoke undefined behaviour.
	killsMsg[0] = temp;

This is wrong, because you delete temp at the end of the scope (although using new[]/delete[] is bad practice here, using an automatic array will give the same error). Change that line to this.
	killsMsg[0] = strdup(temp);

But ya know, if you used C++ and std::string instead of 1960's-era C this sort of thing wouldn't happen.

Edit: fixed strdup() function name.

[Edited by - Bregma on March 5, 2010 11:42:31 AM]

Stephen M. Webb
Professional Free Software Developer

Advertisement
strcpy takes 2 parameters, and i dont see where killsMsg is defined but it looks like it's used as a char * in the rest of the code. There is all sorts of wrong goin on in this code!!

Is this what was meant?

strcpy(killsMsg,temp);

or is killsMsg really a char **?
Quote:Original post by Atrix256
strcpy takes 2 parameters, and i dont see where killsMsg is defined but it looks like it's used as a char * in the rest of the code. There is all sorts of wrong goin on in this code!!

Is this what was meant?

strcpy(killsMsg,temp);

or is killsMsg really a char **?


Yea I know it has lot of mistakes although they seem to work ingame, killsMsg is:
std::string killsMsg[DEFAULT_KILLSMSG]; not char
What the h*ll are you?
Quote:Original post by NukeCorr
killsMsg is: std::string killsMsg[DEFAULT_KILLSMSG]; not char

If killsMsg is an array of std:strings, why not simply concatenate it as an std::string?
killsMsg[0] = Player[killer].name + " killed " + Player[victim].name + " with " + ProjInfo[weapon].name
Assuming that all those name properties are std::strings too of course.
Quote:Original post by Wan
Quote:Original post by NukeCorr
killsMsg is: std::string killsMsg[DEFAULT_KILLSMSG]; not char

If killsMsg is an array of std:strings, why not simply concatenate it as an std::string?
killsMsg[0] = Player[killer].name + " killed " + Player[victim].name + " with " + ProjInfo[weapon].name
Assuming that all those name properties are std::strings too of course.


You got a point... I used C# many years back and it's stuck in my head.

Thanks for that one
What the h*ll are you?
I might be doing something wrong here but.. I get this error from both killsMsg[0] lines

if(killer == victim)	killsMsg[0] = Player[killer].name + " committed a suicide";else	killsMsg[0] = Player[killer].name + " killed " + Player[victim].name;


They are both std::string

"error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class std
::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'"

EDIT: nevermind, forgot + ..works now

[Edited by - NukeCorr on March 6, 2010 3:51:50 AM]
What the h*ll are you?

This topic is closed to new replies.

Advertisement