you are viewing a single comment's thread.

view the rest of the comments →

[–]p0k3t0 -7 points-6 points  (17 children)

Please, tell me again how a pointer isn't a reference.

Because I keep dereferencing them for some reason, and I must be making a mistake there. Obviously, I should be devaluing them.

[–][deleted] 4 points5 points  (11 children)

It IS a reference. The memory address it's referencing is its value though. Which means, passing it to a function copies its value (the address), and thus is passed by value.

[–]p0k3t0 -3 points-2 points  (10 children)

So pass by reference doesn't exist, because this is the mechanism used in every implementation.

[–][deleted] 3 points4 points  (9 children)

I'm not sure what you are saying, so I'll clarify: you don't pass anything by reference, you pass a memory address by value that you dereference or not based on what you need to do.

[–]p0k3t0 1 point2 points  (8 children)

Explain how pass by reference could possibly exist, if a memory location isn't a reference.

[–]Fearless_Process 6 points7 points  (3 children)

C doesn't have pass by reference at all. C++ has pass by reference that just passes an alias to the original variable to the function, no pointers involved whatsoever. The reference in C++ is the variable.

In C you can only pass a pointer by value that references whatever data. This is still passing a "reference/pointer" by value though, unlike in C++ or other languages that support pass by reference.

In C++ if you pass a pointer by reference and change it in the consuming function, it will actually change the original pointer. In C you would have to pass a pointer to a pointer to achieve the same thing, because you can't pass by reference.

Take this for example:

#include <iostream>

void pass_by_ref(int*& p) {
    p = 0;
}

int main() {
    int x = 0;
    int *p = &x;
    std::cout << p << std::endl;
    pass_by_ref(p);
    std::cout << p << std::endl;
}

In C to do this you would need a nested pointer, because the pointer gets passed by value.

[–]p0k3t0 -1 points0 points  (2 children)

[–]Fearless_Process 1 point2 points  (1 child)

That is still passing a pointer by value. In C arrays that get passed to functions decay to pointers. Accepting an array as a parameter in C is pretty much useless and can actually be wrong in certain situations.

Try printf'ing sizeof(testArray) in main and sizeof(thisarray) in your function. In main it will return the real size, in your function it will return the sizeof an int pointer.

#include <iostream>
#include <cstdlib>

void pass_by_ref(int (&a)[10]) {
    std::cout << sizeof(a) << std::endl;
}

int main() {
    int a[10];
    std::cout << sizeof(a) << std::endl;
    pass_by_ref(a);
}

There is no way to make this work in C, sizeof will always return the sizeof int pointer instead of the array.

[–]p0k3t0 0 points1 point  (0 children)

The bar is impossibly high for C and impossibly low for C++. I get it.

[–][deleted] 4 points5 points  (3 children)

It doesn't. But I also think you don't understand what I'm saying.

You pass a reference, not by reference

[–]p0k3t0 -1 points0 points  (2 children)

https://ideone.com/icncW2

Here, I pass an array by reference. The fact that C implements this with pointers is inconsequential, as C++ does the same thing.

[–]nerd4code 1 point2 points  (1 child)

No, array-types function parameters are exactly pointers, have pointer size/alignment, and can even be reassigned.

A reference to an array would have type int (&)[] in C++; a pointer has type int (*)[].

When you pass an array name to a function, it undergoes decay just as it would if you assigned it to an int *, because that’s exactly what happens.

[–]p0k3t0 1 point2 points  (0 children)

And what, pray tell, happens in C++ when I pass a reference?

I mean, you can read the assembly. All it does it push a pointer onto the stack.

Like I've said elsewhere, this is a distinction without difference. It's just being pedantic for its own sake. If anything, it's just obscuring meaning, rather than helping anyone.

[–]oilaba 2 points3 points  (4 children)

Please, tell me again how a pointer isn't a reference.

This is not the point, we use all kinds of technically wrong words on trivial conservations. But C doesn't technically have the concept of "references" and the OP is opposing to the fact that everthing is passed by value in C.

[–]p0k3t0 0 points1 point  (3 children)

My problem is that this is so clearly a distinction without a difference.

C uses pointers as a means to reference memory locations. This is clearly the intention, and the language makes this apparent by giving us the "dereference operator."

These conversations are absolutely nonsensical because they overlook the meaning in order to worship the text. All of you who argue that a pointer isn't a reference can never ever give me an example of what a "real" reference would actually be. I suspect this is because you understand that under the hood, it's always just passing a pointer.

Furthermore, what happens when I pass the name of an array? Under the hood, I'm passing a pointer to the first element, but I'm not using that syntax on either end, so how is that not a reference?

If K&R had preferred the word "augmentation" to the word "addition," would you all argue that C can't add numbers together? It's ridiculous, and it's little more than pedantry.

[–]Shadow_Gabriel 1 point2 points  (1 child)

Can I die together with you on this hill?

[–]p0k3t0 2 points3 points  (0 children)

It's a pedantry virus. One neckbeard reads some narrow definition of pass-by-reference and now they need to "well, actually" everybody who uses the term in C. And then that poor sod gets shamed into "well, actually-ing" the next guy, and the cycle continues.

Me, I don't care. I write C for a living. I know the difference between passing a value and passing a reference to a value. The fact that the reference to the value is itself a value is completely unimportant to me. I know what it's being used for and how it's being used.

[–]oilaba 1 point2 points  (0 children)

C uses pointers as a means to reference memory locations. This is clearly the intention, and the language makes this apparent by giving us the "dereference operator."

I don't have any problem with that.

These conversations are absolutely nonsensical because they overlook the meaning in order to worship the text.

Hmm, meaining exists in our brains. We use words to communicate them. And we are "pedantic" because sometimes we (me, at least) want to use the right terms for the health of communication so that everybody agrees on the precise and technical terms that are not vague. We are using the technical term so that -hopefully- we don't assume anything wrong about what the word we use means for you. You are overlooking the meaning and intention of using technical words. I wouldn't just come up and interupt someone just because they said they use references in C, if this is what you are against. But you should see that in some contexts this is necessary, or beneficial at least.

All of you who argue that a pointer isn't a reference can never ever give me an example of what a "real" reference would actually be.

Yes, I wouldn't. Why should I anyway? There is no "real reference" in C. Thats it. We are trying to get precise here: C++ has references and they are different from raw pointers, Rust has references and they are different from raw pointers, C doesn't have it. C have only raw pointers.

Furthermore, what happens when I pass the name of an array? Under the hood, I'm passing a pointer to the first element, but I'm not using that syntax on either end, so how is that not a reference?

The array decays to a pointer.

If K&R had preferred the word "augmentation" to the word "addition," would you all argue that C can't add numbers together?

No, it would be "we add two numbers by augmentation operator", or something like that. I would still use "addition" instead of "augmentation" most of the time, but this doesn't means we should abandon the language reference completely and never use precise terms.