![]() |
| | #1 |
| Registered User Join Date: Aug 2008 Location: Malaysia
Posts: 3
| Simple Memory Content Question 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;
}
Code: printf ("str1: %X\nstr2: %X\nstr3: %X\n",str1,str2,str3);
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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #6 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
In the case of %s, it does something like this: Code: case 's':
char *str = getnextargument();
while(*str)
{
putchar(*str);
str++;
}
break;
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;
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 | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
![]() |
| Tags |
| hexadecimal, memory content, printf, string |
| Thread Tools | |
| Display Modes | |
|
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 |