Copying between arrays problem

This is a discussion on Copying between arrays problem within the C Programming forums, part of the General Programming Boards category; Hi Guys, I'm doing a 1Mb memory dump like this: Code: for (int i = 0; i < 0x00100000; i++) ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    69

    Copying between arrays problem

    Hi Guys,
    I'm doing a 1Mb memory dump like this:
    Code:
    for (int i = 0; i < 0x00100000; i++) {
    dump[i] = *(chipmemory+i);
    } // i
    
    Then I save the 1Mb "dump" array to a file, and the file contains the data I expect.

    The problem arises when I try to write data back to the array beginning at the "chipmemory" pointer:
    Code:
        unsigned char msga[18] = "SOME MODIFIED DATA";
    
    
        int address = 172378;
        for (int i = 0; i < 18; i++) {
        *(chipmemory+address) = msga[i];
        address++;
        } // i
    
    Is this the correct way to write back to an address 172378 bytes
    from the "chipmemory" pointer? or is my code broken somewhere else?
    Cheers, Art.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Where did you get the 172378 address from?

    The first element of chipmemory array will NOT be chipmemory+172378. Try chipmemory+0.
    Last edited by Adak; 06-12-2013 at 08:01 AM.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    Hi,
    I'm not trying to write the whole 1Mb back to chipmemory.
    Just the string (in this example) starting at address 172378 in chipmemory,
    which was it's address in the 1Mb dump.

    So the syntax is fine?
    It's got me beat then..

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Check you haven't got an "off by one" error with array indexing.

    There is also potentially a problem as an int is not guaranteed to be capable of representing a value of 172378. Try printing the value of address to check.

    Beyond that, you haven't provided information on how you are detecting there is a problem. So people would have to guess. For all we know, it is your test case that is flawed.

    Why not use the memcpy() function?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    Quote Originally Posted by grumpy View Post
    Check you haven't got an "off by one" error with array indexing.
    Hi, I would still notice an error like that in this case. I'm working with UAE (Universal Amiga Emulator),
    and trying to change the emulated computer's memory at runtime, after it has loaded a disk.

    The disk I'm testing with is a game with some scroll text in it's intro screen.
    I can open the disk file in a hex editor and see the scroll text plainly.
    I could edit it right there, but that isn't my overall goal.
    Then when I do the dump after the game has loaded,
    I can open the dumped memory in a hex editor, and it's there:
    http://img.photobucket.com/albums/v1...ps9f637d4e.png

    The bytes are all swapped, but if you use Hex Workshop's byte swap feature, you can see it.
    The 1Mb is the emulated computer's entire RAM memory, and the text is not duplicated anywhere.

    Rather than swap the bytes in my program, I've done it beforehand, and tried to replace some of
    the strings at the exact same point in time that I did the dump, before the scroll text is actually displayed
    by the emulated computer:
    Code:
     unsigned char msga[18] = "A IMAGH SAS  ATTRDE"; // AMIGA HAS STARTED
      unsigned char msgb[25] = "L aoidgnM geBala  liDks..."; // Loading MegaBall Disk...
      unsigned char msgc[22] = "G ma e &umis coldade  !"; // Game & music loaded!
      unsigned char msgd[18] = "T PAT  OOCTNNIEU .";  // TAP TO CONTINUE
    Just to be sure, I've even written it into memory for every time the emulated floppy disk head moves!

    There is also potentially a problem as an int is not guaranteed to be capable of representing a value of 172378
    I thought an int could be much greater than that.
    The actual values are: 172378, 172396, 172466, and 172488.
    but even if I indexed it a little wrong, the scroll text should be still messed up.
    I've also tried just writing a bunch of X characters in a slab all over it,
    and it changes nothing.

    It seems like the simplest of problem.
    Last edited by xArt; 06-12-2013 at 09:33 AM.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    I tried this, which is explicit:
    Code:
      
       memcpy(chipmemory+172378, msga, 18);
        memcpy(chipmemory+172396, msgb, 25);
        memcpy(chipmemory+172466, msgc, 22);
        memcpy(chipmemory+172488, msgd, 18);
    I just keep calling it before and after the disk has loaded.
    I might try writing to the memory this way, and then doing the dump again,
    but the scroll text isn't changing.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    After doing the memcpy to copy the message to the chipmemory, try copying it back to make sure it's really writing to the chipmemory.

    Also, it's possible that those locations are only temporarily used for display purposes and the actual locations of those messages could be elsewhere. Have you tried searching for all instances of "AMIGA HAS STARTED" in the chip memory to make sure there isn't another copy somewhere else?


  8. #8
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Is chipmemory declared as an array of unsigned char?
    If it is, your code looks like it should work.

    But if your emulator is emulating ROM, maybe it's refusing to let you write.
    Did you declare chipmemory? Or is it in a header provided by the emulator?
    It's quite possible to declare ROM writable (not const), but you still can't write it.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    Quote Originally Posted by rcgldr View Post
    After doing the memcpy to copy the message to the chipmemory, try copying it back to make sure it's really writing to the chipmemory.

    Also, it's possible that those locations are only temporarily used for display purposes and the actual locations of those messages could be elsewhere. Have you tried searching for all instances of "AMIGA HAS STARTED" in the chip memory to make sure there isn't another copy somewhere else?

    Yeah, I did mention there are no other instances of the messages.
    I am dumping the emulated computer's entire RAM.

    I have has success with it, but still don't know what I was doing wrong.
    I will just save a copy of the working program and hold onto it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying arrays...
    By darren78 in forum C++ Programming
    Replies: 7
    Last Post: 08-20-2010, 11:55 AM
  2. Replies: 5
    Last Post: 05-30-2010, 11:13 PM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  4. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 04:09 PM
  5. help on copying arrays
    By neversell in forum C Programming
    Replies: 3
    Last Post: 06-30-2002, 05:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21