Friday, July 15, 2011

C++ Pointers and References

#include <iostream>
using namespace std;

int main()
{
int someInt = 0; //<-- memory allocated and initialized to 0.
cout << &someInt << endl;//<-- print the address of someInt
//^^ It really prints the address of the base of the allocated section of memory.
int *dynamicInt = new int(0);//<-- memory allocated (on the heap) and initalized to 0.
cout << dynamicInt << endl;//<-- print the address of dynamicInt

//Assign both sections of memory the value of 1.
someInt = 1;
*dynamicInt = 1;//<-- here the pointer is "de-referenced"

cout << "someInt = " << someInt << endl;
cout << "dynamicInt = " << *dynamicInt << endl;


//Create a reference to both.
int &someRef = someInt;//<-- disregard the syntax with regard to the address
int &otherRef = *dynamicInt;//


//Modify both with the reference.
someRef = 2;
otherRef = 2;

cout <<  "someRef = " << someRef << endl;
cout << "otherRef = " << otherRef << endl;




delete dynamicInt;//<-- free dynamically allocated memory.

cout << "Press ENTER to continue." << endl;
cin.get();
return 0;
}

19 comments:

  1. It's all Greek to me, I'm afraid.

    ReplyDelete
  2. So, is that a memory usage limiting program for some application or something?
    Looks pretty interesting, but I am a big beginner. I'll be reading your older posts after following so i can get more of a hold of your next posts.

    ReplyDelete
  3. @1ockedand1oaded,

    No, it's an example I wrote for someone who asked a programming question on Daniweb. I don't like to lose track of these examples I write, so I posted it here--but I didn't have time to add any explanation to it, lol.

    ReplyDelete
  4. that sure looks complicated, i wish i got more into programming

    ReplyDelete
  5. this looks interesting ,i have been thinking lately in learning all this

    ReplyDelete
  6. im trying to understand this stuff, and your blog helps immensely. +followed

    ReplyDelete
  7. I never bothered to learn C++, but I'd really like to

    ReplyDelete
  8. You're inspiring me to retry with C++. I'll follow you for the massive tips you have. Update ya on my progress =D +1.

    ReplyDelete
  9. interesting stuff man, i've never seen the
    "int &someRef" to declare pointers, thanks for sharing =)

    ReplyDelete
  10. Last year I've started playing with C++, but no way I could make it. You sir is amazing.

    ReplyDelete
  11. @Batow,

    That's why you don't start with C++ lmao. Try Lua or Python or something. Java or C# even.

    ReplyDelete
  12. Interesting post mate, always interested in learning new things with computers, keep up the good work +follow

    ReplyDelete
  13. yeah looks interesting but you should add those explanations

    ReplyDelete
  14. @happyhacker i think is pretty good to start with c++, because his complexity helps to confront yourself with almost all of the problems that you could meet programming, isn't it?

    ReplyDelete
  15. Read it through and you helped me out with some references I couldnt put my finger on before.. Will absolutely follow you man,

    ReplyDelete
  16. good pointers! lol sorry couldn't help but make a pun

    ReplyDelete