Sign in to follow this  
randomperson

c++ - pointers

Recommended Posts

ok pointers - confusing i know they can be used to pass the adresses and values to functions and things like that, just that with everything generally i like to have a deep understanding(it really anoys me when i dont) and i just dont understand the full uses of pointers could anyone point(hehe) me to any articles, infomation and/or give me and pratical examples really helpfull thankyou!!!!

Share this post


Link to post
Share on other sites
In C++, raw pointers are used:
  • To interact with C API functions, and to represent string literals (an usage carried over from C):

    std::string s = "Hello"; // "Hello" is a const char * const
    call_c_function(s.c_str()); // The C function needs a const char *


  • To create and destroy objects with new (returns a pointer) and delete (destroys object through pointer).

    Obj* obj = new Obj;
    delete obj;


  • To gain access to the char[] (array of bytes) representation of a POD type. Rarely useful.

    somePOD p;
    char bytes[sizeof(p)] = reinterpret_cast<char(&)[sizeof(p)]>(p);


  • To implement smart pointers.


That's about all they are used for in modern C++. In the same vein, smart pointers (std::auto_ptr, boost::shared_ptr) are often used to represent ownership of dynamic objects, and references are used to pass arguments by reference.

Share this post


Link to post
Share on other sites
A pointer is essentially a memory address. It has a consistent size based upon your platform (typically 4 or 8 bytes). When you pass variables by value to a function, you are essentially making a copy of that variable to use in the function. Therefore any changes to the variable inside the function do not persist once the function goes out of scope. If you pass a memory address to your function, the function can change the actual variable passed to the function. No copy is made -- because you have the address, you can manually manipulate the data at that address. In this instance, however, you're probably better off using references (if you're using C++) because they are less messy.

Since pointers are consistently sized, they can be used to pass large structs or classes to functions very quickly since no copy is made. If your class contains hundreds of variables (equating to hundreds of bytes), a function will have to copy all of these variables if the class is passed by value. Pass a pointer (or reference) to the function and you only pass 4 bytes. Nifty, huh? But you do have to be careful here -- if you don't want any of the changes to persist once your function goes out of scope then you need to make sure you don't change the object at all. You can make sure the programmer doesn't change the object by passing by const reference.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this