Thread: mysterious overwriting of memory

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    Unhappy mysterious overwriting of memory

    I'm making this text demo for the GBA. Anways I'm making this function to convert ASCII character to my characters. For some reason when I try to generate a string using this function all of my image data gets overwritten. Well, here is the code for that function:
    Code:
    u16 * ascii_to_mine(const char * str, const u8 len)
    {
    	u16 * mstr = new u16[len];
    	
    	u16 loop;
    	for (loop = 0; loop < len; loop++)
    		mstr[loop] = (u16)(str[loop] - 32);
    }
    u16 and u8 are just typdefs for unsigned short and unsigned char. Is there anything here that is kinda funny? I know the problem isn't my method of writing the data since I converted some strings manually and everything went just fine. Thanks for your help!

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    mstr[loop] = (u16)(str[loop] - 32);

    What exactly are you trying to do with this line? I don't see how this is converting the old text to your characters. Unless you some how defined your characters to be 32 less than the normal ASCII characters, you're just changing them to other ASCII characters.

    IE: 'A' would become '7'

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    No, this is excactly what I need to do. My program uses all ASCII characters 32-100 and something. My program has the images of each characters stored in special places in the GBAs memory and I need to use the numbers 0-something to access the images and make them draw onscreen. I'm not sure if I explained that well, but that is excactly what I mean to do.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    u16 * ascii_to_mine(const char * str, const u8 len)
    {
    	u16 * mstr = new u16[len];
    	
    	u16 loop;
    	for (loop = 0; loop < len; loop++)
    		mstr[loop] = (u16)(str[loop] - 32);
    }
    Well, you're not returning anything, so you're doing a lot of work for nothing. So add a return at the end.
    Code:
    u16 * ascii_to_mine(const char * str, const u8 len)
    {
    	u16 * mstr = new u16[len];
    	
    	u16 loop;
    	for (loop = 0; loop < len; loop++)
    		mstr[loop] = (u16)(str[loop] - 32);
    	return mstr;
    }
    And I'm assuming you use len to determine the end of your new string. Otherwise you'd need an extra slot for a terminator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  3. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM