Thread: Pointers: why not?

  1. #16
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    One very simple example, Aerie, of a time you cannot use a pointer is if you attempt to pass a constant to a function. For example, given function void foo(float *num), you could not call foo(&5.5f).
    If you think about how a function call works, it seems apparent that always using pointers would use more overhead, however slight. This is because whether passing by value or a pointer a new variable is invariably created. When passing by value, this new variable is set to the passed value and whenever referenced in the function the local copy is used. If a pointer was passed, a dereference will probably be required every time the variable is accessed. Now if you were talking about always passing by reference that would be another story, but still it would be one with a bad ending.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by PING
    foo(int *ptr);
    int a;
    foo(&a);
    ??i dont think that passing addresses takes up space does it??
    plz dont get me wrong.okk..i am not trying to say that you are wrong or something.just trying to put the point that it doesnt actually take up more space.

    Sure it does. Here's why:
    Code:
    int a; /* The variable a is allocated. */
    
    foo( &a ); /* The address of the variable is passed to a function, foo. */
    
    ...
    
    void foo( int *b )
    {
        /* At this point in the function, there is a variable called "b", which
            is a pointer to an integer. This is an actual variable. It really exists.
            If it didn't exist, and thus take up space, how on earth could you
            reference it and use it?
    
            Thus, we have two variables:
                    a -- declared in a previous function or globally or whatever.
                    b -- declared as part of the function prototype.
            */
    }
    If it didn't actually exist, how on earth would you use the variable? Obviously it has to exist some place. Furthermore, to illustrate that it does in fact exist as a seperate variable, you can do schtuff like this:
    Code:
    void foo( int *b )
    {
        int c;
    
        b = &c; /* This is in fact a different variable. We've just made it point somewhere else. */
        *b = 10;
    
        printf("*b is %d\n", *b );
        printf("c is %d\n", c );
    }
    As such, the origional passed variable is actually ignored here, because we use the pointer to point to a different local variable, losing all reference to the passed variable.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by PING
    just trying to put the point that it doesnt actually take up more space.
    Quote Originally Posted by quzah
    Sure it does. Here's why:
    I believe PING was saying that a pointer doesn't take up more space than when passing by value. Now in response to PING, there is a very minute chance that a pointer would take up more space than something passed by value (since a pointer will usually take 4 bytes and anything bigger than that when passed by value will clearly take more).

    As I was saying in my previous post, the only real possibility of a negative effect between passing a float, for example, by value and passing a pointer to a float by value is that every reference to the pointer will require the dereference before the access. Now, of course, when passing any variable of significant size (a struct or class, etc) this minor dereference overhead is nothing because of the benefit of not having to construct a new, huge object to be temporarily accessed. Make any sense?

  4. #19
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by LuckY
    I believe PING was saying that a pointer doesn't take up more space than when passing by value. Now in response to PING, there is a very minute chance that a pointer would take up more space than something passed by value (since a pointer will usually take 4 bytes and anything bigger than that when passed by value will clearly take more).

    As I was saying in my previous post, the only real possibility of a negative effect between passing a float, for example, by value and passing a pointer to a float by value is that every reference to the pointer will require the dereference before the access. Now, of course, when passing any variable of significant size (a struct or class, etc) this minor dereference overhead is nothing because of the benefit of not having to construct a new, huge object to be temporarily accessed. Make any sense?
    thats what i was saying.what quzah says is okk,but i meant something different.what i suppose the initial argument someone put up was that we might have to declare a pointer in main() or whatever funcion and then pass the pointer to the function,but we can just pass the address using &.what lucky says is right as well.when passing values,it will be a headache,but when we want to pass values,we dont want to do anything with the values,but use then at one place,while with variables,the values will be changed more than once,so i guess a pointer will be much better to use..i agree with lucky.

  5. #20
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by PING
    thats what i was saying.what quzah says is okk,but i meant something different.what i suppose the initial argument someone put up was that we might have to declare a pointer in main() or whatever funcion and then pass the pointer to the function,but we can just pass the address using &.what lucky says is right as well.when passing values,it will be a headache,but when we want to pass values,we dont want to do anything with the values,but use then at one place,while with variables,the values will be changed more than once,so i guess a pointer will be much better to use..i agree with lucky.
    Huh? I don't think you really understand what a pointer is. A pointer is a variable. It takes up memory just like any other variable. The only thing special about a pointer is that the value stored in the memory set aside for it is actually a memory address.

    Whether you're passing the value of a variable or the address of a variable, the called function still has to grow the stack. The only time it's more efficient is if the pointed-to variable is larger than the size of a pointer, but even that savings is miniscule. The hit taken from having to dereference the pointer inside the called function will outweigh any kind of savings you might save by passing a pointer to the function (except for structs of course).

    Emmanuel Delaha gave good guidelines. Go with those and you'll be fine.
    If you understand what you're doing, you're not learning anything.

  6. #21
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by itsme86
    Huh? I don't think you really understand what a pointer is. A pointer is a variable. It takes up memory just like any other variable. The only thing special about a pointer is that the value stored in the memory set aside for it is actually a memory address.

    Whether you're passing the value of a variable or the address of a variable, the called function still has to grow the stack. The only time it's more efficient is if the pointed-to variable is larger than the size of a pointer, but even that savings is miniscule. The hit taken from having to dereference the pointer inside the called function will outweigh any kind of savings you might save by passing a pointer to the function (except for structs of course).

    Emmanuel Delaha gave good guidelines. Go with those and you'll be fine.
    can you explain how dereferencing a pointer will outweigh the savings i make by passing the pointers??also,you forget that by passing pointers,i will not only save some space,but i will also get the ability to change the value of a variable in another funciton from some other function..this would not be possible without returning some values when we pass variables by value...

  7. #22
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Look at this program:
    Code:
    void func_value(int a)
    {
      a += 3;
    }
    
    void func_ptr(int *a)
    {
      *a += 3;
    }
    
    int main(void)
    {
      int a;
    
      func_value(a);
      func_ptr(&a);
    
      return 0;
    }
    And here's the assembly for func_value:
    Code:
    func_value:
            pushl %ebp
            movl %esp,%ebp
            addl $3,8(%ebp)
    .L2:
            leave
            ret
    And for func_ptr:
    Code:
    func_ptr:
            pushl %ebp
            movl %esp,%ebp
            movl 8(%ebp),%eax
            movl 8(%ebp),%edx
            movl (%edx),%ecx
            addl $3,%ecx
            movl %ecx,(%eax)
    .L3:
            leave
            ret
    Do you see how the dereferencing costs more than not dereferencing? It takes time and you have to dereference it every time you use it in the function.

    also,you forget that by passing pointers,i will not only save some space,but i will also get the ability to change the value of a variable in another funciton from some other function..this would not be possible without returning some values when we pass variables by value...
    No...I didn't forget. I said to follow Emmanuel Delaha guidelines and that was one of them. I was making an argument against using pointers for some mysterious "efficiency" reason that doesn't really exist. For passing large data types like structs it's fine. But you're passing a float? Forget it. And if it's an int then you're not even getting of the benefit...just costing yourself the dereferencing. But if you need the function to be able to modify the value in the calling function then yeah, duh, use a pointer.
    If you understand what you're doing, you're not learning anything.

  8. #23
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    thanks itsme86,i really didnt know about the dereferencing part of it.will keep it in mind in the future.thank you

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. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  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