Pointers

https://twitter.com/theprimeagen/status/1683671315377541121

>i have become unsafe, destroyer of production

30
Jump in the discussion.

No email address required.

if you use double pointers anywhere outside plain C (not C++) where it's required in some cases then you should be executed

not an opinion, just an objective fact

Jump in the discussion.

No email address required.

thats why i always use references to pointers to references.

Jump in the discussion.

No email address required.

:#marseydizzy:

I remember at one point using some reference objects but I threw in the towel when I realized I could just use shared_ptr instead and it wasn't even in a hot loop so the overhead didn't matter.

Jump in the discussion.

No email address required.

C is my bread and butter. Double pointers for 2d variable length arrays.


Putting the :e: in :marseyexcited:

Jump in the discussion.

No email address required.

That’s cool I guess

Jump in the discussion.

No email address required.

you use int** for 2d arrays? neighbor that doesn't even work since you've erased the size info from both dimensions, unless by "2d" you mean "array of pointers to arrays of pointers" which isn't 2d, it's just an array of arrays

Jump in the discussion.

No email address required.

>it's just an array of arrays

Yeah and a matrix is just a vector of vectors, what's your point?

Jump in the discussion.

No email address required.

it's not the same in software, mathcel

Jump in the discussion.

No email address required.

what are you talking about, it's different because it:

  • involves an extra array of pointers in memory

  • requires two pointer dereferences instead of one to get a value

Jump in the discussion.

No email address required.

Unless you are using a boost array, nothing beats pointers for speed when variable size is required.

Jump in the discussion.

No email address required.

the number of scenarios where you need variable size and also std::vector<> is too slow for you is vanishingly small

not to mention that I'm talking about double (or triple or more) pointers, your scenario is only a single pointer. there are certainly some uses for raw pointers in C++ (although std::unique_ptr<> is better in the vast majority of cases and has zero runtime overhead for dereferencing) but frankly they're few and far between these days.

Jump in the discussion.

No email address required.

not to mention that I'm talking about double (or triple or more) pointers, your scenario is only a single pointer.

Depending on the design space, having triple or double pointers are better for representing a 3D space, with your vector field. If you order your data right, it becomes really easy to pass along entire blocks for MPI messages by abusing how pointers work across rows.

std::vector<>

Is dogshit slow if you are initializing a buffer during your computation. It initializes all elements where a raw array does not. This is a huge performance hit when you are talking millions of elements. There is a reason high end computational codes avoid std:vector.

Jump in the discussion.

No email address required.

int** is a pointer to a pointer to an int. Not a "2d array". There's no dimension so if you think you're gonna x[4][2] or something it's not gonna work the way you think it will.

If you're really looking for a 2d array you're better off having a plain int* / int[] buffer in a struct with the dimensions and performing lookups via getters or operator overloads. You can also slice up the larger buffer at will.

What int** is actually used for, in reality, is when you want something like an int* output parameter to a function. Even then a reference would be better so the only acceptable use is if you also need to support passing nullptr to "ignore" that output parameter.

Jump in the discussion.

No email address required.

"2d array".

Its funny, I never said the words 2d array,

so if you think you're gonna x[4][2] or something it's not gonna work the way you think it will.

Out of the box, no, but you can initialize the outer pointer as an array, and then the inner array and end up with an array of arrays that can be accessed as A[i][k]. Which if you setup right it becomes trivial to pass the start of your sections for MPI with A + r * m n for MPI or other parallel paradigms, like passing parts of the array array to a GPU.

Let me guess, you're a CS, not a CE?

Jump in the discussion.

No email address required.

You'd need one actual buffer with the data then a separate array to the side that contains pointers into that buffer. This extra array of pointers is generally completely unnecessary, at least if your data is rectangular - the only possible requirement for it is if you have a "jagged 2d array", basically where each row has its own length.

What you're describing is adding a whole extra array and indirection because you're too r-slurred to use an operator overload or getter, and apparently don't think compilers optimize getters (hint: they're virtually always inlined unless you define them outside the class/struct def).

Let me guess, you do this shit on some paltry raspberry pi "cluster" and don't actually have a job doing it for real?

Jump in the discussion.

No email address required.

Let me guess, you do this shit on some paltry raspberry pi "cluster" and don't actually have a job doing it for real?

So I guess the answer to my question is "Yes" you are a CS kiddie, based on not actually replying to what I wrote.

You are not going to what to use getter on a GPU, you want to pass the least amount of data to the GPU. For instance of you are running Strassen's Algorithm you can pass the submatricies using linear offsets to MPI, and derefencing the outer pointer after your linear offset lets you pass that submatrix to CUDA. This becomes a lot more important when your Matrix is actually a third order tensor with a vector of your flow field, which keeps it cleaner.

I do have a job doing this, I even helped get one of our GPU codes to work on Summit.

Jump in the discussion.

No email address required.

There's literally nothing stopping you from slicing up a buffer and you don't need a separate array of pointers off to the side to do it.

Jump in the discussion.

No email address required.

I did some triple pointer array for a project in my freshman year C++ class. The teacher said something along the lines of "well I know you didn't have someone do this for you since anyone who knew what they were doing wouldn't have used triple pointers".

Jump in the discussion.

No email address required.

i actually laughed thanks

Jump in the discussion.

No email address required.

Remind me what's the point of pointers ?

Jump in the discussion.

No email address required.

:#marseysoypoint:

to point at soys

Jump in the discussion.

No email address required.

:!marseysoypoint: :#marseysoypoint:

Jump in the discussion.

No email address required.

They're God's greatest :marseydiversity: gift to humanity (along with the goto statement)

Jump in the discussion.

No email address required.

To point

Jump in the discussion.

No email address required.

Gay nerd shit

Jump in the discussion.

No email address required.

Explain it to me like I'm r-slurred

Jump in the discussion.

No email address required.

I have a stick

I also have a map that "points" to where the stick is

I also have a map that "points" to that map

ect.

seg fault

Jump in the discussion.

No email address required.

Oh I see now why triple pointers are probably unnecessary

Jump in the discussion.

No email address required.

Like others have mentioned, there is some dark magic that can be done with triple pointers but if you have to use triple pointers to do it you are probably doing it wrong anyway.

Jump in the discussion.

No email address required.

Befriend Jannies. Respect Jannies. Roundhouse kick Janny friendships into existence. Slam dunk some healthy fruit and vegetables into Janny babies' mouths. Love thy Janny. Share some food with a Janny. Launch Jannies into orbit as part of high-paying astronaut jobs. Treat Jannies like human beings. Warn Jannies not to fall into active volcanoes. Share life experiences with Jannies. Watch TV and play video games with Jannies. Invite Jannies to parties. Report Jannies to the Nobel foundation. Karate chop ban evaders in half. Give up your seat for pregnant Jannies. Free Jannies from quicksand. Appreciate Jannies. Observe Janny history month. Eat with Jannies. Judge Jannies by the content of their character. Dance with Jannies with steel-toed boots. Cremate Jannies in the oven, but only if their family didn't want them to be buried. Dignify Jannies. Civil rights for Jannies. Collect Jannies garbage to put in the garbage disposal. Surgically reconstruct Jannies with a ray gun. Help old Jannies cross the road. Admire Jannies. Slice word filter evaders with a katana.

Snapshots:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.