← Home ← Back to /g/

Thread 105711325

56 posts 18 images /g/
Anonymous No.105711325 >>105711346 >>105711348 >>105711364 >>105711394 >>105711466 >>105711627 >>105711842 >>105712278 >>105713350 >>105713380 >>105713616 >>105713648 >>105714817 >>105714827 >>105714829 >>105715582
WHY ARE THERE POINTERS TO POINTERS
Anonymous No.105711346
>>105711325 (OP)
And?
Anonymous No.105711348
>>105711325 (OP)
what if you have to point to a pointer
Anonymous No.105711364 >>105711417
>>105711325 (OP)
Anonymous No.105711394
>>105711325 (OP)
To modify or return pointers aka objects. Also multidimensional arrays, linked lists, trees, et al
Anonymous No.105711417
>>105711364
More trannime explanations of code please
Anonymous No.105711426
write access to the write access
Anonymous No.105711466 >>105711589 >>105713490 >>105715147
>>105711325 (OP)
For 2D arrays for example
You have an array of ints pointed by int*
something like [1,2,3] and then array of int* pointed to by int**

int**=> [int*=>[1,2,3],
int*=>[4,5,6],
int*=>[7,8,9]]
Anonymous No.105711589 >>105711692 >>105715204
>>105711466
why not just int a[3][3]?
Anonymous No.105711627
>>105711325 (OP)
Data is data, anon. Memory address is just a number, you can point to any memory regardless of what it holds. You can make a pointer point at itself if you wanna.
Anonymous No.105711666 >>105711718
Your computer has little boxes inside it.
All the boxes have funny names like 0xea37168f43ec58bf. But that's a hard to remember computer name. That's called the address. It's like a mail address for the little boxes in your computer!
When you make a program, you write easy to remember names on the boxes. That's called declaring a variable. When you label the box like that, you can put things in the box and get them out easily because you remember you put them in the box labeled whatever.
You can put all sorts of things in the boxes, like numbers and letters. You can even put in the address of other boxes. A box that contains the address of another box is called a pointer.
You might not remember the address of the boxes you wrote labels on (variables), but you can ask the computer to tell you it. If you've got a box called a, you can ask for the address with &a. Then you can put that address inside one of your pointer boxes.
Maybe you've got an address written in one of your pointer boxes, but you want to find out what's in the box written on the label. If you have a pointer box called p, you can ask the computer to get you what's inside the box that's being pointed to with *p. That's called dereferencing a pointer.
Pointers can point to any kind of box, even other pointer boxes. You can even put in a fake address of a box that doesn't exist, or no address at all (null pointer). But you better be careful if you try to dereference one of those, because the computer will get angry and crash.
Most boxes only contain one value, but with pointers, you can do some fun things like point to the start of an array of boxes. That means you could store lots of data in boxes that are right next to each other so they're easy to access together. You only need to remember the address of the first box, which is what a pointer can help you with.
You could even point to an array of pointers, each pointing to their own array of boxes.
Anonymous No.105711692 >>105711763
>>105711589
Ok modify [3][3] in a function. Oops I need a pointer. Okay, now do pointer things to that pointer. Oops, I need it to be a pointer to a pointer.

