printf and scanf

printf and scanf are call by value or call by reference and why???
They exist in C, and C code doesn't do references :p
well actually they do...*cough reference pointers*
Right. A reference is an invisible pointer. A pointer is a visible reference. It really is a strange duplication.
A reference is an invisible pointer. A pointer is a visible reference.


In C++, a pointer is an actual object that takes up memory and has a value. A reference is another name for an object. That object has size and takes up memory. The reference is just another name for it.

A reference is not an invisible pointer, as a pointer is an actual object that has size and takes up space in memory.

While I'm here, a pointer is not a visible reference either, for much the same reason.
Last edited on
C only does pass by value, and so printf and scanf are pass by value.

C doesn't do pass by reference, if you want a reference passed you have to pass the pointer yourself by value.
No. A reference IS a pointer, that is optimized more strongly because of limitations on its functionality.
1
2
3
4
5
void ref(int &i){i=1;}
int main(){
	int x=0;
	ref(x);
}

Compile that code, without optimizations.
You'll find these lines in the assembly for ref():
1
2
	movl	8(%ebp), %eax ;get i from the arguments.
	movl	$1, (%eax) ;put 1 into what is pointed by i.

A reference is an invisible pointer, with restrictions which make inlining easier. End of story.
I disagree. The C++ standard makes it clear that a pointer is a complete object. The C++ standard makes no such statement regarding a reference. The implementation of a reference is left to the compiler. One way of handling it is to do as you said. There are other ways.

Saying IS in big letters make no difference. Here's someone else doing the same thing with italics, and they have a different spin; they say that the reference is the object.

http://www.parashift.com/c++-faq-lite/references.html#faq-8.1

Interestingly, their implementation does much the same as yours, but they don't find themselves compelled to insist that it's some kind of pointer. Perhaps because it's clearly not a pointer, as defined in the standard. Are you actually saying that it's not actually a bona fide pointer, but it behaves like an "invisible pointer", whatever it means for a pointer to be invisible?

You are asserting as fact one possible means of meeting the standard. You are taking one compiler's interpretation and insisting that it is universal fact.

Pointer takes up memory. Reference does not have to take up memory; it is left to the implementation (very clearly stated in the standard). To assert that a reference is a pointer in the face of this seems nonsensical to me. Are we actually talking about the same thing?

While I'm here...

Seraphimsan wrote:
*cough reference pointers*


The C++ standard nowhere contains this phrase. They do not exist in standard C++. I'd be very surprised if they existed in C. Again, I wonder if we're talking about the same thing. When I say "pointer" and "reference", I mean as defined in the C++ standard.
Last edited on
Right, it doesn't have to take up memory according to the standard. But, according to common sense, unless the function is inlined, the code for the function must be able to accept any integer, and assign to it in a way visible to the caller. The only way to efficiently do this is to pass in the address of the integer (thus taking up stack memory), and then do an indirect move. If you disagree, show me a way to pass in a reference to an integer, without passing in the address, and prove that at least one full-fledged implementation of c++ uses that way.
The standard may dictate the semantics, but the implementation is limited by what is fast on modern computers.
PS. By invisible I mean that you can't tell when x is a reference just by looking at it; you must look back to its definition.
Last edited on
The only way to efficiently do this is to pass in the address of the integer (thus taking up stack memory), and then do an indirect move. If you disagree...


I don't disagree with that and I'm certainly not here to write alternative compilers. My disagreement was with your assertion that a C++ reference is a C++ pointer, which you seem to have now stated yourself by explaining your meaning of invisible to indicate that actually it's not a pointer. The standard does dictate the semantics, and lots more besides; you appeared to be contradicting it, but now you're not.
closed account (E3vDSL3A)
Overall, the question to me seems to be:

Does it even really matter?
Last edited on
Well, it does if you're deciding between using a pointer and using a reference. At the low level, references are exactly like pointers. However, their functionality is limited in that they cannot be assigned to, there is no null reference you can use as a sentinel, they cannot be chained (no way to simply make a reference to a reference), and many others. So in terms of performance they are equal, but references are safer.
EDIT: Now that I think of it, a reference has the same limitations as a constant pointer.
int &x has the same limits as int * const x.
So it really is a complete duplication of functionality.
Last edited on
when I say reference pointer I mean the pointer that points to the address that references the variable in question.
of course I'm probably using the wrong term here. seeing as I'm entirely self taught in c++ and i'm taught through experimentation and not through a text book.
Last edited on
There are other ways to look at it: http://en.wikipedia.org/wiki/Evaluation_strategy

Call by reference can be implemented in C. But C++ references as language construct that did not exist in C. In that sense, scanf uses call by reference, printf uses mostly call by value except for strings.

C++ references do not occupy memory and can not be re-associated after initialization. This supposedly allows better optimization. This is certainly true for references to local objects. C++ reference arguments in inline functions are similar in that way. C++ reference arguments in functions that are not inlined (either because they are not inline or because the compiler decides not to inline them) are unavoidably implemented as pointers under the hood.

The semantics remain different. For example, you can not extract the address of the hidden pointer behind the reference. Whether you use pointers or references, the compiler can optimize them both. The compiler does not have to allocate memory for any object unless you try to take its address and operate with that object through reference or pointer. So the difference is only measurable with less intelligent compilers. It is also a question of style.

Regards
Last edited on
Right, and my style is to make a difference in semantics visible in the line of code, not way up at the function's arguments. The code x++; has a wildly different meaning if x was passed in with a reference. However with a pointer you have: (*p)++; which makes the different semantics visible without looking back up to the top of the function. Also, why would you need a reference or constant pointer to a local variable?
Last edited on
Seraphimsan wrote:
when I say reference pointer I mean the pointer that points to the address that references the variable in question.


I think you're trying to describe a pointer. It's common to just call them "pointers", rather than "reference pointers".
No I mean references.

http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

I described it poorly.

int *p; is the pointer to the address in memory

int &p; is a reference to the value at that address.

Though I'm now doubting whether or not the have them in pure C.

edit: Nevermind, they do.
Last edited on
rocketboy9000 wrote:
Also, why would you need a reference or constant pointer to a local variable?
For purely syntactical reasons.
1
2
3
4
5
int v[3] = {0};
int &x = v[0];
int &y = v[1];
int &z = v[2];
inner_product(&v[0], ...);


"i++" and "++i" (built-in types) used to produce different machine code. I write "++i" for loop counters, because my teachers (understandably) used this form in preference, but I will not start correcting someone if they use "i++" instead (for built-in types). References and constant pointers are suspected in producing different machine code. But in reality, the difference gets less significant due to smarter compilers. The stylistic and syntactical aspects are going to be the only that remain. And those are important indeed.

Regards
Topic archived. No new replies allowed.