Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help comparing memory addresses...
#3
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;
}
Slackware 13/ArchLinux - C/Assem/Python
Reply


Messages In This Thread
help comparing memory addresses... - by Cursed - 02-02-2010, 06:29 AM
RE: help comparing memory addresses... - by MrD. - 02-02-2010, 02:46 PM
RE: help comparing memory addresses... - by g4143 - 02-03-2010, 10:44 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)