Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2008, 10:33 AM   #1
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Char memory address?

Declared variables:
c++ Syntax (Toggle Plain Text)
  1. char d='x';
  2. char e='y';
  3. char f='z';

printed to command line:
c++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. cout<<"value of d = "<< d <<" Address of d = "<< &d <<" size of d= "<< sizeof(d) << endl;
  4. cout<<"value of e = "<< e <<" Address of e = "<< &e <<" size of e= "<< sizeof(e) << endl;
  5. cout<<"value of f = "<< f <<" Address of f = "<< &f <<" size of f= "<< sizeof(f) << endl;
  6. return 0;
  7. }

and I get the following (garbage?)
commandline Syntax (Toggle Plain Text)
  1. value of d = x Address of d = x��*��*��*��*��*��*��*��* size of d = 1
  2. value of e = y Address of e = y��*��*��*��*��*��*��*��*��*��*��*x��*��*��*��*��*��*��*��* size of e = 1
  3. value of f = z Address of f = z��*��*��*��*��*��*��*��*��*��*��*y��*��*��*��*��*��*��*��*��*��*��*x��*��*��*��*��*��*��*��* size of f = 1
  4. Press any key to continue . . .

this doesn't look like valid address notation to me, has anybody seen this before? This is on Vista 64 and it doesn't show as the squares in the command line, but looks like the screen shot below.

Can anybody tell me why the address for e and f include the previous addresses and all the junk?
Attached Images
File Type: gif SS.gif (15.8 KB, 12 views)
Jabo is offline   Reply With Quote
Old Mar 24th, 2008, 10:36 AM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: Char memory address?

If I had to guess, I'd say it's because it still allocates a byte for the chars, that's where the junk comes from, but wouldn't it still have a normal memory address? This is confusing.
Jabo is offline   Reply With Quote
Old Mar 24th, 2008, 10:50 AM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: Char memory address?

If I had to guess, I'd say the way to handle the addresses is ambiguous. You're basically giving cout a char* to print out, and you want the int value, but it's probably looking at it as a null-terminated string. Try casting the addresses to int and see what you get
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Mar 24th, 2008, 4:06 PM   #4
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Char memory address?

Quote:
Originally Posted by Jimbo
If I had to guess, I'd say the way to handle the addresses is ambiguous. You're basically giving cout a char* to print out, and you want the int value, but it's probably looking at it as a null-terminated string. Try casting the addresses to int and see what you get
Jimbo is correct, except for the casting to int part (sizeof(int) and sizeof(char *) are not necessarily equal). When you pass it the address of a char, it receives a char *, and has no way of knowing you want to print the address, rather than a string. Remember that a C-style string, used in C++ as well, is just a series of zero or more char elements, followed by a char with the value zero to act as a terminator. Try something like this instead:
C++ Syntax (Toggle Plain Text)
  1. int main(void)
  2. {
  3. char d = 'x';
  4. std::cout << "value of d = " << d << " Address of d = "
  5. << (void *)&d << " size of d = " << sizeof(d)
  6. << std::endl;
  7. // repeat for the others
  8. }
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Mar 24th, 2008, 8:59 PM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: Char memory address?

Thanks guys, I will digest this a bit and see if it can make sense to my old head.
Jabo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hexadecimal Memory Address Question 357mag C++ 1 Jul 8th, 2007 10:19 PM
problem processing file into a char array csrocker101 C++ 1 May 9th, 2007 12:50 AM
address memory in an array b1g4L C++ 9 Feb 5th, 2006 3:33 AM
Copying memory address values rsnd C++ 4 Jun 19th, 2005 8:41 AM
Pointers in C (Part I) Stack Overflow C 4 Apr 28th, 2005 8:03 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC