Thread: In what cases you HAVE TO use pointer?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    In what cases you HAVE TO use pointer?

    I was asked: "In what cases you HAVE TO use pointer?"
    The case I can think out is that some functions require you to do so in their signatures, for example, the qsort(void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) requires you to pass in void* base, etc..

    What else?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    I was asked: "In what cases you HAVE TO use pointer?"
    The case I can think out is that some functions require you to do so in their signatures, for example, the qsort(void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) requires you to pass in void* base, etc..

    What else?
    You need a pointer whenever you must dynamically allocate an object with "new". You also might use one if you need to dynamically point at different objects using the same pointer -- this can't be done using references.

    With STL containers, it is possible to solve many real-world problems without ever seeing a pointer. I personally try to avoid them as much as possible.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I actually came across an example of this problem recently where I was forced to use a pointer.

    I had a program that created random 3-D mountains. One of the control-variables was the "apex" or "peak" of the mountain.

    there were 3 possible peaks:

    1. right in the center (automatically generated)

    2. off-center( randomly, and automatically generated)

    or

    3. user-defined

    In main the peak had to declared before being created like this:

    Code:
    Coord *init_peak;
    Depending on the command-line arguments, the peak was created in one of these three ways:
    Code:
    // Centered -automatic:
    init_peak = new Coord( AUTO_X, AUTO_Y, AUTO_Z ); // Predefined x,y,z values
    
    
    // Off-centered -automatic & random:
    double init_x = get_rand_double( 0, 100 );
    double init_y = get_rand_double( 0, 100 );
    double init_h = get_rand_double( 50, 150 );
    
    init_peak = new Coord( init_x, init_y, init_h );
    
    
    // User-defined:
    init_peak = new Coord( get_init_peak() ); // Func returns peak value entered by user
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I was asked: "In what cases you HAVE TO use pointer?"
    Do iterators and other smart pointers (e.g., auto_ptr, shared_ptr, weak_ptr) count as pointers?

    The case I can think out is that some functions require you to do so in their signatures, for example, the qsort(void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) requires you to pass in void* base, etc..
    That sounds like an incomplete answer though. One could then ask: why must these functions require the use of pointers?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    I actually came across an example of this problem recently where I was forced to use a pointer.
    In this type of situation it is usually possible to break the function out into an initializer function and a subfunction which takes a reference to the dynamic object. So strictly, you COULD avoid the pointer in this case. But it's one of the cases where I wouldn't feel bad about using one. Splitting your code out into two different functions SOLELY to avoid using a pointer is probably overdesigning it.

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by brewbuck View Post
    In this type of situation it is usually possible to break the function out into an initializer function and a subfunction which takes a reference to the dynamic object. So strictly, you COULD avoid the pointer in this case. But it's one of the cases where I wouldn't feel bad about using one. Splitting your code out into two different functions SOLELY to avoid using a pointer is probably overdesigning it.
    I guess that's true. Sorry for the bad example. I guess I was thinking more along the lines, "when SHOULD you use a pointer?"...

    But I would think if you're going to invest the time learning C++, you shouldn't avoid learning pointers, though I'm with you brewbuck, I don't overuse pointers when they're obviously not necessary. The use of reference as arguments to functions seems to cut out a lot of need for pointers.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Quote Originally Posted by meili100 View Post
    I was asked: "In what cases you HAVE TO use pointer?"
    Pass by value vs. Pass by reference

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mcha View Post
    Pass by value vs. Pass by reference
    If you want to pass by reference in C++, you can use... a reference. No need for a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM