Skip to main content
GameDev.net gamedev.net
🔒 Locked

char* problems

Started by ms291052 Jan 13, 2004 at 7:32 PM 2 replies 400+ views
Original Post
ms291052
ms291052
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]
SiCrane
SiCrane
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().
VenDrake
VenDrake
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."
-------------VenDrakeTo understand recursion, you must first understand recursion.
Zahlman
Zahlman

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++;
}


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.