Concept of step by reference and value


I know that the subject has already been commented and I have been checking in detail the contributions that have been presented during each one, thanks to them I think I have managed to really capture the concept but I still have doubts about whether I have done it correct way, I would appreciate it if someone could confirm my hypotheses outlined below.

Points to consider.

1.Java does not have steps by reference.

2.C and C ++ have steps by reference.

All the previous languages I have covered up to the necessary subjects to be able to realize this type of comparisons. In Java I can express the following line.

Persona p1 = new Persona();

While in C I can do the following with structures.

Persona p1;
Persona *ptrPersona = &p1;

From the previous examples I thought the following.


1.The reference p1 refers to the object created in that same line so that the variable p1 contains the memory address of that object.


2. The ptrPersona pointer points to the p1 object.

This is where I had a clash of concepts ... I thought that the fact that the C pointer pointed towards an object emphasized that the pointer was simply a type of variable that contained memory addresses but when thinking about that clashed with the concept of references in java and I had the doubt what was the difference between the reference in java and the pointer in C if both when going to a function really what was done was to copy that memory address to the parameter of the function.

From the above I tried to find an answer that really gave me a solution to this incognita and that I ended up confusing a bit.

Hypothetical Points that I now believe I understand

1. The pointer in C is not really a variable that contains memory addresses, it is simply an alias for the memory address it points to.

2. The reference in Java if you save a memory address.

Through the previous points, then try to link all the answers I found ... and arrive at the assumption that the step by reference does not exist in java and if in C for the following circumstances.

1.In C there is a step by reference because if you pass a pointer to a function you are not really telling the function parameter to copy the memory address that contains the first pointer, but you are telling it that now the variable parameter will be a new alias for the memory address that "pointed" to the first pointer or rather said to the one that was aliased by the first pointer.

2. In Java there is no step by reference because if you pass a reference to an object, the only thing that is happening is the memory address, this implies that indirectly the Java language must perform a hidden process to access to that memory address and that in addition to the memory cells there will be stored two references or different variables that contain an equal memory address of an object.

I would be grateful if you would confirm if this is really the difference that limits to languages like java not supporting the passage of parameters by reference to a function, also I would like to emphasize the point of the pointer in C ... Really a pointer in C is not like a variable that can contain a value (memory address) but it is something more advanced so it does not contain anything but it can be an alias for a memory address. The previous thing I mention because I have actually seen some authors mentions a pointer in C as a special variable that contains memory addresses.

asked by Sergio Cortez 31.12.2018 в 03:29

0 answers