Thread: Copying a memory area

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    182

    Copying a memory area

    I need to copy an array of 8000 bytes to a structure of 8000 bytes.
    Is it possible to do it with the function memcpy() ?

    These are the two objects:

    Code:
    char array[8000];
    CHAR_INFO starget[2000];
    What kind of pointer should I pass to memcpy() in order to copy
    the content of array[] into starget[] ?

    Is it enough to do:
    Code:
    memcpy(starget, array, 8000);
    ?
    Last edited by frktons; 07-30-2010 at 01:02 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    memcpy is the function you'd want to use if you want to avoid writing a loop, but it may be more complicated than just one function call. It depends on the specifics of the char info type.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by whiteflags View Post
    memcpy is the function you'd want to use if you want to avoid writing a loop, but it may be more complicated than just one function call. It depends on the specifics of the char info type.
    The CHAR_INFO type is a standard Windows type defined in the include:
    Code:
    typedef struct _CHAR_INFO { // chi  
        union {                /* Unicode or ANSI character  */ 
            WCHAR UnicodeChar; 
            CHAR AsciiChar; 
        } Char; 
        WORD Attributes;       // text and background colors 
    } CHAR_INFO, *PCHAR_INFO;
    And well, the memcpy() works just fine, I tried it and it works.

    I need to perform another operation on the structure, I need to
    access the single bytes one at a time. Can I do it using a pointer?

    Something like:
    Code:
    char *ptr_starget = &starget[0];
    char *ptr_array = &array[0];
    
    for (x = 0; x <= 7999; x++)
          *ptr_starget++ = *ptr_array++;
    Last edited by frktons; 07-30-2010 at 01:34 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes you can access the structure one byte at a time; first you'll get an ansi character, or the first half of a unicode character, then eventually the bytes of the word.
    Code:
    char *ptr_starget = &starget[0];
    chat *ptr_array = &array[0];
    
    for (x = 0; x <= 7999; x++)
          *ptr_starget++ = *ptr_array++;
    This is another copy though... If that is what you're doing with CHAR_INFO arrays, then perhaps you can fill that array later, and work on character arrays first.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by whiteflags View Post
    Yes you can access the structure one byte at a time; first you'll get an ansi character, or the first half of a unicode character, then eventually the bytes of the word.
    Code:
    char *ptr_starget = &starget[0];
    char *ptr_array = &array[0];
    
    for (x = 0; x <= 7999; x++)
          *ptr_starget++ = *ptr_array++;
    This is another copy though... If that is what you're doing with CHAR_INFO arrays, then perhaps you can fill that array later, and work on character arrays first.
    This could be an alternative. Instead of filling array[] first and after copying it
    into starget[], I could directly fill starget[] and not using array[] at all, bacause the
    source info that I have to copy are inside another array that I manipulate a little
    while copying into array[]. If accessing single bytes of a structure is possible, I can
    copy data form source array after manipulating it, directly to starget[].

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by whiteflags View Post
    Yes you can access the structure one byte at a time; first you'll get an ansi character, or the first half of a unicode character, then eventually the bytes of the word.
    Code:
    char *ptr_starget = &starget[0];
    chat *ptr_array = &array[0];
    
    for (x = 0; x <= 7999; x++)
          *ptr_starget++ = *ptr_array++;
    This is another copy though... If that is what you're doing with CHAR_INFO arrays, then perhaps you can fill that array later, and work on character arrays first.
    Something is wrong. The compiler complains about declaring a pointer to a structure
    with the char type.
    Being starget a structure, the pointer needs to be declared in a different way.
    Anybody knows how?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well if you're doing ugly things, be ready to use some ugly casting.

    const int INFOSIZE= 2000;
    CHAR_INFO starget[INFOSIZE];

    unsigned char *p = (unsigned char*)starget; /** OK **/

    No complaints. Now, as long as you know how to fill a CHAR_INFO array with a mess of bytes from god knows where, you're OK.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by whiteflags View Post
    Well if you're doing ugly things, be ready to use some ugly casting.

    const int INFOSIZE= 2000;
    CHAR_INFO starget[INFOSIZE];

    unsigned char *p = (unsigned char*)starget; /** OK **/

    No complaints. Now, as long as you know how to fill a CHAR_INFO array with a mess of bytes from god knows where, you're OK.
    This means that if I want to access a structure of CHAR_INFO type byte by byte I have
    to typecast the structure to unsigned char.
    Well, thanks whiteflags, enlightning suggestion.
    I'm going to give it a try. I'll let you know.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    This means that if I want to access a structure of CHAR_INFO type byte by byte I have
    to typecast the structure to unsigned char.
    I don't mean to be too picky, especially if it's already making sense to you, but it would be more accurate to say that you're casting the -pointer- to the structure to a -pointer- to unsigned char. You're not changing anything about what you're looking at, but changing the way you talk about it is what allows you to access it byte-by-byte. It's a subtle difference, but I hope that helps you see why it works.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by sean View Post
    I don't mean to be too picky, especially if it's already making sense to you, but it would be more accurate to say that you're casting the -pointer- to the structure to a -pointer- to unsigned char. You're not changing anything about what you're looking at, but changing the way you talk about it is what allows you to access it byte-by-byte. It's a subtle difference, but I hope that helps you see why it works.
    Thanks sean, this is a more precise description of what the instruction does.
    And well, the work is done, I attach the result. It is a program to display old DOS
    screen format 25 x 80 chars, with colors, in a Win32 console. It works only for Windows
    systems, because it calls Windows APIs.

    If you want to see it or try it, change txt extension with scn, compile inside
    a folder where the screen format is [scn], and you are done.

    You have an handy Extended ASCII table with 255 displayable characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM