gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > game save: memory block copying

#176999 - blessingta@hotmail.co.uk - Sat Nov 19, 2011 12:11 am

I'm trying to create a function to copy memory for what will be my game saves in bytes. And so far I have been trying to make my algorithm work in c++ but it refuses to work.

the problem is where
Code:
*pt_destination++ = *pt_source++;
its complaining that I'm not allowed to access this.

why is it?

Code:
//why wont this function work?

void* memory_copy(void * x_destination, void const * x_source, size_t bytes)
{
   //cast destination to the size of a byte
char * pt_destination = (char*) x_destination;
//cast source to the size of a byte
char const *pt_source = (char const *) x_source;

//loop terminates when all the bytes have been copied
while (bytes--)
{
*pt_destination++ = *pt_source++;

}
return (x_destination);
}

#177000 - elhobbs - Sat Nov 19, 2011 1:08 am

I think you want "const char *pt_source" not "char const *pt_source"

#177001 - blessingta@hotmail.co.uk - Sat Nov 19, 2011 10:25 am

I still get the error after the code modifications:

error message
Quote:
An unhandled exception of type 'System.NullReferenceException' occurred in memorycopying.exe

Additional information: Object reference not set to an instance of an object.



Code:
// memorycopying.cpp : main project file.

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace System;

//MEMORT COPY
//Arguements:
//void * x_destination: what we are copying to
//void * x_source: what we are copying from
//size_t bytes: amount bytes to be copied
//Purpose: For copying a block of code which will be used as a gamesave else where
void* memory_copy(void * x_destination,  void const* x_source, size_t bytes);

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

   char * Message = "whats good blessman? whats really good?boondocks reference";
   char * whats_my_message;
   //memory copied
   memory_copy(whats_my_message,Message,sizeof Message);

   std::cout<<whats_my_message<<std::endl;
    return 0;
}


void* memory_copy(void * x_destination,  void const* x_source, size_t bytes)
{
   //cast destination to the size of a byte
char * pt_destination = (char*) x_destination;
//cast source to the size of a byte
const char *pt_source = (const char*) x_source;

//loop terminates when all the bytes have been copied
while (bytes--)
{
*pt_destination++ = *pt_source++;

}
return (x_destination);
}
[/quote]

#177002 - Dwedit - Sat Nov 19, 2011 5:21 pm

The memory copy function itself looks fine, but I can't say the same about the rest of the code. It's a total mess. Lots of .NET framework specific code there, I thought you were targeting the GBA?

Lots of fundamental misunderstandings with C code here.
Let's start with this line:
char * Message = "whats good blessman? whats really good?boondocks reference";

That's a string literal. The memory for the string itself lives in the .rodata section. On the GBA, that's in cartridge ROM. You created a local variable that points to the string literal. This is not the same as creating a local variable that is a *copy* of the string. For string literals living in a read only section, a pointer to them should always be const.

If you want to easily create a writable copy of the string, declare it as an array instead: char Message[] = "..."; This create a char array that lives in the stack, and it is initialized to a copy of your literal string. You can't enlarge this, but you can modify it.


On the next line, you are creating a local variable that doesn't point to anything. It's not even initialized to anything. Of course you get a null pointer exception, you're writing to address 0, that's exactly what a null pointer exception is.

In order to copy memory, you need memory to copy to. The memory could live on the stack, or in the heap.
"xx = malloc(80);" will allocate 80 bytes on the heap. So will "xx = new char[80];" "char xx[80];" will create a local array on the stack that you can write to.

Note that you need to know the size of the memory. If you write past the end of your allocated memory, you've create a buffer overflow bug, and the memory after it gets corrupted. There's no automatic bounds checking for these kind of things.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#177003 - blessingta@hotmail.co.uk - Sun Nov 20, 2011 2:23 am

thanks, but I had just been trying out a memory copy algorithm before I got it onto asm. It turns out threre was another problem wrong with it, but it worked when I re-compiled the test again in visual studio.