Results 1 to 22 of 22

Thread: By reference & Pointers

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987

    By reference & Pointers

    I tried searching for something to help me on this but with no luck, so here goes my question. I am trying to pass a struct by reference (because it seems like the right thing to do) and I get no compilation errors however when the function gets called my app crashes immediately. Here is what I have.

    Function Declaration
    PHP Code:

    void drawline
    (POINT *, POINT *); 
    Function code
    PHP Code:

    void OSDC
    ::drawline(POINT *pt1POINT *pt2)
    {
     
    POINT pts[1];
     
     
    pts[0] = *pt1;
     
    pts[1] = *pt2;
     
     
    Polyline(hDC, &pts[0], 2);
     

    usage
    PHP Code:

    POINT pt1
    pt2;
               
    pt1.0pt1.0;
               
    pt2.10pt2.20;
               
    o.setlinecolor(RGB(015255));
               
    o.drawline(&pt1, &pt2); 
    Thanks in advance.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    POINT pts[1];

    That only allocates space for one element (index 0). You need to allocate two.

    PS: That's passing by pointers, not references. I'd recommend using a const reference:
    Code:
    void OSDC::drawline(const POINT &pt1, const POINT &pt2) {
        POINT pts[2];
    
        pts[0] = pt1;
        pts[1] = pt2;
    
        Polyline(hDC, pts, 2);
    }
    
    // ...
    
    o.drawline(pt1, pt2);
    Although I wouldn't recommend this, because it performs two structure copies per operation. Surely there's a more efficient way?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    POINT pts[1] D'OH, lol, still weening myself from VB. I looked up using the const keyword in the param list and from I can tell that allows you to pass the address of a variable without allowing the function to change the value. Anyway kinda confused which method you don't recommend.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I don't recommend any method that requires a copy, although it might be inevitable. Perhaps providing a version of that function that takes a two-element array as well?

    RE: const. Yep, does exactly what it says on the tin.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    So just to keep bugging with innane questions I should probably already know this method...

    PHP Code:
    void OSDC::drawline(const POINT &pt1, const POINT &pt1
    only passes the address of the variables and this method...

    PHP Code:
    void OSDC::drawline(POINT *pt1POINT &pt1
    passes a copy of the actual data?

    Sorry for the stupid questions, the book Im using as a reference doesn't speak in understandle terms.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Passing a reference and a pointer both only pass the address. However, a reference is not allowed to be NULL. With a pointer, you need to check that it's not NULL when it's passed in.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    So passing a reference is generally more efficient due to the fact that you don't have to check if the pointer is NULL?
    Last edited by YoungBuck; Nov 8th, 2002 at 07:07 PM.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    ???
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    If you don't check for NULL there is no difference. Absolutly none, except in syntax (the reference thing is easier, which is why it is to be preferred).
    But you could pass NULL for the pointers, so you should check for NULL, unless it's some high-performance function or a wrapper, in which case you could just rely on the caller not passing NULL - at least in release builds (there's a nice macro called assert for such things).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Polyline(..., &pt1, 1); SHOULD work =).

    Z.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    That does work Zaei, just not sure why. Is it because pt1 & pts[0] refer to the same memory address and the polyline function just steps to pt2.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  12. #12
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    No, it is because pt1 and pt2 are pushed onto the stack one afte the other. pt2 is located at (&pt1)[1]. Its just using the way the compiler calls functions to your advantage.

    Z.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    I don't really care about the function at hand just more interested in the logistics of all this, so I'll continue until I understand completely .

    So if I were to declare a variable inbetween pt1 and pt2 then using Polyline(...., &pt1, 1) wouldn't work because they would be put in the stack in a different order.

    pt1
    other var
    pt2

    ???
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  14. #14
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Thats right. The stack grows downward, which is why this works...
    Code:
    [stuff] <- stack pointer is here
    ...
    
    push pt2
    [stuff]
    [pt2] <- stack pointer is here
    
    push pt1
    [stuff]
    [pt2]
    [pt1] <- stack pointer is here
    Notice how the parameters are passed right to left... so the slot above pt1 is pt2. If you push something between those, you no longer have that nice continuous memory.

    By the way, there is no real guarentee that this will work on all platforms =)

    Z.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    So in other words, while the method you described works (Polyline(..., &pt1, 1)), it's not generally recommended?
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  16. #16
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    I wouldnt do it if i didnt have to. Its not really worth the couple of clock cycles you save. It was really just a demonstration =).

    Z.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    OK thanks Zaei, it's nice to know one's alternatives and in summary to my whole confusion in the whole situation, this is what I have gathered.

    This...
    PHP Code:

    //declaration
    void drawline(POINT *, POINT *)

    //function
    void OSDC::drawline(POINT *pt1POINT *pt2)
    {
     
    POINT pts[2];
     
     
    pts[0] = *pt1;
     
    pts[1] = *pt2;
     
     
    Polyline(hDC, &pts[0], 2);
     
    }

    //usage
    o.drawline(&pt1, &pt2); 
    .... is the same as this ....

    PHP Code:

    // declaration
    void drawline(POINT &, POINT &);

    //function
    void OSDC::drawline(POINT &pt1POINT &pt2)
    {
     
    POINT pts[2];
     
     
    pts[0] = pt1;
     
    pts[1] = pt2;
     
     
    Polyline(hDC, &pts[0], 2);
     
    }

    // usage
    o.drawline(pt1pt2); 
    ...except for the difference in syntax, everything else is happening the exact same and they are both equally efficient methods?
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you use const, then the compiler might be kind enough to inline it for you, because it guarantees there are no side-effects from changing them.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, the compiler will generate equivalent code. Inlining is usually only done for functions explicitly marked to be used for inlining (unless you set some compiler options).

    Using const is always good when you don't need to change values, as it allows you to pass in things that already are const.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    MSVC++ ignores the "inline" keyword, at least in version 6. I didn't check the asm output in 7.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What do you mean, ignores? Does it always inline what it wants? Or doesn't it inline anything (nah...)? I'm sure I saw a compiler setting for that in 6. My 7 doesn't optimize anyway, it really sucks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It always inlines if it can if optimisations are enabled, and ignores the keyword. There's a __forceinline or something like that, and a couple of #pragmas you can use to hint it, though.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width