Simple pointer question
I had this
LPRECT rec;
playback->GetClientRect(rec);
and it froze up everytime. So I try this eventually:
RECT rec;
playback->GetClientRect(&rec);
What is the difference between address of a RECT and a LPRECT which i assume is RECT *rec.
Oh yeah here is msdn function header of getclientrect()
BOOL GetClientRect(
HWND hWnd,
LPRECT lpRect
);
# 1 Re: Simple pointer question
In your first example rec is an uninitialized pointer. Might point anywhere.
Kurt
ZuK at 2007-11-9 1:25:14 >

# 2 Re: Simple pointer question
RECT rec1; // Create object of type RECT
playback->GetClientRect(&rec1); // Call function with address of object rec1
LPRECT rec2; // Create an uninitialized pointer
playback->GetClientRect(rec2); // Call function with address inside unintialized pointer
LPRECT rec3 = new RECT; // Create object of type RECT on heap
playback->GetClientRect(rec3); // Call function with address of newly created object
delete rec3; // Delete object again (don't forget)Hope this clarifies things a bit.
# 3 Re: Simple pointer question
So I have to create a new RECT object and initialize the pointer with that to be able to do what i was trying to do before? So my attempt is giving it a pointer that points nowhere and the function cant use that pointer? If I initialized it to NULL could that function fill that in then?
//wrong
LPRECT rec;
playback->GetClientRect(&rec)
//right way?
LPRECT rec = new RECT();
playback->GetClientRect(&rec);
delete rec;
# 4 Re: Simple pointer question
So I have to create a new RECT object and initialize the pointer with that to be able to do what i was trying to do before? So my attempt is giving it a pointer that points nowhere and the function cant use that pointer? If I initialized it to NULL could that function fill that in then?
//wrong
LPRECT rec;
playback->GetClientRect(&rec)
//right way?
LPRECT rec = new RECT();
playback->GetClientRect(&rec);
delete rec;Just read the documentation of the function you are calling:Parameters
hWnd
[in] Handle to the window whose client coordinates are to be retrieved.
lpRect
[out] Pointer to a RECT structure that receives the client coordinates. The left and top members are zero. The right and bottom members contain the width and height of the window.Your pointer does not point to a RECT structure, it points to nowhere. But let's assume for a moment you could pass in a NULL pointer and the GetClientRect function would create the new RECT object for you: Who would delete this object again?
# 5 Re: Simple pointer question
The function expects that you are giving it the address of somewhere valid.
The function you're calling has no way to check if what you're sending is invalid (unless there is a check in the function for a NULL pointer).
That's why it freezes when you give it a pointer that isn't initialized. For example,
void foo()
{
LPRECT prect;
SomeFunc(prect);
}
The prect has a value that we don't know -- it could be anything. Say it is 0x0287A873, which is garbage. When you pass this to the function SomeFunc, the SomeFunc function has no way to check if this value 0x0287A873 is junk or not.
Without an extension to the C++ language or by using a third-party library of some sort, there is no way to check for bad pointers, so the function trusts what you're sending is valid, and then goes ahead and attempts to use it.
So I have to create a new RECT object and initialize the pointer with that to be able to do what i was trying to do before?Yes, but it isn't neccessary. All the function cares about is if you are giving it an address that refers to a valid RECT type. It doesn't care how you got the address, so long that the address refers to a valid RECT type.
Your code does an unnecessary call to new and delete. You could simply just do this:
RECT rec;
playback->GetClientRect(&rec);
You are now passing the address of a known RECT variable. That is all the function wants -- an address, but it has to be a valid address.
I think you're being confused, because you saw the prototype as taking an LPRECT, so you believed you must declare an LPRECT, and then somehow manipulate it correctly so as to be passed to your function. This is incorrect.
All an LPRECT parameter type to a function means, is that the function expects the address of a RECT type, since the address of a RECT is an LPRECT. You don't have to declare any pointers at all -- just give the function what it wants, and again, that is an address.
Regards,
Paul McKenzie
# 6 Re: Simple pointer question
//right way?
LPRECT rec = new RECT();
playback->GetClientRect(&rec);
delete rec;This would be incorrect as well. In this case it should be GetClientRect( rec );
Looking at this another way, consider comparing the size difference between the LPRECT and the RECT.
size_t sizePointer = sizeof( LPRECT ); // 4 bytes
size_t sizeRect = sizeof( RECT ); // 16 bytes
GetClientRect needs space to write the result to. In fact it needs 16 bytes worth of space. If you just allocate the LPRECT on the stack and pass it in, it will overwrite the 4 bytes allocated for the pointer and you'll crash.
What it needs is the starting point of a RECT (that's got 16 bytes allocated). That's what you are giving it by doing:
RECT rect;
GetClientRect( &rect );
The &rect is returning the pointer to the stack allocated rect variable.
In general whenever you see some windows api that take a pointer you can either allocate an object on the stack and pass in the address of it (using the & notation) or you can create an object on the heap and pass in the pointer to the object;
LPRECT pRect = new RECT;
GetClientRect( pRect );
delete pRect;
Usually you can just declare the object on the stack (so you don't need to worry about cleaning up.
Arjay at 2007-11-9 1:30:12 >

# 7 Re: Simple pointer question
I see. Thanks guys for all the help I was confused and did think I had to use an LPRECT.