Programming Megathread

Author Topic: Programming Megathread  (Read 105470 times)

a pointer is a thing that just points to a value in the programs memory referenced usually via addresses

if you wanna get technical a pointer is a variable that may or may not contain a memory address

if you wanna get technical a pointer is a variable that may or may not contain a memory address
well if you wanna get technical you're a mother frickin NERD

Does a pointer truly exist until you dereference it? Are we all pointers? Who points to the pointers?

well if you wanna get technical you're a mother frickin NERD

technically speaking, suck my richard

technically speaking, suck my richard
technically speaking it's a nullptr

i'm not well versed on pointers or have much experience with anything outside of scripting languages for that matter so could someone explain why use pointers in the first place
like why do book *x = y when you can just do book x = y

Usually it's to save memory. If you pass a normal variable into a function and it returns a changed value that you just want to update your variable with, you end up copying your data into three different memory addresses, because each new instance of a variable is basically a copy. If instead you used a pointer, you declare your value once and then pass the pointer into the function where the function is then able to access and manipulatethat value directly rather than needing to hand around a bunch of copies of it.

On mobile, sorry if my explanation is kinda roundabout.

Well it has a lot of uses. First off, it's faster and more efficient because you don't have to constantly copy and allocate more memory to keep all the data in, which is especially useful when you have pieces of data that are gigantic and take up multiple megabytes of memory.

Second, it allows you to have a function that edits variables in the caller. Let's say you had one function like this (This is just generic pseudocode that uses C-style pointer syntax):

function initializeNumbers()
{
    Number *a, *b;
    *a = 5;
    *b = 8;
}

Then how would you go about adding these numbers together and put the result in A?
Well, you could do this (Using the * operator to de-reference):

*a = *a + *b;

But if instead of adding you're doing some much more complex operation, you might want to break it up into different functions. So instead, you could do this:
function add(Number* a, Number* b)
{
    *a = *a + *b;
}

function initializeNumbers()
{
    Number *a, *b;
    *a = 5;
    *b = 8;

    add(a, b);
    printLine(a, b);
}

to print out 13 and 8.

Third, it lets you represent the absence of data. If you have a non-pointer object, it could consist entirely of zeros, but those zeros do not indicate an absence of data, the data IS zeros.
If you want to represent an absence of data, you just have to set the pointer to null.
« Last Edit: April 19, 2017, 03:50:54 PM by Ipquarx »

You can also get pointers to functions which is extremely useful for making generic data structures and algorithms.

void func1( void )
{
    printf( "func1 called" );    
}

void func2( void )
{
    printf( "func2 called" );
}

void call( void(*func)( void ) )
{
    func();
}

void main( void )
{
    call( &func1 );
    call( &func2 );
}
« Last Edit: April 19, 2017, 04:05:05 PM by SUSHI »


I once used two function pointers to get the size of a function. Very dangerous but possible on certain systems and compiler outputs. I think MSbuild pads around procedures/functions to mitigate againt ROP attacks.

Does a pointer truly exist until you dereference it? Are we all pointers? Who points to the pointers?

of course not. Because u dereferenced it :\

of course not. Because u dereferenced it :\
this is why Brazilians deserve to be nuked

and here i am, trying to figure out how programming languages even... work.

I tried starting with Javascript, failed.
Tried starting with C++, forgetin' failed.
I've tried starting with TorqueScript like 4 times before and failed,
and this time, I'm starting to get what's going on, but like half the stuff doesn't make sense.

How does this exist?
 where did it come from?

these two questions seem to hold me back since If I don't know where the variables are coming from (just to give an example.)
Then how the hell am I supposed to know where to get other variables should I need them?

and that's just the beginning.
I strongly recommend Java.  Its syntax and implementation is probably the most basic while also the most universal, not to mention Java is very friendly and will handle lots of things for you off the get go.

Consider resources like online classes/tutorials and/or books as well.  Those will get you in the right direction and serve as a guide.

I like java and C#. but python and javascript will always be my favs. lua is also neat. PHP sucks ass
technically speaking, suck my richard
my greatest legacy

I like java and C#.
ive never seen someone in the middle about java and C# before