Next Generation Emulation banner

1 - 7 of 7 Posts

·
-PM to advertise here-
Joined
·
8,262 Posts
Discussion Starter · #1 ·
I tried to search the web but I am kinda having difficulty in finding some "basic" difference between these two in layman terms.

POINTER:

int A;
int *pA;
pA = &A;

REFERENCE:

int A;
int &rA = A;

So what?

A is the name of the address of 2 bytes reserved in memory for integer type.
pA is the pointer which contains the address of A.
So what would rA be?
 

·
Registered
Joined
·
1,576 Posts
In C++, references are implicit pointers. Basically, when passing a reference, the receiving function has only a copy of the value in the address passed. The value in the original function cannot be manipulated by anything but its originating function. So it's like a "safer" but less versatile pointer. If you want a detailed read on when to use which, try this.

In C, there are only pointers.

In Java, there are only references.
 

·
Registered
Joined
·
1,577 Posts
A reference is the address at which a variable is located in memory. A pointer is used to store this reference.

Code:
int a = 5;         /* store 5 in int a */
int *ptra = &a;    /* store reference to a in pointer to int ptra
int b = *ptra;     /* dereference pointer a, store in int b */
printf("%d", b);
This example code would output "5".

The obvious advantage of passing variables by reference, rather than by value, is that only one word of data is passed, rather than a, possibly long, piece of data.
 

·
-PM to advertise here-
Joined
·
8,262 Posts
Discussion Starter · #4 ·
klatch said:
In C++, references are implicit pointers. Basically, when passing a reference, the receiving function has only a copy of the value in the address passed. The value in the original function cannot be manipulated by anything but its originating function. So it's like a "safer" but less versatile pointer. If you want a detailed read on when to use which, try this.

In C, there are only pointers.

In Java, there are only references.
You said that passing through referance leaves the called function with only a copy of the value stored at the addressed referred.

Is it not the same same as passing the value?

I mean:

void myfunc(int &I)
{
I = I +1;
}

void main(void)
{
int A=2;
myfunc(A);
printf("%d",A);
}

What'll happen? vbmenu_register("postmenu_836894", true);
 

·
Emu author
Joined
·
1,488 Posts
Passing by reference doesn't pass a copy of the variable, it passes a reference to the variable. As you clearly realized this means that changing the value in the called function will result in it being changed in the caller's variable space. In your example this would print 3..

References are "safe" pointers. They must always refer to either a valid object or explicitely nothing (the exception to this exists in a language that allows or requires explicit deallocation of memory, in which case what a reference refers to may become invalid. In such a case it's more correct to say that a reference can only be explicitely bound to something that exists at the time or to nothing at all, such as null in Java). In C you can use pointers as references but that requires pointer notation to dereference them. This can get pretty ugly, but occasionally it's nice to be very aware of exactly what layers of abstraction you're jumping through.

Pointers in C can point to any numeric memory location in the program's address space. You can't directly set it this way (without a cast), so even pointer arithmetic should retain alignment, but just the same there's no mechanism in C to stop you from incrementing a pointer into a region where no such object exists.

Pointers aren't necessary in just about any application that's sufficiently abstracted from the machine and OS kernel, and in those cases where they are you possibly have to coerce the language to allow for it (casting in C/C++). References make certain optimizations and garbage colllection troublesome but pointers make these just about impossible.

In general, references > pointers

- Exo
 

·
It’s Me .. I still Exist
Joined
·
1,224 Posts
Syed Fawad said:
What'll happen? vbmenu_register("postmenu_836894", true);
Sorry unrelated, but what did you put at the end of your post to make that vBulletin bug happen?
 

·
-PM to advertise here-
Joined
·
8,262 Posts
Discussion Starter · #7 ·
Exo cleared up the things I had in my mind after some more searches on google ...and then he went deeper then my light can reach :)
Thanks a lot buddy!

Keith, I have seriously no idea what caused that. I just completed my type and pressed the post button like I have done some thousand times :p
But this is the first time that sprang up....something to do with me posting after a LONG time?

Moreoever, I'd assume that this thread has fulfilled its purpose .... and I have copy pasted Exo's post for any reference needed later so any one may close up this thread. If someone would like to tell me more stuff related to this topic then a very thankfully welcomed PM may be used.

Over 'n out,
Fawad.
 
1 - 7 of 7 Posts
Top