Converting a memory Address into an int

May 12, 2013 at 10:52pm
Hello, I have been trying to figure out a way to convert a memory address, 0x200G69, or something similar, into an integer. I was hoping someone could help. I have had a few ideas like attempting to change the memory address into a character array then changing the characters into their ascii codes and adding them together, but that is not working. Any help would be great, thanks.

1
2
3
4
5
  char temp[9] = &CDs[i]; //CDs is a CD**
  int ascii = 0;
  for(int i = 0; i < 9; i++)
       ascii = ascii + temp[i];
  cout << ascii << endl;


Let me add, I was wanting it in a comparable form of an int. I am trying to use something similar to this as a means to calculate weights between two nodes, vertices, of a graph to find the shortest distances.
Last edited on May 13, 2013 at 12:50am
May 12, 2013 at 11:10pm
Consider the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>
 
int main()
{
        int x;
        int *p = &x;
 
        x = reinterpret_cast<int>( p );
 
        std::cout << ( void * )&x << '\t' 
                << std::hex << std::uppercase << x << std::endl;
}


However take into account that in some systems address can occupy 8 bytes while the type int has size only of 4 bytes.
May 12, 2013 at 11:32pm
What do you mean by "converting a memory address into an integer"?

All memory addresses are already integer values (usually either 32-bit a 64-bit, though there are processor architectures that use other values).

Do you just want to display the address as a decimal rather than the hexademical that is used by convention? Or something else?

Andy
May 13, 2013 at 12:08am
I am trying to create a graph like storage system which contains the previous node's, vertex, memory address and the memory address of three other nodes. I am trying to use the memory address of the nodes to traverse this graph like structure to find the shortest route to the address of the piece of memory being sought out by the user.
May 13, 2013 at 12:09am
I have not understtod what is the link between your task and your attempt to deal with addresses as integers.
May 13, 2013 at 12:14am
Just treat it as a pointer. You can subtract two pointers of the same type and assign the result to a integral object of type ptrdiff_t.
http://www.cplusplus.com/reference/cstddef/ptrdiff_t/?kw=ptrdiff_t
May 13, 2013 at 1:09am
Here, I think I have been making it seem more confusing. I am trying to use the memory addresses of what my pointers are pointing to, to maneuver through a system I have set up similar to a graph. What I am trying to do, is figure out a way to compute the current nodes weight via with its memory address by turning it into an int, then using this int to compare it to up's, down's, right's, and left's memory addresses to find the fastest path by this comparison to find the node the user wants.

template < class U >
    class GraphNode
    {
        private:
            GRAPH graphfactor;
        
            GraphNode<U>* Up;
            GraphNode<U>* Down;
            GraphNode<U>* Right;
            GraphNode<U>* Left;
        
            //int weight;
        
            U* vertice;
        
        public:
            GraphNode(U* vert);
            ~GraphNode();
            
            bool End();
            GraphNode<U>* getUp();
            GraphNode<U>* getDown();
            GraphNode<U>* getRight();
            GraphNode<U>* getLeft();
            
            U* getVertice();
            
            int getWeight(); 
/*           ^ ^ ^ ^
I'm trying to figure out how to convert the memory address 
of this current node into an int to return to figure out if it 
would be faster to use this node or not to find the location 
of the item.  Comment to int getWeight()
*/
            void setGraphFactor(GRAPH factor);
            GRAPH getGraphFactor();
            
            void setVertice(U* vert);
            void setUp(GraphNode<U>* edge);
            void setDown(GraphNode<U>* edge);
            void setRight(GraphNode<U>* edge);
            void setLeft(GraphNode<U>* edge);
            
            void insertUp();
            void insertDown();
            void insertRight();
            void insertLeft();
            
            void removeUp();
            void removeDown();
            void removeRight();
            void removeLeft();
    };
Topic archived. No new replies allowed.