Why do pointers even exist?

Why do pointers even exist?

Attached: 1-main[1].png (562x522, 23K)

Other urls found in this thread:

stackoverflow.com/questions/162941/why-use-pointers
blog.erratasec.com/2015/03/x86-is-high-level-language.html
twitter.com/SFWRedditGifs

Attached: 1524739837002.png (862x444, 139K)

The pointers would leak into LISP and not the other way around, dummy.

pointers are one of the simplest abstractions/forms of indirection available to programmers. the abstractions found in the likes of java are really a thousand times more complex.

>why does your cursor exists
because gui's

this doesn't answer OP's question, show a simple example application of pointers

int main(int argc, char *argv[]) {
}
There we go. In fact, it's even showing pointers to pointers.

if you followed this logic through, you would be writing all of your code in binary

No it does't. Also, saying "code in binary" just goes to show you don't know what you're talking about.

Do you use OOP at all? Function pointers are mandatory.

Krakens aren't Greek though

All these brainlets not understanding it. Memory adresses exist on the actual machine, pointers are simply memory adresses. Even if you use a language without pointers, someone had to write a compiler for that language and he had to use pointers. Or maybe even the compiler was written in a language without pointers, but then that language used a comlpiler written wirth pointers, or the sandbox it runs in uses pointers.
At some point you have to bridge the gap between your abstract language and the machine, which uses memory adresses.

When I first came to C, I tried to understand this for awhile. The more you program in C, the more you'll get it. Mainly, the reason I used pointers is to pass data around a program so that it isn't automatically garbage collected.

Currently, I'm writing a program using GTK to make a GUI. I have a struct that is declared in the main() function, and it is passed around to each function that either builds widgets or writes values. This keeps the data from ever being automatically garbage collected by any of these functions, because it isn't garbage collected until the program is exited and the main loop is then done.

As far as I can tell, the main thing is keeping data from disappearing. There's also voodoo that can be done with void pointers and changing their types, but that's more complex stuff.

Stuff being popped off the stack is not "garbage collected".
You're using that term incorrectly.

>C
>garbage collection
LOLZ WHAT??????

>As far as I can tell, the main thing is keeping data from disappearing.
I'm getting cancer don't ever post about programming again

Now this is a subtle one.

Attached: 1338715934996.png (564x432, 174K)

weak bait
but effective enough

why would you modify the mem directly instead of modifying it under some sort of variable

Pointers aren't a problem, pointers are a solution.

How do we stop people from using pointers, webdevbros?

Invent a type of computer that doesn't use them.
Good fucking luck.

Not b8, just used the wrong term. pls no bully. I think you understand what I meant though, the data in a function disappears after it is no longer in context (not even sure context is the right word).

You're using a more restrictive form of pointers called references. Despite that you still have all problems of pointers.

same person, it's scope, not context, my bad

You always modify bytes at a certain adress. Even for stack variables there is an adress, it's only abstracted away. Look at the assembly, ESP is a memory adress.

I don't care if you used the wrong term you are still missing some important fundamentals and I advise you to read a book nigger

how else do you figure are recursive data types implemented?
>pointers are real

To build any data structure at all you either need pointers or algebraic types.

Because sometimes you don't know where a piece of data will be at compile time and because sometimes you don't want to copy the data to pass it to a function now back to /sqt/ you stupid fuck. Saged.

How Can Data Be Real If Our Pointers Aren't Real

>pointers are real
"therefore we should use them"?
what a retarded argument

Attached: 1540755168688.jpg (552x548, 43K)

I prefer practical experience over reading books, and I've read books before. I don't get what you think I fundamentally misunderstand about pointers.

not even any code in that program

Keeping stuff from being popped off the stack is not even close to being "the thing" for pointers and I don't understand how anyone could even come to this conclusion.
Even leaving any kind of dynamic memory allocation alone... how about using arrays, or modifying variables through functions???
Read this page and maybe you will reach the understanding of a high schooler. stackoverflow.com/questions/162941/why-use-pointers

>can literally show an example of using pointers in a program without the program having to contain any actual code
Garbage collectors BTFO

I mean....what exactly does this mean?

Is it a gross implication of the shittiness of modern high-level language compilers in terms of pointer management?

it's like the same argument could be applied to any language other than whatever assembly language applies to your development platform

Attached: 1541786698411.jpg (1024x681, 95K)

I now realize after re-reading my post why you think I misunderstand how pointers work. I was explaining how I use them for the most part, and I think it gives a good example of a use case for someone who doesn't understand why pointers are useful.
I have dynamic memory allocation in my current program, and I understand how this all works. I have code in the Linux kernel. Like I said, I probably should've worded it differently. It was supposed to be an example to someone who doesn't understand what a use case for pointers is.

It means that you have to use them in order to talk to hardware. You can have a more abstract language, that is absolutely fine, but some people still have to build the base for that and they will need pointers or something similar.

If you really just want to use the variable later, you can just make it global.

Global variables are generally frowned upon AFAIK, and I'm dynamically allocating the structures for this program, so it isn't exactly applicable. But, yeah, someone could just use global variables in the use case I've described. But most books try to teach you to stay away from them.

There is no reason why they shouldn't exist

You're riding high on mount stupid and you know nothing.

If you and OP took a basic systems course it'd be obvious what pointers are and why they exist.

fyi u can write assembly in binary, in fact asm languages are basically just syntactic sugar for straight binary

Passing a huge block of data without copying it, reinterpreting the data with a different context and accessing memory mapped hardware.

I don't know. Can you show me a linked list implementation that doesn't use pointers?

You're wrong, but it's not worth arguing with you. I've got nothing to prove. I was just trying to help OP understand a basic use case.

You have to use them to talk to the x86 processor that's implemented in software running on your CPU.
blog.erratasec.com/2015/03/x86-is-high-level-language.html

The way you describe it just seems super weird.
1. OP asked why we need pointers, your usecase is specific to a language that heavily relies on pointers, obviously you will use pointers in C, but that itself doesn't mean pointers are a good concept.
2. How does your design even work? You have a pointer to dynamic memory and you say you do it so it doesn't get deleted. Ok fine, but you still need the pointer itself (not the memory it points too) that pointer variable gets deleted after you leave the scope it was defined in. So if you already left the scope you must have copied it somehow or otherwise a stack variable would have done the same job. You oopose global variables, but the way you use your pointer it is almost a global variable.
You also don't think about pointer ownership or ever deleting that data, yes it will not crash the PC after you leave the program, but it looks like you will run into tons of problems with your current design philosophy when you write bigger programs.

>Somebody has to deal with them.
Yup, my non-autistic modern programming language.

Says the webdev standing on the shoulder of giants.

what is this fabled "modern programming language" that doesn't use pointers?

yeah but what they're saying is that all programmers should learn what pointers are and how to use them?

that's retarded, only the people who write the high-level languages should be using them because people trying to use them and not having experience will only end up with buggy and likely extremely insecure software

there's nothing wrong with stratifying software development, getting tired of this meme that everybody needs to know everything to be productive

>Calling machine code "binary"
You're retarded.

>Pointers are real
>Implying virtual memory addresses are real
>Implying the is any language not heavily abstracted from hardware.

Underrated post.

Personally I don't find pointers to be that complicated, but I don't think you have to understand them if you can write good high level code without them. There is always some level of abstraction where you just say it no longer concerns you, for example I can not design hardware. If you write JS or whatever and it works for you, that's fine.

Fine, flip the processor into real mode if you dislike virtual addresses. Or write a kernel extension and work with virtual addresses.

