I've been recently reading the forums a lot, seeing people throw around the phrase "pass-by-reference" in normal C. As far as I've been taught, C only provides pass-by-value. Now.....the developers of C came up with this genious idea of passing an address (which is still a value) through the parameters of a method (&variable) in order to SIMULATE pass-by-reference.

As far as I'm concerned, I dont care who says this IS pass-by-reference, I disagree. Just because a certain variable's value happens to be an address does not (in most C books i've referenced :cheesy: ) constitute pass-by-reference. I consider (not alone on this) a language to provide pass-by-reference if and only if the code specifically states it's passing a reference to the variable into the method (ref in C# and other languages). I myself, find this statement to be self evident.

I know this may stir a debate. Others may have a different view, but I am sort of tired of hearing that arrays are passed by reference when the address of the array (hense it's value) is passed in the C language. I'd be interested to hear a rebuttle.


EDIT: On a similar note, Java is also pass by value only. An objects value is an address in Dynamic Storage, and this address is passed into methods, not an actual reference to that variable.


Reguards,

Tyler S. Breton

Simple..pass by value was present way before references were introduced in C++, so to go out to say that you consider pass by reference only when a actual "reference variable" is passed is wrong.

Pass by reference is done in two ways:
1. Pass by reference using pointers
2. Pass by reference using reference variables

And array actually are passed by reference since the name of the array holds its address. And the reason why arrays are by default passed by reference is to avoid the creation of a temporary array inside the function since arrays normally pack a lot of data. Creating temporary may be a waste of processor time and the precious memory.

So reference passing at the very core implies passsing the address so that the object can be manipulated using its address rather than using its name which is used for normal variables.

But as you have created this thread I know many people would be jump on the discussion bandwagon readign the thread title :D

I pretty much agree with SOS. The act of passing a pointer seems to me to be a form of pass-by-reference since the value is not passed but a pointer that references the value. Therefore pass-by-pointer is akin to pass-by-reference.

And I would assume that rather than explaining all the itty bitty technical nuances between pass-by-pointer/reference it just simpler for the new programmer to grasp the concept as being the same. Time later to add the technical differences to their knowledge when the time is right.

Maybe I'm being thick, but I can't see any way of passing by reference except by passing a pointer. After all, if you want to write a value back to the original variable, you have got to know whereabouts that variable lives in memory. The method of passing by reference may not be explicitly spelt out in other languages as it is in C (or assembler), but it always does involve passing a pointer. No other way of doing it.

No, you can do it without a pointer (or by using a pointer that would be there already, like the stack pointer). When you call a function with the C calling convention, you push the last argument, the second-to-last argument, the third-to-last argument, ..., and finally the first argument onto the stack, and then you call the function (which places another pointer on the stack). So when you call f in the snippet x = 2; f(x,3,4); , the stack looks something like this:

+0x000C: 04 00 00 00
+0x0008: 03 00 00 00
+0x0004: 02 00 00 00
+0x0000: <return pointer>
-0x0004: <uninitialized/junk data>

where these addresses are relative to the register %esp (or maybe %esp+4, I don't remember.) I'm assuming regular old 32-bit here...

Pass-by-reference can be implemented by letting the callee function modify the variables at +0x0004, 0x0008, and 0x000C, and then having its caller use that as its location for that variable. For example, the callee above would use +0x0004 to store the variable x. This means the address is passed to the caller implicitly, not explicitly.

The thing is, this only works if what you're passing by reference is a local variable. Otherwise, the caller would have to copy the passed-by-reference variable's value to the stack when it makes the call and then copy the new version back when the function returns. So you might say, okay, this could be efficient for passing integers by reference, but not, say, vectors or trees or other fancy datastructures. (Actually you'd be just fine with certain types of trees...)

But you've still got one problem: threading. If the program is multithreaded, well, you just can't go making assumptions like having side effects happen whenever you want, waiting for the end of the function before the actual value gets assigned. And that's the reason why a compiler will compile it to have a pointer passed.

But anyway, the original poster is correct. C does not have pass by reference. You pass memory addresses by value. Whether you're trying to solve the same problem as pass by reference doesn't matter; these are language mechanics, not semantics, we are talking about.

Well in any normal program the variables will have been declared long before any call is made to f. Therefore the stuff which gets pushed onto the stack will be copies (ie values of) the original variables. If the called function then returns the result by placing it on the stack, instead of following the usual convention of returning it into the eax register, that is still return (and call) by value. The original variable will not have been modified. The caller might modify it subsequently (just as it might if the value was returned in the eax register), but that's a different matter.

Maybe I'm being thick, but I can't see any way of passing by reference except by passing a pointer.

If you have gone this far, wont it be more generic to say that inorder to pass by reference in the end you need the address of the data segment.

Be it language semantics or mechanics, be it pointers in C or references in C++, it all boils down to the simple concept of manipulating the contents placed at a given address( if you have the privileges ).

It should be well kept in thought that "Pass by reference" and "Passing reference variables" are two different things, well actually the latter is one techinique by which the former can be implemented.

No, you can do it without a pointer (or by using a pointer that would be there already, like the stack pointer).

Do also consider cases in which there is no such thing as a stack or a stack pointer -- the languages evolved from such situations.

If you have gone this far, wont it be more generic to say that inorder to pass by reference in the end you need the address of the data segment.

You need a pointer, whether that be near or far. If the program only has one data segment (as most have) you don't need to push the contents of the ds register, because the current data segment is assumed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.