Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774

    Function parameters choice?

    Im working through the c++ vtms, and started my evil monkeys.
    Without giving any game away, Im going to ask 1 question - as I guess while its choice, Im left wondering why the choice they made was the one made.

    Code:
    function a(char *c)
    {
      *c = something;
    }
    
    a(&mykey);
    Now, in the earlier vtm they did point out thats basically the same as

    Code:
    function a(char &c)
    c = something;
    }
    
    a(mykey);
    so, why would you use the pointer and so on referencing when it just means you have to reference and dereference all over the place?

    PS - I understand when sending pointers to arrays etc.. but in the above senario?
    Last edited by LizKimber; 11-22-2008 at 08:01 AM. Reason: clarification
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  2. #2
    Join Date
    Nov 2006
    Location
    Texas
    Posts
    521
    Quote Originally Posted by LizRiley View Post
    so, why would you use the pointer and so on referencing when it just means you have to reference and dereference all over the place?

    PS - I understand when sending pointers to arrays etc.. but in the above senario?
    From what I understand sending pointers is memory effiecent. When passing a parameter to a function, the variable is copied to the function. Arrays on the other hand can become very large. It would be ineffiecent to copy the entire array to the functions params. It would just be better to use a pointer to point to the address of an array, if an array is a function parameter.

    If I remember correctly the pointer well point to element 0. The beginning of the array. The array is contigous in memory so finding the rest of the elements would be easy. You can use a for loop and some array arithmetic.

    Code:
    example:
    int myarray[8] = {0,0,0,0,0,0,0,0}
    int *something = &myarray; OR        /// element 0
    int *something = &myarray + 1; OR  /// element 1
    int *something = &myarray + 2; OR   /// element 2
    int *something = &myarray + 3; // and so on  ///element 3
    I hope the syntax is correct. Hope this helps

    Code:
    function a(char &c)
    c = something;
    }
    
    a(mykey);
    Whatever something equals its copied again for "c" thus using more memory.




    Code:
    function a(char *c)
    {
      *c = something;
    }
    To my understanding doing it this way well save some memory sence we just point the address of whatever something equals. Think you home address, mail and post office.

  3. #3
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    no, & passes the reference, so its the same in a way as the pointer hence my question - this is proved in the pointers vtm (you can view) they show it as the same mem reference, and indeed show its nice as you can use . notificaiton rather than -> with classes

    the differences of

    void funca(char c);
    void funca(char *c);
    void funca(char &c);

    the first creates a copy, and I get that, and understand why not to do that unless you need to.

    However, the second 2, I get why * maybe better for arrays, but what I dont get is why you'd use * for anything else, over &
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  4. #4
    Join Date
    Jan 2004
    Posts
    76
    its all personal preference,
    if yout use..

    Code:
    void a(int *c)
    {
      *c = something;
    }
    you have to call it like..
    a(&myInt);
    so who ever calls it will see that this funktion may change the data.

    but if you use..

    Code:
    void a(int &c)
    {
      c = something;
    }
    you can call it like
    a(myInt)
    and may be surprised that your data has changed.
    other than that there is no difference

  5. #5
    Join Date
    Oct 2006
    Posts
    1,548
    Actually theres a big difference between Pointers and References, just look up on the many "difference between pointers and references" links on google
    [quote][\quote]

  6. #6
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Ok, I thought it was a simple question, and as its not the choice Id have made, Id have gone with reference for such a simple function, to avoid the necessity of then having to reference everything, I was hoping someone would come up with a reason relavtive to these VTMs as to why the decision went the way it did.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  7. #7
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    It seems to me that DA HUI has summed it up perfectly. In the end, it makes no diference which convention you choose to use, as they both generate identical machine code. I prefer using the

    void function(varType *param)

    notation myself, as it indicaes each time I use the function that it has the ability to modify the value of the variable it is passed.

    Please, take a look at the following code. It's been compiled with MinGW, and the assembly dump provided by OllyDBG.

    C code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    int something = 0xBADF00D;
    char *helper = "marker";
    
    void a(int *c)
    {
      *c = something;
    }
    
    void b(int &c)
    {
      c = something;
    }
    
    int main()
    {
        int z;
        char *tmp;
    
        tmp = helper;
        printf("%s", tmp);
        a(&z);
    
        tmp = helper;
        printf("%s", tmp);
        b(z);
    
        getch();
        return 0;
    }
    Assembly listing:
    Code:
    00401310      /$  55                         PUSH EBP
    00401311      |.  B8 10000000                MOV EAX,10
    00401316      |.  89E5                       MOV EBP,ESP
    00401318      |.  53                         PUSH EBX
    00401319      |.  83EC 14                    SUB ESP,14
    0040131C      |.  83E4 F0                    AND ESP,FFFFFFF0
    0040131F      |.  E8 6C050000                CALL funcTest.00401890
    00401324      |.  E8 E7000000                CALL funcTest.00401410
    00401329      |.  C70424 07304000            MOV DWORD PTR SS:[ESP],funcTest.004>; |ASCII "%s"
    00401330      |.  A1 00204000                MOV EAX,DWORD PTR DS:[402000]       ; |
    00401335      |.  8D5D F8                    LEA EBX,[LOCAL.2]                   ; |
    00401338      |.  894424 04                  MOV DWORD PTR SS:[ESP+4],EAX        ; |
    0040133C      |.  E8 B7050000                CALL <JMP.&msvcrt.printf>           ; \printf
    00401341      |.  891C24                     MOV DWORD PTR SS:[ESP],EBX
    00401344      |.  E8 A7FFFFFF                CALL funcTest.004012F0
    00401349      |.  C70424 07304000            MOV DWORD PTR SS:[ESP],funcTest.004>; |ASCII "%s"
    00401350      |.  A1 00204000                MOV EAX,DWORD PTR DS:[402000]       ; |
    00401355      |.  894424 04                  MOV DWORD PTR SS:[ESP+4],EAX        ; |
    00401359      |.  E8 9A050000                CALL <JMP.&msvcrt.printf>           ; \printf
    0040135E      |.  891C24                     MOV DWORD PTR SS:[ESP],EBX
    00401361      |.  E8 9AFFFFFF                CALL funcTest.00401300
    00401366      |.  E8 55050000                CALL <JMP.&msvcrt._getch>           ; [_getch
    0040136B      |.  8B5D FC                    MOV EBX,[LOCAL.1]
    0040136E      |.  31C0                       XOR EAX,EAX
    00401370      |.  C9                         LEAVE
    00401371      \.  C3                         RETN
    As you can see from the parts of the code I've highlighted, the generated code is identical. It's just a feature of the C language.

    So again, each perform the same task in the same way - it's just that I find the void function(varType *param) notation to be more clear.

    S.
    Last edited by enhzflep; 11-24-2008 at 06:45 AM. Reason: spelling, gramaticall errors

  8. #8
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Thanks Enhzflep for that, I knew they were the same, I guess the idea of marking it so you are reminded that this variable may never be the same again is a useful thing especially in big projects.

    I always tended to & however.. maybe this is something i needed to change.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  9. #9
    Join Date
    Jul 2008
    Location
    Oak Lawn, IL
    Posts
    8
    Well, there is a difference actually between passing parameters by references or as a pointers.. there are only 2 types of passing parameters to functions (by value, or by reference) passing a pointer is actually passing by value which imitate the behavior of passing by a reference..

    if you declared a function to take a pointer as a parameter, you can only pass an address to the function [Not the value of the variable!!]

    on the contrary, if you declared the function to take reference as a parameter, you just
    have to pass the variable as it, without caring about its address..

    And the most important thing that makes the difference between pointers and references, is that a Pointer is an actual variable that its value contains the address of another variable, which means it's an alias of another variable, but when passing a Pointer to a function you actually making copy of the actual pointer variable in the stack. (more extra work, although the value of the passed parameter changes affects the real variable pointed to.. so it changes the value of the passed parameter..)

    While passing by reference it's almost the same thing as passing by Pointers, except that a reference is not a variable itself, which means no copying of the passed parameter, since it takes the address of the passed variable itself (no 3rd party variable like when dealing with pointers)..

    and technically the fastest way to call a function is to avoid the overhead of creating a copy of the parameters which is using "&" references.

    and if you want the protection from changing -accidentally- the value, that being passed, you could declare the function to take something like "funcName(const int &number); which protects the variable from being changed and save both memory and precious time of copying the variable "number" when passing it to the function..

    VS2008 suggests this when writing the function prototype, it brings a tool tip showing that.. also from the VTMs, Joel did this in one of those lessons without explaining anything, (while Dan was talking about Chinese food and egg rolls :drool: ) also when I was reading Deitel & Deitel book C++ how to program, it contained extensive explanations about these stuff..

    Oh sorry for the long post and reviving this thread, I forgot myself while writing..

  10. #10
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    No need to apologise. The key difference seems to be(in terms of practicality) that by reference wont take a null, a pointer will.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •