What the FUCK is a pointer

what the FUCK is a pointer

Attached: 6c783155b49eced982969c437b5d2746.jpg (600x484, 24K)

Other urls found in this thread:

en.wikipedia.org/wiki/Protection_ring
twitter.com/NSFWRedditImage

It's a programming construct that allows you to refer to and utilize a data structure stored in memory.

have a good day, sir

Attached: 1524487513293.jpg (334x334, 88K)

you too fren, good luck on your programming journey

>A variable containing the address of another variable is called a pointer. Pointers are a great tool
for manipulating arrays and data structures, and they make dynamic memory allocation possible.

a pointer is a number that tells you where something is
everything has a place, and places have everything

Something that points to something else.

so it's basically like a symlink

This

>read C++ code
>cluster fuck of * $ & and other mindfucking symbols that manipulate memory

Attached: 1399817822500.jpg (495x480, 49K)

>read a series of X records from a file into a vector of person objects
What did they mean by this?

Attached: 1510770303940.jpg (800x729, 48K)

Pointer is a medium to large-sized breed of a Mastiff, developed in England as a gun dog. Pointers are even-tempered, congenial dogs, and despite their large size, make good house pets so long as they get sufficient exercise due to their extremely high energy levels. Pointers are intelligent, affectionate, clean and intensely loyal. Their aggression level is very low to non-existent and they normally happily coexist with other dogs and cats. They are not typically territorial and can be reserved with strangers. They are very good with children and fit in well with family life generally. Pups can be somewhat boisterous and their long legs as they grow, make them appear somewhat clumsy in a charming way. They will bark at suspicious noises, but are not a great watchdog breed.

chk

You can think of a pointer as a variable, in some level of abstraction. It is just accessible via different syntax, as it stores memory location, you have to use * to use what is stored there. Remember, this is on certain level of abstraction, but it will maybe be easier to understand. When you understand why is dynamic allocation useful, than you will get why pointers are used.

Attached: zidov.png (1600x900, 65K)

A variable containing a memory address, hopefully containing another variable.

>read C++ code
>clusterfuck of templating, operator overloading, and mixed reference/pointer types

Attached: 1488949632086.png (637x1430, 554K)

mah mans

Attached: 461075436281c09b67b0306b007b4e53.png (600x600, 197K)

>>read C++ code
>>clusterfuck of templating, operator overloading, and mixed reference/pointer types
You should avoid direct pointer use as much as possible because it quickly get really bug prone, use smart pointers and stl containers instead

what code are you reading that has $ in it? as far as I can remember that isn't even reserved and you can put it in variable names if you really were a fag

if you make one-way linked list in C and understand it, then you will understand pointers as well.

it's when you use your finger to point at the screen to explain what you did in your code to someone.

I don't know either but from an electronics point of view I think it's like some parallel eprom circuits which couple one or more output data pins to the address pins. I've seen it used to let 4 position rotary switches emulate the 40 position type. I wonder what an assembly language implementation would be like.

Imagine if the universe could be folded over onto itself and then rolled into a long tube, the tube needed to hold such a large mass would be billions if not trillions of parsecs long, now imagine there's a street address for this hypothetical universe tube - there you have it, a pointer is a type of street address

>inb4 whats a memory address

Pointers are A-cup women that are aroused.

It's a pointy structure stored in memory.

Attached: CopyQ.jd2119.png (620x465, 133K)

What's the difference between pointer and linked list?

Attached: 1522583752430.jpg (500x479, 27K)

What is it?
Please, use technical terms.

the crash

You need a pointer for another function to be able to change the data within a variable

A variable storing a memory adress,

are you actually asking that?
or just joking like OP?

Can you watch your language? Thanks.
Love, Anonymous

I don't know why pointers are so confusing to people. It's essentially just a special type of integer which represents the location in memory of a variable. The reason they are used is because it is faster to copy an integer representing an address than it is to copy an entire class/struct. Also, sometimes you want a function to make changes directly to an object, in which case, it needs to know the address in memory of the variable to do that.

>tfw I only ever use raw pointers and Valgrind reports show that I consistently manage to avoid leaking memory.
Also, STL containers don't really have anything to do with this. They are not an alternative to pointers. I stuff pointers in STL containers all the time.

A linked list is a data structure which contains a pointer to a data structure of it's own type.

Go to the post office.
See those mailboxes and those people putting things inside? Mailbox numbers are pointers, the stuff inside are dereferenced values.

I stand firm that C++ is a write only language. Yes it's easy to implement something, but it's not easy to understand it without documentation or a lot of effort. And nobody wants to do either of those thing.

>whole up while I just redefine/overload this common function/operator
>I'll just pass in this regular pointer and return a safe pointer and cast it to this other thing that's of questionable safety and responsibility

zuz

Attached: mio cute.jpg (800x800, 247K)

OK REAL TALK HERE.

can I even get a "real" pointer while writing from inside an operating system?

I know processes on *nix have their private memory that the kernel then maps into real memory. Can I somehow get past that and fuck with real memory?
Im sick and tired of the kernel memory management telling me what to do, i can handle myself

>hurr it points
such an awful explanation for beginners

just experiment with them, OP.

it's built into hardware. Your process id (as the hardware sees it) would need to be flagged as machine state (aka OS).
you'd need to trap to yourself, which is basically not a thing.

Relevant
en.wikipedia.org/wiki/Protection_ring

It points. There's nothing more to it, that's kind of the problem. It's literally a sequence of bytes that is actually another memory address rather than data or an instruction.

Why would you use a pointer when you can just reference a variable directly?

Example: Why use a pointer to an address of an element of an array, when you can just reference the element itself?

My thinking leads me to believe its just because if you're using C, memory comes at a premium or something.


pls expand

