Thread: & vs * in param list?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    & vs * in param list?

    What the difference between
    Code:
    void myfunc(mytype &a);
    void myfunc(mytype *a);
    From what I've found reading online, in both cases, modifying the 'a' modifies the param passed into the function after it returns. But if mytpe is a struct, in the 1st, it requires . notation and the 2nd needs -> notation.

    I know only a pointer to a is passed in the 2nd, so not much gets added on the stack when the func is called, but what about the 1st? Is pointers on my system are 4 bytes, is their only an extra 4 bytes added to the stack in the 1st?

    Hopefully that q was clear.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah, references are passed as pointers under the hood.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    Yeah, references are passed as pointers under the hood.
    No, that's not guaranteed.
    A reference is an alias. If you pass "b" to a function which takes a reference named "a", then "a" is an alias for "b". And that is all.
    So whatever happens to a, happens to b.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Above you have both the real-world answer and the theoretical answer. Great huh!
    Neither necessarily uses stack space. Registers are often used to pass parameters. Stack space may not be required for them at all if the function is simple enough.

    What you also should know is that using a reference means that:
    you can't accidentally reassign 'a' to point to something else, and
    the passed in parameter cannot be NULL.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    No, that's not guaranteed.
    A reference is an alias. If you pass "b" to a function which takes a reference named "a", then "a" is an alias for "b". And that is all.
    So whatever happens to a, happens to b.
    By the same token, pointers aren't guaranteed to be memory addresses either. What's under the hood is be definition implementation defined.

    But in the common case, pointers and references are passed through the stack as 4 byte memory addresses. And both have the same overhead.
    Last edited by King Mir; 06-17-2011 at 05:32 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing ... as a param?
    By homer_3 in forum C Programming
    Replies: 3
    Last Post: 06-29-2010, 12:53 AM
  2. set param in activeX control
    By sgh in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2009, 02:54 AM
  3. [?] what's the doese param in destroy(class* &i) mean?
    By martinuk in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2009, 12:07 PM
  4. Pass Param out in Activex
    By Duyetnv in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 05:17 AM
  5. Getting File param name
    By CodeMonkey in forum Windows Programming
    Replies: 10
    Last Post: 09-04-2002, 07:35 PM