Thread: help with displaying memory addresses

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    55

    help with displaying memory addresses

    here's what i'm trying to do. I want to input a number into a variable, (int,float,double).. i want to display it's address, and display the bytes in big endian form.. (intel machines are little endian)

    so, i can print the address, but &variable.. the problem is, i need the address to have spaces...

    so for example, i can print "ox454567, but i want it to be 0x 45 45 67. to do this i need to go through memory byte by byte.

    So, how can i go through memory byte by byte,, the code below is trying to do what i described..
    but it's not working.. it keeps printing byte values inside the address



    Code:
    void IntType(void)
    {
    	int num0;
    	char conti0;
    	char *addr0;
    	
    
    	cout << endl;
    	cout << endl;
    	cout << "Enter an integer:";
    	cin >> num0;
    
    	
    	addr0 = (char*)(&num0);
    	
    
    	
    
    	cout << endl;
    	cout << endl;
    	cout << "Address:0x";
    	cout << hex << addr0;
    
    	cout << "          Big endian representation:";
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    
    	while(1)
    	{
    		cout << "press 'c' to continue:";
    		cin >> conti0;
    		cout << endl;
    		if(conti0 == 'c' || conti0 == 'C') break;
    	}
    
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Seems a bit odd, but whatever. I made a little bit of code, it doesn't use std::cout but rather printf with the %x format specifier, cheating. Maybe someone would help with the std::cout implementation.

    Code:
    #include <stdio.h>
    int main()
    {
    	int n, *p = &n, j = 0x000000ff;
    	for(int i = 0; i < 32; i += 8)
    		printf("%02.2\n", (unsigned int) p & (j << i));
    	return 0;
    }
    This one assumes a pointer is 32 bits, like an unsigned int.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    #include <iostream>
    
    int main()
    {
       int p;
       std::cout << std::hex << reinterpret_cast<unsigned long>(&p) << std::endl;
    }
    Of course, int* and unsigned long are not guaranteed to have the same size. Unsigned long is just likely to be a safe bet.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    i'm still confised

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    i'm still confused

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    before you got confused did you at least try it.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    well, i don't really understand it, I mean i can just copy and paste it. I'm not sure which variables in that thing correspond to my variables..

    Code:
    int temp;
    int num; // the address of this variable is what i want to print
    char *addr;
    char **byt;
    
    num = 6;
    addr = &num;
    byt = &addr;
    
    temp  = byt;
    cout << temp << " ";
    
    byt++;
    
    temp = byt;
    
    cout << temp << " ";


    i'm trying to do something like that.. not sure if that works though

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    okay.. i got it!!! i did something similar to what Tonto did.. it took me a while to realize he was bit shifting and masking out bits and stuff..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can know in memory someone addresses in free list
    By 0000000009 in forum C Programming
    Replies: 8
    Last Post: 10-17-2005, 12:35 AM
  2. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  3. Editting Memory Addresses?
    By Dae in forum C++ Programming
    Replies: 7
    Last Post: 07-03-2005, 07:07 PM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Memory Addresses
    By Breetai in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 08:10 AM