Support Forums
help comparing memory addresses... - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: help comparing memory addresses... (/showthread.php?tid=4638)



help comparing memory addresses... - Cursed - 02-02-2010

My homework has a bonus question.

Bonus question:
  • Declare two variables, one is of regular integer type and the other of unsigned integer type.
  • Find out their sizes.
  • Find out if those two variables are next to one another in the memory.
  • Find out the largest integer (signed or unsigned) that these two variables can hold.
I tried to use an if/else statement (at the bottom of the code) to determine this, but I get the error "comparison between distinct pointer types `int*' and `unsigned int*' lacks a cast" in these two lines:

Code:
if (&variableX+1 == &variableY){

Code:
if (&variableY+1 == &variableX){
Can anyone give me a hint on how I could resolve this problem?

Code:
//Assignment 1
#include <iostream>
using namespace std;

int main()
{
    //Declare a variable of double type
    double variableA;
    //Display the size of that variable
    cout << "The size of variableA: " << sizeof variableA << " bytes" << endl;
    //Display the location of that variable
    cout << "The address of the memory location of variableA: " << &variableA << endl;
    //Display what is in it before you put any data into it
    cout << "The contents of variableA: " << variableA << endl << endl;
    //Prompt the user to enter a number from the keyboard
    cout << "Enter a number: "; cin >> variableA;
    //then display the tripled value of the number
    cout << variableA << " multiplied by 3 is " << variableA*3 << endl << endl;

    system("PAUSE");
    
    //Pick one other data type that C++ supports to do the same
    int variableB;
    //Display the size of that variable
    cout << "\nThe size of variableB: " << sizeof variableB << " bytes" << endl;
    //Display the location of that variable
    cout << "The address of the memory location of variableB: " << &variableB << endl;
    //Display what is in it before you put any data into it
    cout << "The contents of variableB: " << variableB << endl << endl;
    //Prompt the user to enter a number from the keyboard
    cout << "Enter a number: "; cin >> variableB;
    //then display the tripled value of the number
    cout << variableB << " multiplied by 3 is " << variableB*3 << endl << endl;

    system("PAUSE");
    
//Bonus question:

    //Declare two variables, one is of regular integer type and the other of
    //unsigned integer type
    int variableX;
    unsigned variableY;
    //Find out their sizes
    cout << "\nNote: variableX is a signed int while variableY is an unsigned int";
    cout << "\n\nThe size of variableX is: " << sizeof variableX << " bytes";
    cout << "\nThe size of variableY is: " << sizeof variableY << " bytes" << endl;
    //Find out if those two variables are next to one another in the memory
    cout << "\nThe address of the memory location of variableX: " << &variableX << endl;
    cout << "The address of the memory location of variableY: " << &variableY << endl << endl;

    if (&variableX+1 == &variableY){
       cout << "They are next to one another in memory!" << endl;
       }
       else {
            if (&variableY+1 == &variableX){
               cout << "They are next to one another in memory!" << endl;
               }
               else {
                    cout << "They are not adjacent to one another in memory!" << endl;
                    }
               }
            
    system("PAUSE");
    return 0;
    
}





Nevermind.. I got it working with reinterpret_cast


RE: help comparing memory addresses... - MrD. - 02-02-2010

What compiler do you use? Because MSVC gives a huge hint about this by saying "Types pointed to are unrelated; conversion requires reinterpret_cast".


RE: help comparing memory addresses... - g4143 - 02-03-2010

This should work, even with the C style casting

Code:
#include <iostream>
using namespace std;

int main()
{
    unsigned variableY;
    int variableX;

    cout << "\nNote: variableX is a signed int while variableY is an unsigned int";
    cout << "\n\nThe size of variableX is: " << sizeof variableX << " bytes";
    cout << "\nThe size of variableY is: " << sizeof variableY << " bytes" << endl;

    cout << "\nThe address of the memory location of variableX: " << &variableX << endl;
    cout << "The address of the memory location of variableY: " << &variableY << endl << endl;

    if ((void*)&variableX < (void*)&variableY)
    {
        if (sizeof(int) == ((long)&variableY - (long)&variableX))
        {
            cout << "They are next to one another in memory!" << endl;
        }
        else
        {
            cout << "They are not adjacent to one another in memory!" << endl;
        }
    }
    else if ((void*)&variableX > (void*)&variableY)
    {
        if (sizeof(int) == ((long)&variableX - (long)&variableY))
        {
            cout << "They are next to one another in memory!" << endl;
        }
        else
        {
            cout << "They are not adjacent to one another in memory!" << endl;
        }
    }
    return 0;
}