Someone else described it as a house address.
When you mail someone a package, you send it to their address, you don't mail them a copy of their house with the package included.

Attached: C pointers.png (249x117, 4K)

you can allocate heap memory

the answers are somewhere in this thread
it's always good to read other posts before posting

>why would you use a pointer when you can just reference a variable directly
A reference is just a pointer which cannot be null. You use a pointer when you want to be able to set it to nullptr.

Wrong.

>Why use a pointer to an address of an element of an array
Ready to have your mind blown. Array indexes are syntactic sugar for pointer arithmetic.
array[2] is the equivalent of array(the start address) + sizeof(arrayElement) + 2.

Also in a lot of cases it's for defining your own scope, and being efficient about it.

If you wanted to access an array element in seperate functions, what do you do? Pass copies of a whole giant 2GB struct around, or do you pass around a 64bit address referencing it.

Do you want to point to the entire struct, or elements of it?

>hopefully
use a better language

yeah my bad guys.

my guess is the best answer is Perhaps I'm missing something. Could you give an example of using pointer compared to using a variable so I can see the difference?

I get what pointers are I think, I just don't entirely get why you would use them.

>array[2] is the equivalent of array(the start address) + sizeof(arrayElement) + 2.
The sizeof part is wrong, but if you remove at it's true. Incrementing a pointer by 1 automatically offsets the appropriate number of bytes.

non autistic definition: it's a variable storing a memory address

I believe user meant to say sizeof(arrayElement) * 2

Actually I think yall got this for me. Thank you friends. So its for referencing a single part of a struct, rather than the entire struct? You largely do not edit whatever is data at the address the pointer is referencing correct?


**side stupid question: wouldn't the data at the memory address change upon execution? like every time you run it, you get new addresses? I suppose that isn't really relevant because its all self contained in the program.

That's still wrong. Like I said, incrementing a pointer by 1 automatically offsets the appropriate number of bytes. If you are using sizeof at all you are doing it wrong.

var fuckhugeArray = ...
element = &fuckhugeArray[x]
someFunction(element)

In someFunction, it can re-assign an element in the entire array without having to copy the entire array itself, then return that copy.
The alternative would look like this
fuckhugeArray = someFunction(fuckhugeArray)

Except that array are by reference anyway but hopefully you get the point.


>he doesn't work with poorly defined, arbitrary pools of memory with dynamic data
jej

>incrementing a pointer by 1 automatically offsets the appropriate number of bytes
it shouldn't
1 is 1.

void swap(int *a, int *b){
int c;
c = *a;
*a = *b;
*b = c;
}
so now the effects will last outside of scope

I do now! I get it!

Thank you so much user you're the best! I've been trying to wrap my head around this and it finally clicked.

Attached: 1401676487468.gif (256x256, 27K)

and thank you too, don't think I missed your efforts

Attached: 1439067892759.gif (500x500, 1.03M)

>So its for referencing a single part of a struct, rather than the entire struct?
That's an application of it, ultimately it's for pointing to any arbitrary space in memory.

>You largely do not edit whatever is data at the address the pointer is referencing correct?
It depends on what you're doing with it, you can use it for read or write access, or re-assign the pointer itself to point to something else, like another element, member, variable, whatever.

>wouldn't the data at the memory address change upon execution?
It shouldn't matter unless you're using static addresses, which people usually use fixed offsets for.
Everything is done at runtime, and handled by the runtime. For instance, malloc is called at runtime and gives you an address then, you use that address, relatively.
i.e.
array[0], first element, array[1] second element but really it means the beginning address + enough bytes to skip over the first element.

dumb frogposter

There is a lot of use case for pointers.

One of them is data sharing:
Let's say you have your music collection represented as an array of Music objects. Now you want to create a "playlist" array.
To do so, you could copy some Music objects in the playlist array. Or your playlist array could be an array of pointers referencing the Music of your collection.
In this scenario, using a pointer has several advantages:
- there is no need to copy a whole Music object to add it to a playlist
- Without pointer, if you were to modify the Music object (by example changing its name), you would have to modify both the origin object and the one present in each playlist. By using pointers, the playlists items has no need to be updated (since playlists doesn't directly store the data).

> **side stupid question: wouldn't the data at the memory address change upon execution? like every time you run it, you get new addresses?
> I suppose that isn't really relevant because its all self contained in the program.

Exactly. Addresses are random, but usually, we never use absolute addresses. There is 3 ways to get these addresses:
- Arrays variable are already an address, and can be used a such.
- The '&' operator gives you the address of any variable.
- Allocators ('new' in C++, 'malloc' in C) gives you an address to use.

You can try this example to create a variable and print its address: it should changes at each execution.
int main() {
char foo = 10;
printf("%p\n", &foo);
}

> hey computer, i need that XYZ shit
> it's over there (points at some memory address)
> you go over there, and there's your shit

how hard can it be to understand?

Tell that to the people who wrote the standard. Tell that to every programmer ever who has used pointer arithmetic to iterate through the contents of an array.

> 1 is 1.
Yeah it is.
But for a pointer of int, is 1 an increment of 1 byte ?
Or an increment of 1 int ?

isn't that a variable? what's the difference?

that thing in your pants

this T B Q H Q B T

Attached: 1419391357308.jpg (596x285, 32K)

It's a variable that stores the starting memory address of another variable. Dereferencing is just pulling up the data at that address. Passing pointers around is convenient because you're only passing around a word instead of potentially a whole ton of data (like a really long array). Using pointers also means whatever you pass the pointer to can manipulate the data directly. This can be useful for performance, but I tend to prefer copying memory around the edges of an API for security.

this

who the fuck needs 0.000000001 bit of memory to use a pointer instead of just another normal variable

The long metal stick your prof uses to point at the blackboard and sometimes spanks your little booty