Thread: Pointers?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    18

    Question Pointers?

    Hello Everyone.

    Just wondering, why would you use pointers (ie. Find the memory address of a variable etc)? Also, is there any chance that memory addresses change after (say...) closing the program and then restarting your computer, then reopening the program?

    Thanks.

  2. #2
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    Pointers allow you to "point" functions and methods to a value that is stored in memory. Unlike passing a value only, the functions and methods can use the pointer to alter the memory at that location. There are many possible uses of pointers--both clever and dangerous--that I am sure other members could give you better examples than I could.

    Restarting the program will change the literal address of where data is stored, but usually not the offset. Meaning that the data in memory is usually a fixed distance from where the program was entered into memory (somone correct me if I am wrong on this).
    One of the disadvantages of being a 22 year old RPG programmer is having to repeatedly explain to your friends that you don't make videogames for a living.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Pointers have many uses. They can be used to pass objects by address in functions to avoid copying, they allow polymorphism, they allow dynamic memory allocation (on which are based all STL containers and such), they allow you to make callback functions, etc.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Thanks guys .

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i remember after first reading about pointers, i thought too it was used to "find the memory address" of a variable or function. i also was confused about why this is so popular or even how to use it in practice. however, the purpose of pointers is not really to "find the memory address" from a high-level / human point of view--that is, do you or i care about the actual hexadecimal address that some variable in a program is stored in? no. however, it is important that the computer knows where to find a variable or function.

    here are 2 important uses of pointers:
    1) variable-sized arrays
    - lets say we write a phone-book program. at compile-time, the compiler (nor do we or the computer) does not know how many people we are going to add into our phone book program. we could define the array structure that stores the phone book entries to be of size 20 to be safe. however, what if we only use 2 entries? there is wasted space. what if we want to enter 100 entries? there is not enough space.
    - a dynamic array is used instead of a array of static/constant size 20. we can resize the array at run-time as needed, and doing it this way (can) ensures minimal to zero wasted space, and that we can always add more entries, or make the array larger.
    - see "malloc" for C and "new" for C++

    2) pass-by-reference
    - lets say we have some math program that has a 100 by 100 matrix of integers. we also have a function to search a given matrix for the value at a specific row-column pair. if the function used "pass-by-value" for the matrix parameter, it would involve (the computer automatically) making a copy of the 100*100*(size of integer, usually 4 bytes)=40000 byte matrix so it can be used in the function, then forgotten about after the search function has returned.
    - thankfully, "by default", arrays and pointers are "passed-by-reference", where only a copy of the fixed-size pointer argument is passed, rather than the actual data structure.
    - all primitive data types are passed by value, unless "told" otherwise. these are things such as char, int, float, etc.

    once you start, if you havent already, really digging into the language and writing some programs, you will find the need for pointers and be thankful. so the best way to learn, is to do!

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Thanks.

    I don't really understand though, why use a pointer?

    Say for example, this script:

    Code:
    int nMyInt = 0;
    int* nMyPointer = &nMyInt;
    
    nMyPointer = 10;
    Why don't you just use int nMyInt = 10; rather than using a pointer?

    I'm actually finding pointers quite fun to learn haha. :P

    Thanks ,
    Sorry if I'm confusing hehe.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    in my last post i tried to explain something similar to your example. i.e., we dont really care about the address of some variable; use pointers when needed. in your code, there is no reason to use a pointer. it would depend on the context (what is later done with the variable).
    Last edited by nadroj; 12-30-2008 at 05:03 PM.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Quote Originally Posted by nadroj View Post
    in my last post i tried to explain something similar to your example. i.e., we dont really care about the address of some variable; use pointers when needed. in your code, there is no reason to use a pointer. it would depend on the context (what is later done with the variable).
    Aaah okay thanks .

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    lets look at an example function:
    Code:
    int isEven(int x)
    {
         if ( x % 2 == 0 )
             return 1;
         else
            return 0;
    }
    this function returns 1/true if the given number number is even, and 0/false otherwise. what if some program we are using expects all numbers it uses to be even? we can modify this function so that if the given number is odd, we can make it even, and "return" that new even number as well as whether the original number was even or odd (say our program has a counter of how many "original" numbers were actually even vs. odd; i know silly).
    we can change our function to use pass by reference:
    Code:
    int isEven(int &x)
    {
         if ( x % 2 == 0 )
             return 1;
         else
         {
            // x is odd, so lets make it even
            x++;
            // x is now even
            // the important thing is that since x was passed by reference, the original variable is also changed, i.e. the one in the "int main()" function or
            // whoever else called this function.
            // this is important because, in our example, the program/main() expects to only work with even numbers.
            return 0; // however we still need to return 0/false/"odd number" to the calling function who is keeping a count of how many even vs. odd
         }
    }
    again, somewhat of a silly program i know, but i hope it helps you understand. when this function is called, the actual variable is not passed from the calling function, but a pointer to the memory address of where the variable is. because of this, any changes made to the variable (via the pointer) in the isEven function, effect the original variable (since it is modifying the same memory address).
    Last edited by nadroj; 12-30-2008 at 05:06 PM.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nadroj View Post
    again, somewhat of a silly program i know, but i hope it helps you understand. when this function is called, the actual variable is not passed from the calling function, but a pointer to the memory address of where the variable is. because of this, any changes made to the variable (via the pointer) in the isEven function, effect the original variable (since it is modifying the same memory address).
    But the function isn't really performing an "isEven" kind of task now. It's altering its input. The name of the function needs to be changed, perhaps to "makeEven", since that's what it's doing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by brewbuck View Post
    But the function isn't really performing an "isEven" kind of task now. It's altering its input. The name of the function needs to be changed, perhaps to "makeEven", since that's what it's doing.
    well you are correct, for odd numbers. if the number is even it doesnt alter it correct? it doesnt make sense to call it "makeEven" in that case. anyways, i think this is up to debate, and a design question. since it does both check polarity of the number and make it even if it is odd, there are two logical names for the function, or maybe a combination of the two should be used.

    i think it is a subjective issue.
    Last edited by nadroj; 12-30-2008 at 05:17 PM.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    57
    They're useful in games, for example, pointing to the high score.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by michaelp View Post
    They're useful in games, for example, pointing to the high score.
    Please let this be a joke.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    57
    Quote Originally Posted by EVOEx View Post
    Please let this be a joke.
    :|
    I'll be quiet.
    But they are useful in games.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nadroj View Post
    well you are correct, for odd numbers. if the number is even it doesnt alter it correct? it doesnt make sense to call it "makeEven" in that case.
    Wow -- you've devised a function who's name depends on the inputs passed to it. I think you've invented an entirely new programming paradigm.

    i think it is a subjective issue.
    Not really.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM