C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-17-2008, 08:41 PM   #1
Registered User
 
Join Date: Aug 2008
Location: Malaysia
Posts: 3
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.
azzuwan is offline   Reply With Quote
Old 09-17-2008, 08:47 PM   #2
Registered User
 
Join Date: Apr 2006
Location: United States
Posts: 3,202
Why did you think the result would be similar? What do you think %X means to printf?
__________________
Os iusti meditabitur sapientiam
Et lingua eius loquetur indicium

"There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii)

http://www.myspace.com/whiteflags99
whiteflags is offline   Reply With Quote
Old 09-17-2008, 09:24 PM   #3
Registered User
 
Join Date: May 2008
Posts: 54
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 )?
jason_m is offline   Reply With Quote
Old 09-18-2008, 12:56 AM   #4
Registered User
 
Join Date: Aug 2008
Location: Malaysia
Posts: 3
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
azzuwan is offline   Reply With Quote
Old 09-18-2008, 01:23 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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].)
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-18-2008, 03:30 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-18-2008, 03:56 AM   #7
Registered User
 
Join Date: Aug 2008
Location: Malaysia
Posts: 3
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.
azzuwan is offline   Reply With Quote
Old 09-18-2008, 04:23 AM   #8
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
hexadecimal, memory content, printf, string

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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