Shigure
Shigure

Reputation: 433

Why does CopyRect WinAPI function exist?

There is the CopyRect WinAPI function, which simply copies a RECT to another.

I've been interested in the reason for the existence of the function ever since I dug it up. Is the assignment operator (=) or CopyMemory function not suitable for copying RECTs? Should I use the CopyRect function whenever possible?

Upvotes: 5

Views: 252

Answers (3)

Shigure
Shigure

Reputation: 433

Raymond Chen answered my question!

Copying a RECT generated a bunch of code. CopyRect saved memory in the era which we had only 256KB of memory.

Nowadays, we don't have to use it because manually copying a RECT can be done with a few instructions.

Upvotes: 2

Ari0nhh
Ari0nhh

Reputation: 5920

CopyRect is an old function, which is present in Win32 API mostly for the compartibility sake, like WinExec, GetVersion and so on. Its source code is pretty simple: enter image description here

On start it checks both parameters for the NULL values and then copies 4 members of the source record to the destination through the intermediate register. Perhaps there is a point in using this function, if you are operating with the RECT pointers. Otherwise - direct assigment is much cheaper.

Upvotes: 4

Adrian Mole
Adrian Mole

Reputation: 51874

The CopyRect function is (probably) designed for use with languages that don't allow direct assignments to structures! In C++ you can have, say: RECT src{ 0, 0, 10, 10 }, dst; then (somewhere, anywhere): dst = src;. However, there are some languages that don't allow this, so you have the function to do it. (It's a very long time since I wrote anything in Pascal, but how would you do it there? Or in COBOL??)

Similar arguments can be made for functions like SetRect() and EqualRect(), of course. But, the answer to your question depends, primarily, on what language you are using (none specified in tags) and, that language permitting, on your personal coding preference. (But note, if efficiency is a concern, calling a WinAPI function will always involve an actual call, not an inline!)

Upvotes: 4

Related Questions