Pointers: why not?

This is a discussion on Pointers: why not? within the C Programming forums, part of the General Programming Boards category; I've been thinking about this for some time, and am very curious: Other than to prevent the function being called ...

  1. #1
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115

    Pointers: why not?

    I've been thinking about this for some time, and am very curious:

    Other than to prevent the function being called from being able to overwrite the variable in question, is there any reason why you would specifically want to pass a variable to a function, instead of a pointer to that variable? It would seem that using pointers as much as possible would be good for performance/footprint, but I don't want to overdo it if there are other considerations.

    I hope this isn't a terribly stupid question; I've yet to read up on some of the more advanced aspects of C, and it's possible my question is answered there...
    I live in a giant bucket.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When you don't need the power of pointers, there's no reason to have two variables instead of one.

  3. #3
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    I thought having a pointer would let you manipulate the original variable in the calling function, thus preventing one from having to create a second variable.

    Or are you talking about having two conceptual variables, since you can name two local variables the same thing and conceptually treat them as the same thing, if one's used as an argument to get the data into the called function?

    Edit: personally, I'd be more confused(and write more bugs) if I tried what I just suggested, I'm just trying to work out what you meant in your post.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As far as performance goes, it truthfully depends upon how big the variable is. But for what you are saying, for most items where it makes most sense to pass pointers everytime, pointers are passed everytime. I'd need to see an example of what you suggest needs fixiing.

  5. #5
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    I'm not trying to fix something, I'm trying to determine when pointers are/are not a good idea when trying to make data available to functions.

    And yeah, I know that passing, like, pointer to a char might not be the most wildly intelligent of ideas, unless you REALLY have a good reason... but say with floats or the like, I thought I'd like to know if there are any reason I'd particularly want to pass the actual data instead of a pointer to it.
    Last edited by Aerie; 12-19-2004 at 05:32 PM.
    I live in a giant bucket.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm not saying you can't modify the original variable used to cal the function without pointers. What I'm saying is that if there's no actual reason for you to use a pointer (like if you're not using an array), then having a pointer to that variable is just a waste of space.

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    A common programming error occurs when a pointer is passed and the function attempts arithmetic operations with the value. Which doesn't operate properly because it's not using a variable's value, rather it's modifying an address. Value++ produces a completely different result than pointertoValue++.

    That's just one reason off the top of my head. However really the predominate reason is just to ensure and enforce the priniciples of least priveledge.
    Last edited by Scribbler; 12-19-2004 at 07:45 PM.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That error has more to do with the programmer using the function incorrectly. The programmer is responsible for knowing the function's prototype and passing it the right information.

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Yes that is true, however projects with more than one coder, or somebody else's code is not always readily apparent to someone other than the original coder. The principles of least priveledge help reduce the chance of it happening.

  10. #10
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >I thought having a pointer would let you manipulate the original variable in the calling function, thus preventing one from having to create a second variable.

    For a 32-bit address space, if a value is passed it is added to the stack (whatever the size), if a pointer is passed, the 32-bit address is added to the stack, so, in the case that you are deciding to pass either an int, or a pointer to an int, they would incur the same overhead it would seem. However, you'd still have to dereference within the function, making it an extra instruction.

    Also, as mentioned, access rights help determine it. In general, pointers are not passed if unnecessary, and I would think that the reason I mentioned above (if anything else) would suffice.
    hasafraggin shizigishin oppashigger...

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Everyone's talking about performance but I haven't seen anyone mention the usage of the pointer inside the function. If you had a function like:
    Code:
    int sum(int *a, int *b)
    {
      return *a + *b;
    }
    The CPU has to fetch the value inside each pointer variable, and then it has to fetch the value at that memory address. a is one fetch, *a is two fetches. For more complex functions you can see how it would be much more efficient to just have the value instead of a pointer to the value.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by itsme86
    The CPU has to fetch the value inside each pointer variable, and then it has to fetch the value at that memory address. a is one fetch, *a is two fetches. For more complex functions you can see how it would be much more efficient to just have the value instead of a pointer to the value.
    Isn't the address pointed to simply a value in a register? No additional memory is allocated for a new variable, nor is there any additional instruction to store those values into another memory address.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    The address is a memory address somewhere in your computer's memory. If the function has few enough variables and the CPU is able to hold that kind of variable in a register and the compiler is smart enough to keep it in the register, then yeah...it doesn't require a fetch.

    But *a would still need to look at 2 registers instead of 1, which admittedly is much faster than 2 memory fetches, but you better have a really simple function with the right variable types in order to count on it happening.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Aerie
    I'm not trying to fix something, I'm trying to determine when pointers are/are not a good idea when trying to make data available to functions.

    And yeah, I know that passing, like, pointer to a char might not be the most wildly intelligent of ideas, unless you REALLY have a good reason... but say with floats or the like, I thought I'd like to know if there are any reason I'd particularly want to pass the actual data instead of a pointer to it.
    Pointers are good when:
    - There is no alternative (arrays)
    - The function have to modify the original value of a variable (write access)
    - The data are bigger that an int (typically, structures with at last 2 members)
    Emmanuel Delahaye

    "C is a sharp tool"

  15. #15
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by sean_mackrory
    When you don't need the power of pointers, there's no reason to have two variables instead of one.
    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.

Page 1 of 2 12 LastLast
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, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21