Jump to content
  • Advertisement
Sign in to follow this  
TFS_Waldo

"strcpy" leaves destination string with all random chars. =(

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

Can someone PLEASE help me? I have been working on an engine for the past couple of days. I am using SDL to include 2D and 3D (OpenGL) support. Well, I am trying to come up with some sort of a small scripting engine, I guess you would call it. Well, I am trying to create some sort of command system. Example: cmd_ExecCommand("CmdName"); And you create a command like this: cmd_AddCommand("CmdName", pfuncPtr); "pfuncPtr" is a function pointer that is called when you call "cmd_ExecCommand." Well, here is how I add a command:
	strcpy(cmdInfo.m_szCmdName, szCmdName);   // Copy command name
	cmdInfo.m_pCmdFunc = pCmdFunc;            // Copy command function pointer

	m_pCmds.push_back(&cmdInfo);      // Add command to the list
"m_pCmds" is "vector<cmdinfo_t*> m_pCmds;" and "cmdinfo_t" is:
typedef struct       // Struct. for command information
{
	char      m_szCmdName[20];   // Command name (used to search for and exec. commands)
	cmdfunc_t m_pCmdFunc;        // Command function pointer
} cmdinfo_s;      // Command info. struct.
Where "cmdfunc_t" is "typedef void (*cmdfunc_t)(void);" Now, when I call the "strcpy" line, and the print on the screen "cmdInfo.m_szCmdName" it says "╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠@↔å" and I have even tried to change the "strcpy" 2nd parameter to "ACmd." And I get the same result. So, when I call "cmd_ExecCommand("ACmd");" I look through the "m_pCmds" object for a command with the name "ACmd" and it never finds it, because it is not copying the string correctly. If you could help I would really appreciate it. Thanks! =)

Share this post


Link to post
Share on other sites
Advertisement
I'm willing to bet your actual problem is that you are pushing back a pointer to an object that is going out of scope. Try to change m_pCmds to store your commands by value rather than by pointer.

// insert obligatory comment about use std::string instead of character arrays here.

Share this post


Link to post
Share on other sites
typedef void (* ScriptCallableFunction)(void);
typedef std::map< std::string, ScriptCallableFunction > ScriptFunctions;
ScriptFunctions scriptCallableFunctions;

void addCommand(std::string commandName, ScriptCallableFunction function)
{
scriptCallableFunctions.insert(std::make_pair(commandName, function));
}

void execCommand(std::string commandName)
{
ScriptFunctions::const_iterator command = scriptCallableFunctions.find(commandName);
if (command != scriptCallableFunctions.end())
{
// invoke function
command->second();
}
else
{
// handle error
}
}


Enigma

[Edited by - Enigma on July 27, 2005 6:39:10 PM]

Share this post


Link to post
Share on other sites
Well first of all you shouldnt be using strcpy anways. Its really easy to overflow buffers like that. The problem here is most likley that your dest buffer is not NULL terminated. When you call printf it will read the buffer until it hits a NULL. If the buffer is not NULL terminated it will read past the buffer and spit random contents of memory (info leak). Try using snprintf instead. It will always terminate the buffer with a NULL.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Did you #include ?

Share this post


Link to post
Share on other sites
Make sure you #include <map>. Also note the edit to my original post (corrected (*command)() to command->second()).

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!