|
-
Nov 8th, 2002, 02:43 PM
#1
Thread Starter
Fanatic Member
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 *pt1, POINT *pt2)
{
POINT pts[1];
pts[0] = *pt1;
pts[1] = *pt2;
Polyline(hDC, &pts[0], 2);
}
usage
PHP Code:
POINT pt1, pt2;
pt1.x = 0; pt1.y = 0;
pt2.x = 10; pt2.y = 20;
o.setlinecolor(RGB(0, 15, 255));
o.drawline(&pt1, &pt2);
Thanks in advance.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Nov 8th, 2002, 02:50 PM
#2
Monday Morning Lunatic
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
-
Nov 8th, 2002, 03:02 PM
#3
Thread Starter
Fanatic Member
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}
-
Nov 8th, 2002, 03:31 PM
#4
Monday Morning Lunatic
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
-
Nov 8th, 2002, 03:39 PM
#5
Thread Starter
Fanatic Member
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 *pt1, POINT &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}
-
Nov 8th, 2002, 03:48 PM
#6
Monday Morning Lunatic
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
-
Nov 8th, 2002, 03:51 PM
#7
Thread Starter
Fanatic Member
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}
-
Nov 8th, 2002, 07:08 PM
#8
Thread Starter
Fanatic Member
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Nov 8th, 2002, 08:29 PM
#9
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.
-
Nov 8th, 2002, 09:02 PM
#10
Frenzied Member
Polyline(..., &pt1, 1); SHOULD work =).
Z.
-
Nov 8th, 2002, 09:14 PM
#11
Thread Starter
Fanatic Member
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}
-
Nov 8th, 2002, 09:18 PM
#12
Frenzied Member
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.
-
Nov 8th, 2002, 09:26 PM
#13
Thread Starter
Fanatic Member
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}
-
Nov 8th, 2002, 10:17 PM
#14
Frenzied Member
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.
-
Nov 8th, 2002, 10:26 PM
#15
Thread Starter
Fanatic Member
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}
-
Nov 8th, 2002, 10:29 PM
#16
Frenzied Member
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.
-
Nov 8th, 2002, 10:48 PM
#17
Thread Starter
Fanatic Member
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 *pt1, POINT *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 &pt1, POINT &pt2)
{
POINT pts[2];
pts[0] = pt1;
pts[1] = pt2;
Polyline(hDC, &pts[0], 2);
}
// usage
o.drawline(pt1, pt2);
...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}
-
Nov 9th, 2002, 06:05 AM
#18
Monday Morning Lunatic
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
-
Nov 9th, 2002, 07:11 AM
#19
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.
-
Nov 9th, 2002, 07:52 AM
#20
Monday Morning Lunatic
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
-
Nov 9th, 2002, 09:20 AM
#21
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.
-
Nov 9th, 2002, 02:18 PM
#22
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|