OP asked why do pointers even exist, and I gave a scenario where they would be used, maybe that doesn't justify their existence, but it makes sense to me why they exist in that scenario.
My current program is a sound card control panel for Linux. When the program starts, it searches through all ALSA cards on the system, finds the ones that match my use case (it's looking for Creative Labs cards), returns the number of cards and also modifies the variable in the structure passed to it giving the cards alsa number.
Then, with the number of cards found, the actual structures for the device specific context's are allocated, and the addresses of each allocated structure are assigned to a pointer in an array structure pointers.
This makes it easy to pass things around to functions like widget callbacks, where the individual structure can be passed around without having to have tons of variables in a function definition. C probably isn't the best language for making a GUI, but it's what I've chosen to work with, so I'm sticking to it.

>american education

Attached: 1517317619870.jpg (427x507, 44K)

Make me a collection in native C to store a potentially infinite amount of things without using pointers.

>Can you show me a linked list implementation that doesn't use pointers?

struct Node
{
int data;
struct Node next;
};

This won't compile.

Why would a computer be mad that every byte of RAM has an integer ID represented in hex?

yeah but OOPajeets use brainlet terms like "delegate" nowadays cause pointers are hard

Registers deal with the word size of the processor which also happens to be the size of a memory address for that architecture, therefore when dealing with register-level operations you need to be able to pass memory locations (i.e. pointers) around which refer to structures in memory like buffers.

why do you exist then

Not everyone has infinite RAM, user.

You don-t need to use pointers if you don't want to

>Pointers are *real*. They're what the hardware understands. Somebody has to deal with them.
This is what fizzbuzzing basement dwellers all believe, its why referring to C as a "high level assembly" is just as big a meme as "install gentoo" on Jow Forums. The actual fact is that in assembly language all variables are pointers in that they are all offsets to the start of the memory space of the binary that the assembly gets compiled into. What people who don't program in assembly are not aware of is that you never deal directly with any variables in assembly, you use labels and jump statements (similar to labels and goto statements in C). First of all if you are programming to an operating system then most likely you are using virtual memory so you cannot access hardware memory directly. And even if you are doing embedded programming directly to hardware then you have to trust the assembler to access hardware memory for you, you can't access actual hardware memory addresses. So all this talk about pointers giving direct access/control to CPU memory is just a big smokescreen to draw in brainlets into the C/C++ cult.

Yes it will, subhuman.

Which compiler did you use, brainlet?

Attached: ohmy.jpg (550x420, 41K)

No, the t*rks took them with constantinople.

error: field ‘next’ has incomplete type
struct Node next;

PHP got how references should work right, and that's pretty much the only thing I like about PHP.

>to be productive
you think I give a fuck about how "productive" you are? no. I'm pissed off because all the software running on my computer is garbage shit, and there are no better alternatives because of uneducated fucks who don't understand performance.

>>Pointers are real
>>Implying virtual memory addresses are real
>>Implying the is any language not heavily abstracted from hardware.

Your TLB is not architecturally visible state, nor are page tables from within usermode.
A program can't be "closer to the metal" than what bare machine code allows to be done. Unless your compiler can output to encrypted and signed processor-specific microcode, you're not getting any closer than assembler with processor-specific instructions (including MSR access, etc.), which C is reasonably close to.

Show me how to implement a vector without a pointer.

The question becomes: is there any valid reason for you to ever use something in a memory address which was allocated outside of your method's scope? In cases where the data alignment is pre-agreed to the answer could theoretically be "yes", but in the vast majority of cases there is no reason for pointers to be exposed to the programmer as when used properly they are merely references.

so people like you can corrupt memory, OP

webm related

Attached: kraaken.webm (508x452, 2.79M)

>when used properly they are merely references
References are just syntactic sugar for pointers.

Also, have you considered LITERALLY ANYTHING THAT USES HEAP MEMORY?

GOTO CONSIDERED HARMFUL

Pointers are the bases of dynamic data structures.
Without pointers you couldn't create a stack or a queue, there are languages that let you include these from libraries but someone at some point had to design them in that language.
Plus what if you wanted a very specific queue, maybe a first out but not always a first in, maybe it needs to be insert in the middle of the queue if the number is a certain size.

What languages need is references. Pointers are a form of reference that is: 1) convertible to and from an integer representation and 2) arbitrarily indexable. References are often also not nullable. A pointer is a weaker abstraction than a reference, so it functions more like the hardware, but provides fewer guarantees that the compiler and runtime (and programmer) can depend on.

>Pointers are a form of reference
Wrong. References are pointers. They are syntactic sugar for pointers.

They are semantically distinct abstractions for memory addresses. When you add a feature to one abstraction to make a new one, such as pointer math, nullability, or integer conversion, the new ADT is considered a subtype of the original. For example, nullable references are a type of reference. Pointers are too for the same reason.

Underrated

>semantically distinct abstractions
Forget the abstractions. At the level of assembly, they're both an integer value that you use as an argument to a load or store instruction. That is what a pointer is.

Clearly you meant assembly right retard-kun?

>Forget the abstractions
But then, as you say, we'd just be talking about integers. Pointers and references are both abstractions.

If I wanted to learn pointers I'd use something other than Rust.

"sometimes you gotta go fast"

Attached: LEDs.jpg (700x1094, 228K)

Garbage collection is the wrong term here, local variables disappear because they are removed from the stack once the function ends. Garbage collection is what languages like java and python use to keep track of what they store on the heap. Pointers, malloc(), and free() is how C lets you store things on the heap (which doesn't go out of scope until the program ends) but you have to keep track of these things yourself or else you'll have memory leaks. Overall you're right but you should learn more C so you understand the correct terminology and why things work the way they do.

That should be
struct Node {
int data;
Node next;
};

And while they may compile, you'll probably need to put it on the heap anyway because you're going to run out of stack space pretty quickly.

Are you trying to imply that people don't use pointers in assembly? Because if so I have news for you

>I don't know how superscalar architectures work and think my babby understanding of c equates to some "low level knowledge on how computers work"

This meme needs to stop, now.

Assuming that compiles, that is an array. It only appears to be a linked list superficially due to syntax quirks.

Just because you can go lower level, that does not mean that pointers are no longer required knowledge for a low level understanding of the architecture. Imagine trying to explain a virtual memory page to someone who doesn't understand pointers.

with the state of this board these days, I can not even tell if this is bait, good job.

Low level implementations that need to min/max the hardware optimization require the user to at least understand the dichotomy of both the physical and virtual states of programming

I like em.