Thread: Working with pointers...

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Working with pointers...

    Outside of passing objects by reference, why would you want to access memory directly by using pointers?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Outside of passing objects by reference, why would you want to access memory directly by using pointers?
    There are two aspects to using pointers:

    1) Efficiency.
    Whenever the data you are passing to a function is not a simple data type like int, double, etc. using a pointer is more efficient. Functions have to make a copy of whatever arguments are passed to them. When you pass a pointer, you are passing an address. An address is essentially an integer, and making a copy of an integer doesn't take up much room in memory. On the other hand, if you pass an object to a function, the function has to copy the whole object, and an object can have an enormous amount of data inside it, which takes up more room in memory.

    2) Being able to change the original object you pass to a function.
    If you pass an object to a function, the function makes a copy of the object for the function. If the function then makes changes to the object, the changes are made to the copy, and when the function ends, the copy is destroyed leaving the original object unchanged. Instead, if you pass a pointer to the function, the function copies the pointer. But, a copy of a pointer points to the same place in memory as the original pointer, so you can use the copy of the pointer to access the original object and change it. Like before, when the function ends, the copy of the pointer is destroyed, however the original object remains permanently changed. The original object remains permanently changed because you made changes directly to the original object--not a copy.
    Last edited by 7stud; 05-09-2005 at 08:21 AM.

  3. #3
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Oh, good call. Forgot my Java background.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 06:10 PM
  4. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM