Friday, January 11, 2008

Pointers F*CKING FINALLY

I was taking a crap about ten minutes ago and was reading a C Programming book, midway thru I figured out why you would want to pass a reference to a function(pointer) versus just passing a value(variable). This distinction is lost in most higher level languages that use garbage collection, because they dont have pointers.
For the longest time I was looking at a C++ Programming book trying to understand pointers, and just was not getting it. The basic mechanics I got, but the need for pointers(outside of passing arrays to functions) I just did not get, neither did I get the whole thing with the passing by reference versus value.
SIDE BAR: For the non programmers out there the difference is that when you pass something to a function by value, a copy is made to the stack(could possibly be a issue if it runs you out of memory). The original value is unchanged, you can do what you want inside the function but will not change the value that you passed into the function.
Passing by reference gets you alittle closer to the memory and you are dealing with the memory locations and you can actually change the values that way, it is also the only way to pass an array to a function.
I dont know if it was just explained better in the C book or what but I am finally starting to get the whole passing by reference versus passing by value and that was really holding me up on the whole C/C++ understanding thing. I really want to get a good hold on C because I want to start experimenting with creating python libraries in C for linked lists and different data structures(I want to grok B-trees and more advanced trees in the first part of this year).