Thread: Simple Memory Content Question

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Malaysia
    Posts
    5

    Simple Memory Content Question

    Hi folks,

    I use memcpy to copy a content from a memory location to another

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str1[]="Sample string";
      char str2[40];
      char str3[40];
      memcpy (str2,str1,strlen(str1)+1);
      memcpy (str3,"copy successful",16);
      printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
      return 0;
    }
    Printf will show the exact same "Sample String" as expected, but if i change printf to

    Code:
    printf ("str1: %X\nstr2: %X\nstr3: %X\n",str1,str2,str3);
    I get something like

    str1: BFBF4962
    str2: BFBF493A

    I thought the content should be similar? Can anyone help explain it to me please?

    Thanks in advanced.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why did you think the result would be similar? What do you think %X means to printf?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    87
    X prints out the value of an unsigned int in hex notation. Do you know what the values printf is showing you are? Hint: What is the difference between the two (BFBF4962 - BFBF493A = what )?

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Malaysia
    Posts
    5
    Hi citizen and jason_m,

    Thanks for replying. I know what %X does but I tought after copying the content from one location to another, the contents in both location should be exactly the same.

    I tought the resulting machine instruction will load the exact bit patterns from the source address and put it into the destination address.

    Shouldn't str1 and str2 have the same contents regardless what kind of representation I use for printing the values wether in string, hex or int form...? why does the two values differ in hex representations but not in string form?

    Thanks for taking the time to reply to my silly questions

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because those arrays decay to pointers and pointers contain a memory address. You're printing the memory address.
    If the memory address was not the same, it would be the same array or variable which would make no sense. Because you're creating two different arrays, and then copying the contents into each other.

    So to sum it up: they have the same contents, but they are different arrays, so the memory address is different.
    (You can't expect to take two mugs, one full of beer, then pour the beer into the other mug and place them at the exact same location [x, y, z]. It's impossible, and it's likewise impossible in the computer world [x, y, z being the memory address].)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by azzuwan View Post
    Shouldn't str1 and str2 have the same contents regardless what kind of representation I use for printing the values wether in string, hex or int form...? why does the two values differ in hex representations but not in string form?
    The real issue here is that when you pass a string to printf() [or any other function for that matter], you actually just pass the location of the string. Inside printf, the different %letter combinations will then take the respective argument and do the relevant display operation.

    In the case of %s, it does something like this:
    Code:
       case 's':
        char *str = getnextargument();
        while(*str)  
        {
            putchar(*str);
            str++;
        }
        break;
    in another place you'd find the %x code:
    Code:
        case 'X':
           usecapitals = 1;
        case 'x':
           unsigned int val = getnextargument();
           char buffer[9];
           // Convert x to a string in buffer, using base 16.
           num2str(buffer, x, 16, usecapitals);
           puts(buffer);
           break;
    The above code is heavily simplified, and both of these cases are probably 3-5x longer in a real printf implementation, mostly to handle width and right/left positioning.

    But as you can perhaps figure out, getnextargument() is a generic function to pick up the next of the arguments, and printf itself DOESN'T know what the next argument represents in itself, it just sees the numeric value as a value that it prints in the form that you've asked for.

    If you print a string as %X, it will show you the address of the string, not the string itself, as hex.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Malaysia
    Posts
    5
    Great explanations!

    I'm an example of one of those people who started learning programming using "managed environment" language / platform. Totally messed things up when tasked to do things in a language that allows power and control over everything such as C.

    Thank you all , especially Elysia and Matsp.
    That cleared things up for me.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just a thought, but if you want more features that are available in managed environment while still remaining close to the hardware, try C++, since it's an evolution of C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about memory
    By simo_mon in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 08:02 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Yet another memory question
    By Dohojar in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 01:47 PM

Tags for this Thread