capiche?
Anonymous No.105711718 >>105711738 >>105711823
>>105711666
ah yes. when moving i always have a box for telling me whats in another box... can't forget my box for telling me what's in that box too! im so forgetful
Anonymous No.105711738 >>105711801
>>105711718
No! It doesn't tell you what's in the box! It tells you WHERE the box is!
Anonymous No.105711763
>>105711692
ohhhhhhh
Anonymous No.105711801
>>105711738
sorry anya
Anonymous No.105711823 >>105711854
>>105711718
think of it as a shop catalog
brick is the data, let's say it's a purple brick
catalog saying "pick up your brick on this shelf" is pointer to data, it doesn't know that the brick is purple but it knows that it's brick-type
a line in catalog's table of contents saying "bricks, page 5" is a pointer to pointer to pointer, it points to page 5 that points to shelf where our purple brick resides
someone can change the brick to yellow, but you'll still have the same catalog so you'll find your brick
new edition of the catalog can change the shelf it tells you to pick up bricks from, but table of contents can stay the same since it's all still on page 5
Anonymous No.105711842
>>105711325 (OP)
Imagine you have something the dynamically allocates memory, you're not sure where it is going to be allocated but you need to access it. If you're the code that creates it youll get the pointer back. Now imagine multiple areas need to access that memory. You could pass the pointer around, or you could store the pointer in a known location. Then you could use a pointer to that known location in those multiple areas so that they can easily find that allocated memory without having to allocate it themselves
Anonymous No.105711853 >>105711907
Variables are just pointers with human understandable names.
Anonymous No.105711854 >>105711868
>>105711823
Some chucklefuck moved the shelf so when I got there I Segmentation fault (core dumped)
Anonymous No.105711868
>>105711854
Anonymous No.105711887 >>105711913 >>105711925
Wait until OP learns about pointers to pointers to pointers.
Wait until he learns arrays are like pointers.
I wonder how OP will react when he learns hashmaps and realizes all languages with multidimensional datasets are juggling points.
Think about how many multidimensional pointers to structs and associated hashmaps need to be iterated through and processed just to render a web page every time something on the page changes.
And now you truly understand how bad tech has gotten.
Anonymous No.105711904
If you have for example a function to get some element of an array, you may want to return it as a pointer because you don’t know if the element exists.
Say you have a Find function, and you have a list of 3 elements and call Find(5), then that could crash your program. Instead Find returns a pointer to the found element. Now, if it’s out of bounds, it’s just a nullptr.
What happens if the list is not a list of values, but a list of pointers?
Anonymous No.105711907 >>105713234
>>105711853
No not really
Anonymous No.105711910 >>105715548
My first job, years back, I was working in a massive 20 million LOC C++ codebase, mostly from the 90s. I saw fucking triple pointers all the time. It was hell on Earth but put hair on my chest.
Anonymous No.105711913 >>105711949
>>105711887
Who is confused by pointers??? Nigger it’s like finding index fingers confusing
Anonymous No.105711925
>>105711887
it's already happening anon...

is it too late to crawl back to js?
Anonymous No.105711949
>>105711913
I didn't say they're confusing. I'm saying modern tech is a clusterfuck because people keep trying to abstract pointers away causing layers of overly complex spaghetti code that no one understands.
Anonymous No.105712278
>>105711325 (OP)
>why are there arrays
Anonymous No.105713234
>>105711907
>No not really
No kinda exactly.
Anonymous No.105713350
>>105711325 (OP)
So we can have a listless link list
Anonymous No.105713380 >>105713681
>>105711325 (OP)
Less analogy. More programming.
You will learn it intuitively. A lot of learning is done subconsciously.
Stop looking for the best explanation/article/video/book. And just do it.
Anonymous No.105713490 >>105713610 >>105715147
>>105711466
>2D array
Flatten that shit into a 1D array for obvious performance reasons.
Anonymous No.105713610 >>105713800
>>105713490
Source?
Anonymous No.105713616
>>105711325 (OP)
because object which you have a pointer of might have an object inside
because an array cells might store pointers to other arrays
Anonymous No.105713648
>>105711325 (OP)
Because Dennis Ritchie copied it from other people. He didn't invent anything.
Anonymous No.105713681
>>105713380
I kinda dove headfirst into network/socket programming trying to do exactly this. A lot of the explanations here did help though especially about multidimensional arrays.
Anonymous No.105713800 >>105714928 >>105715133
>>105713610
>why is a single contiguous area of memory more efficient than a contiguous area containing pointers to other contiguous areas scattered everywhere?
I bet you can't even tell why almost all list implementations are arrays instead of linked lists, loser.
Anonymous No.105714817
>>105711325 (OP)
Wait until you come across the higher levels of indirection! By the time you get to five, even experts will be scratching their heads to start with...
Anonymous No.105714827
>>105711325 (OP)
Post yfw every RAM access is a 5 level pointer.
Anonymous No.105714829
>>105711325 (OP)
An array of arrays you can't define at compile time.
Anonymous No.105714928
>>105713800
No source? I just don’t believe /g/ poster.
Anonymous No.105714979
>WHY ARE THERE POINTERS TO POINTERS
Why not?
Anonymous No.105715035
most of the time think of it as a pointer to an array that contains pointers. dummy program to show u exactly how it works.
#include
#include

