Sign in to follow this  
gp343

C and Call by Value query

Recommended Posts

I'm just reviewing C and its Call by Value mechanism. I've stumble upon the wglUseFontOutlines function - http://msdn2.microsoft.com/en-us/library/ms537567.aspx When I examined the wglUseFontOutlines function signature/parameter, I've notice that the HDC and LPGLYPHMETRICSFLOAT parameters are NOT declared as pointers but rather as regular/normal parameters. With that in mind, can someone please educate me on how wglUseFontOutlines is modifying/updating the actual parameters passed in for HDC and LPGLYPHMETRICSFLOAT when according to the K & R book HDC and LPGLYPHMETRICSFLOAT should be only "LOCAL" copies to the wglUseFontOutlines function? Many thanks.

Share this post


Link to post
Share on other sites
HDC is a handle - "Handle to Display Context".

LPGLYPHMETRICSFLOAT is hungarian notation for "pointer to array of GLYPHMETRICSFLOAT" - as the documentation describes it.

Share this post


Link to post
Share on other sites
So the Call by Value concept only work for struct, union, primitives and NOT for Array, Pointers in C?

Share this post


Link to post
Share on other sites
Quote:
Original post by gp343
NOT for Array, Pointers in C?


Without going into pedantic details...

Arrays are almost identical to pointers, and dynamically allocated arrays are pointers.

Pointers get passed by value, but their contents don't. If you pass a struct, you pass contents. If you pass a pointer to struct, you copy (by value) value of pointer (usually a memory address), but not the actual contents. Same for arrays.

If you have pointer to array of 4 ints, the memory layout looks like this:

0x0036
0x0040 ptr_to_0x0680 // pointer to array
0x0044
...
...
0x0676
0x0680 8
0x0684 9
0x0688 10
0x0672 11


Then, of you read memory address of 0x0040, you get value of 0x0680. That is pointer to array of 4 ints (8,9,10,11).

When you pass this pointer to a function, the value 0x0680 is copied effectively as int, and by value, but the contents of array are not.

PS: simplified version, in reality there's a few details, but the above concept is universal.

In memory managed languages, pointer are handles, and are not required to be related to actual memory layout, or memory addresses.


Share this post


Link to post
Share on other sites
Quote:
Original post by gp343
So the Call by Value concept only work for struct, union, primitives and NOT for Array, Pointers in C?


Call by value applies to everything in C: if you pass a value as an argument, a copy of that value will be available in the called function.

The only exception here is arrays: arrays cannot be passed as arguments in C. If something looks like you're passing an array as an argument, in fact the compiler will have converted it to a pointer argument and silently converted your array to a pointer to its first element. However, even then, the resulting pointer is passed by value.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this