okay - i've scoured the internet for an explanation i understand. what are pointers, why would i use them and is there anything else i need to know?
Have a look at this: http://www.cprogramming.com/tutorial/lesson6.htmlsatans_banjo said:okay - i've scoured the internet for an explanation i understand. what are pointers, why would i use them and is there anything else i need to know?
Try not to think about pointers as anything special, don't let the &blabla and *blabla syntax confuse you.satans_banjo said:okay - i've scoured the internet for an explanation i understand. what are pointers, why would i use them and is there anything else i need to know?
satans_banjo said:yeah ive already looked at that. i'm wondering why anyone would use pointers? have you got any examples of applications for pointers (by applications i dont mean programs, i mean practical uses)
int variable; //declares a variable
variable = 4; //declares the value of the variable
int *pointer; //declares the pointer
pointer = &variable //assigns the value of the pointer to the memory address of the variable
*pointer = 5; // gets the value of variable (pointed to by the pointer) and changes it to 5
Yes, exactly.satans_banjo said:so if i declare a pointer to an int, then printf the pointer, would that return the memory address of the int?
satans_banjo said:so if i declare a pointer to an int, then printf the pointer, would that return the memory address of the int?
int a = 10;
int *b;
#include <stdio.h>
#include <stdlib.h>
main() {
int a = 10;
int *b;
b = (int *)malloc(sizeof(int)); // allocate memory for int pointed to by b
*b = 20; // set value of int pointed to by b
printf("value of 'a': %d\n", a);
printf("value of 'b': %d\n", b);
printf("value of '*b': %d\n", *b);
printf("value of '&a': %d\n", &a);
printf("value of '&b': %d\n", &b);
}
value of 'a': 10
value of 'b': 5243120
value of '*b': 20
value of '&a': -1073745000
value of '&b': -1073744996
Yes, as long as *pointer was declared as int *pointer.satans_banjo said:so i've gathered this so far:
Code:*pointer = 5; // gets the value of variable (pointed to by the pointer) and changes it to 5
satans_banjo said:EDIT: one last question: out of curiosity, do any developers target a particular memory address for a specific variable? for example, would they change the value of &variable to make the program run more predictably?
satans_banjo said:EDIT: one last question: out of curiosity, do any developers target a particular memory address for a specific variable?
Its like placing a link in a mail message instead of including the whole web-page in the mail, you can click it and go if you want, and the mail is smaller and faster to download.
Fukui said:Its like placing a link in a mail message instead of including the whole web-page in the mail, you can click it and go if you want, and the mail is smaller and faster to download.
Yea, C# and Java pass by reference by default don't they.(I have limited exp.)mrichmon said:Also, different languages handle passing arguments differently. Some languages default to passing a copy and you need to use a different syntax to pass a reference. Other languages only pass references so in this case you need to do some extra work as the programmer if you want pass by copy semantics.
Fukui said:...Java pass by reference by default don't they...
Fukui said:Yea, C# and Java pass by reference by default don't they.(I have limited exp.)
Do you know any OO languages that pass by copy by default?
Wow, thats a lot of info.mrichmon said:Arguably C++ is pass by value as a default. But that is a bit misleading.
Java as you say is pass by reference for objects but pass by value for atomic types, except in the case of using Remote Method Invocation. Using RMI the semantics for passing objects and atomic types are pass by value.
C# is pure pass by reference.
Python is also pass by reference.
Ruby only allows pass by reference.
PHP uses pass by value.
Lisp when using CLOS I believe uses pass by value semantics if my memory serves.
Smalltalk uses pass by reference semantics.
Oberon, like Pascal has explicit pass by value and pass by reference syntax.
Object COBOL (yes such evil exists in the world) allows pass by reference, pass by value and pass by content semantics. Pass by content means that a pointer to a copy of the data item is passed. How's that for twisted?
Fukui said:Wow, thats a lot of info.
Hmm, most pass by reference, thats pretty interesting...
Its interesting, I wonder if it would even be possible to implement a programming language and runtime in say C# that implements C#... IOW programming in C, one could make another C runtime, C compiler or other languages, so the C runtime is implemented in C, but could a Java runtime or C# runtime be implemented in those languages? If they hide pointers? Isnt it a kind of weakness?mrichmon said:Under the covers a reference is often implemented using a memory address but does not have to be implemented that way. The key thing is that a programmer should not be able to modify a reference to get access to another object. But a programmer can for example increment a pointer to access the next memory block.
Fukui said:Its interesting, I wonder if it would even be possible to implement a programming language and runtime in say C# that implements C#... IOW programming in C, one could make another C runtime, C compiler or other languages, so the C runtime is implemented in C, but could a Java runtime or C# runtime be implemented in those languages? If they hide pointers? Isnt it a kind of weakness?
I'm not sure there's actually a reason to get rid of pointers, though it sounds nice at first... since the basic design of every computer is to use a buffer of memory why hide it?
Right, but I wonder there couldn't be a kind of compromise, instead of "Must Garbage Collect" or "Must Hide Pointers" is to provide a layered approach, like a base implemenation using functions, pointers, and no collection or bounds checking, then based on that build on an object layer, then add collection etc, then there wouldn't be any translation "layers" like JNI or the C# bridge...mrichmon said:In some ways the omission of pointers from a language could be seen as a weakness. But what is really happening in these languages is that the language designer is removing the need for a programmer to explicitly manage memory allocation. Memory allocation and pointer manipulation is one of the biggest sources of bugs, complexity and inefficiency in code.
I didn't know garbage collectors manipulate pointers... I though they just keep references to the memory (pointer) and the null it once it had no references... but then again I guess thats why they hide the pointers, thats how they count references! I thought instead the runtime would check if the code had pointers to the location of an object or struct, then if all the pointers were nulled to that particular location, then it would be freed...If you have automatic memory collection, specifically garbage collection, then it is generally not possible to provide pointers in a language. A reason for this is that the garbage collector moves objects around in memory. With references, these object movements can be transparent since the reference does not refer to a specific location in memory. Pointers however refer to a specific location in memory so will break in the context of a garbage collector.