Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ms291052

char* problems

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

As of now I've implemented a message queue type thing into my app. Recently, however, I found that the following lines were outputting bogus garbage (more specifically ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ). char* temp = new char[999]; sprintf(temp, "ID is %d", ID); AddMessage(temp); delete[] temp; I also noticed that I was using g_Messages[g_NumMessages] = message (see below) instead of strcpy(). After chaging it to strcpy (as it is in the example below) it now just acts like I'm dereferencing a NULL pointer. I assume the problem is in AddMessage which is defined as char* g_Messages[10] = {"", "", "", "", "", "", "", "", "", ""}; int g_NumMessages = 0; void AddMessage(char* message) { if(g_NumMessages == 9) { for(int i=0;i<9;i++) g_Messages[i+0] = g_Messages[i+1]; strcpy(g_Messages[9], ""); if(g_NumMessages > 0) g_NumMessages--; } strcpy(g_Messages[g_NumMessages], message); g_NumMessages++; } Any thoughts as to why? (And please no flames telling me to use std::string, as I dislike having c_str() return a const and I dislike casting out of const variables). Edited to change i to i+0 because it was giving me italics. [edited by - ms291052 on January 13, 2004 8:33:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
You aren''t allocating storage for your strings. Most likely you''re trashing static memory with your strcpy() calls, which, in turn, would likely give you memory access errors. If you''re intent on using bare char *''s for your string needs, consider looking up strdup().

Share this post


Link to post
Share on other sites
ms291052,

Change this to a strcpy.
g_Messages[i+0] = g_Messages[i+1];


char* g_Messages[10] = {"", "", "", "", "", "", "", "", "", ""};
This line creates 10 strings. Each one is zero bytes long + 1 byte reserved for a null terminator.

strcpy(g_Messages[g_NumMessages], message);
If message has "Hello World" in it, this will copy 12 bytes. (11 bytes for the text + 1 for the null) You''re string doens''t have enough room for this. Your string doesn''t have ANY room in it.

Change your definition to this and you will see an improvement.
char* g_Messages[10] =
{
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};

This is not the ideal solution, but it should illustrate your problem.

Lastly, change your strcpy() to a strncpy(). This will make sure you don''t copy more memory than your buffer can hold.


String memory is a subject that I''ve seen professional programmers strugle with. Don''t get discouraged. Just try to understand where you memory is, how much you have, and when it enters and leaves scope.



-----------
VenDrake

"My stupid jar is full. I can''t talk to you anymore."

Share this post


Link to post
Share on other sites

char* temp = new char[999];
sprintf(temp, "ID is %d", ID);
AddMessage(temp);
// DON''T delete[] temp; here!


char* g_Messages[10];
int g_NumMessages = 0;
void AddMessage(char* message)
/* I''m trying to respect your indentation style here... */
/* If you HAVE to be paranoid and make a copy, OTOH, try something like:
char * copy = new char[strlen(message)];
strcpy(copy, message);
and then change ''message'' to ''copy'' below. Of course in this case you DO need to delete[] temp outside the function. This is why garbage collection is your friend. Ok, done evangelizing. :) */

{
if(g_NumMessages == 9)
{
for(int i=0;i<9;i++) g_Messages[i+0] = g_Messages[i+1];
g_Messages[9] = NULL;
/* This is silly. Of course g_NumMessages > 0! */
if(g_NumMessages > 0) g_NumMessages--;
}
g_Messages[g_NumMessages] = message;
g_NumMessages++;
}


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!