Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Accessing memory locations in C/C++

  • 26-02-2002 11:20am
    #1
    Posts: 0


    I was wondering something about C/C++.

    I used java for a while but now i'm going into C/C++...I love the freedom associated with the language...one can access all sorts of things that was protected or disabled in java...real programming freedom.

    However, I was wondering if I could access memory locations and print out the contents for a test on memory accessing or will that work?

    I was trying

    #include <iostream.h>

    int main()
    {
    int a; //create the variable
    int *aPtr; //create pointer to memory location

    aPtr = &a;
    cout << aPtr << endl;

    return 0;
    }

    that prints out the memory locatin in hex

    can I explicitly give the pointer a memory location from the program or will the OS give me an error message?

    I think it's all interesting, things one couldn't do in Java, more control over what you're doing.

    Thanks for any help


Comments

  • Registered Users, Registered Users 2 Posts: 944 ✭✭✭nahdoic


    MoonHawk are you in case2 in DCU? Cos we are doing this stuff in c++ now ... and we only did java last year. Sounds like you are ;)


  • Registered Users, Registered Users 2 Posts: 4,676 ✭✭✭Gavin


    you should use the redbrick boards aodhan :)

    get them to renew your account.

    Gav


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Originally posted by MoonHawk
    I was wondering something about C/C++.

    I used java for a while but now i'm going into C/C++...I love the freedom associated with the language...one can access all sorts of things that was protected or disabled in java...real programming freedom.

    However, I was wondering if I could access memory locations and print out the contents for a test on memory accessing or will that work?

    I was trying

    #include <iostream.h>

    int main()
    {
    int a; //create the variable
    int *aPtr; //create pointer to memory location

    aPtr = &a;
    cout << aPtr << endl;

    return 0;
    }

    that prints out the memory locatin in hex

    can I explicitly give the pointer a memory location from the program or will the OS give me an error message?

    I think it's all interesting, things one couldn't do in Java, more control over what you're doing.

    Thanks for any help

    You might try something like

    cout<<aptr<<endl; //giving you say 0x100

    aptr=(long*)&aptr+(or)-the number you would like it to equal say 102
    aptr=(long*)&aptr+2;
    cout<<aptr<<endl;
    or something similar possibly.


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    You could try somthing along the lines of this:

    #include <iostream>

    using namespace std;

    void main(){

    int val = 100;
    int *ptr = &val;

    // Note that we are printing out the values as integers, because the
    // pointer is declared to be pointing at an integer. You could write your
    // own code to print it in some other form
    cout << "Address " << ptr << endl;
    cout << "Has value " << *ptr << endl << endl;

    // This adds 4 to the pointer ( assuming 64bit ints )
    // ( pointer arithmetic multiplys by the size of the type of the pointer
    // , in this case we defined ptr to be a pointer to an int, so adding one
    // to it will in fact add 4 becase int's as 4 bytes long )
    ptr++;
    cout << "Address " << ptr << endl;
    cout << "Has value " << *ptr << endl << endl;

    // This adds 8 to the pointer ( assuming 64bit ints )
    ptr+=2;
    cout << "Address " << ptr << endl;
    cout << "Has value " << *ptr << endl << endl;

    // Sets the pointer to point at a specific address
    // A random address may not belong to the programs memory space
    // and so may generate an access violation
    ptr = (int*)0x130000;
    cout << "Address " << ptr << endl;
    cout << "Has value " << *ptr << endl << endl;

    }


  • Posts: 0 [Deleted User]


    MoonHawk are you in case2 in DCU? Cos we are doing this stuff in c++ now ... and we only did java last year.
    you should use the redbrick boards aodhan

    Aha! They've found me out!! NOOOOOO!
    Seriously, that's cool to know fellow DCU people are out there, using these boards! Nice one.
    Actually I still use Redbrick! Haven't kicked me off, I renewed the account! :D

    Zab and Typedef, thanks for the help, useful as always


  • Advertisement
Advertisement