int** two_d_size_n(const int n)
{
int** mem = malloc(n * sizeof(int*));
for (int i = 0; i < n; i++) {
*(mem + i) = malloc(n * sizeof(int));
}
return mem;
}

void fill_2d(int** mem, const int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
*(*(mem + i) + j) = i;
}
}
}

void print_2d(int** mem, const int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", *(*(mem + i) + j));
}
printf("\n");
}
}

void free_2d(int** mem, const int n)
{
for (int i = 0; i < n; i++) {
free(*(mem + i));
}
free(mem);
}

int main(int argc, char* argv[])
{
/// 10 x 10 matrix
const int size = 10;
int** mem = two_d_size_n(size);
fill_2d(mem, size);
print_2d(mem, size);
free_2d(mem, size);
return 0;
}
Anonymous No.105715114 >>105715147
Y'know indexes in an array?
Your memory sticks in your computer are literally just a gigantic 16 or 32GB array. In practice you have a small amount of this array available to your program but it's still all contiguous like a single big array. Declaring arrays or pointers or whatever is just a matter of navigating around your section of this giant array. If you want to skip around to random parts of it to do fancy algorthms or make trees and whatnot, you can use pointers and pointers-to-pointers and pointer arithmetic
Anonymous No.105715133
>>105713800
That is completely negligible compared to anything important you will have to use matrices for
Anonymous No.105715147 >>105715160
>>105711466
>>105713490
2D arrays are not arrays of pointers. C is brain damage.

>>105715114
C cripples the mind.
Anonymous No.105715160 >>105715198
>>105715147
If C cripples the mind, (p)OOP injects mercury into it and FP puts a 12 gauge slug through it
Anonymous No.105715198 >>105715233
>>105715160
OOP and FP have a lot of amazing advances for hardware and OS design that are ignored by the mainstream because C programmers are not smart enough to understand any of them.
Anonymous No.105715204
>>105711589
you can resize malloced arrays besides what the other anon said
Anonymous No.105715233 >>105715454
>>105715198
>advancing hardware and operating systems via programming paradigms that were literally invented so we could shut the physical reality of a computer (its hardware) out of our minds while programming and use jeet-friendly abstractions instead
You don't know what the sentence you put together even means. Letting turd worlders on the internet was a mistake
Anonymous No.105715454
>>105715233
FP and OOP are easier to use AND closer to the hardware at the same time. C has nothing to do with "the physical reality of a computer (its hardware)" at all. The PDP-11 that C came from used a microcode layer to implement an artificial instruction set with no relationship to the actual hardware. That was not just the PDP-11, but x86, 68k, S/370, and most other processors. They hid the hardware so they could make it easier for assembly programmers who wanted the computer to mimic a person doing things one at a time. Computers like Itanium that don't follow those rules are notoriously hard to write assembly for by hand. FP and OOP expose the physical reality of the computer and allow more freedom in hardware design instead of mimicking a fake sequential processor that pretends it's the 1960s.

OOP is basically how components of a computer communicate. DDR, PCI, USB, SATA, and other protocols are object-oriented. You are sending messages to physical objects, which are abstract and encapsulated (for example, you can replace your hard drive with an SSD and commands would still work because they follow OOP principles). Even your RAM works that way. FP is basically how FPGAs and circuits work at the electrical level, like the Clash language based on Haskell. The internals of the CPU are not "procedural" or "like C" at all, but more like a collection of objects with parts that are functional and parts that hold state and communicate with messages.
https://clash-lang.org/
Anonymous No.105715548
>>105711910
Spider-Man meme.jpg
Anonymous No.105715582 >>105715607 >>105715629
>>105711325 (OP)
Everything that's not a bitword in a register is a pointer to something. A string array is a pointer to a memory location where a pointer to a memory location is located. Pointers to pointers are just ripping away the abstraction. It can be useful sometimes, and it's good to understand just so you have a more holistic understanding of how the machine actually works.
Anonymous No.105715607
>>105715582
>a pointer to a memory location where a pointer to a memory location is located
Anonymous No.105715629
>>105715582
>Everything that's not a bitword in a register is a pointer to something. A string array is a pointer to a memory location where a pointer to a memory location is located.
This is not true at all. C dumbed you down so you can't understand how things work anymore. String arrays do not have any pointers anywhere. The array itself might have an address (depending on how it's implemented and how the computer works), but that is not stored in the array.