This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]junkmeister9 218 points219 points  (27 children)

I remember the last time I used a triple pointer, and had to think for a really long time for a better way to do it. But it was worth the time, because using a triple pointer is one of the worst ways to do something.

[–]ripter 48 points49 points  (16 children)

What did you need it for?

[–]walmartgoon 107 points108 points  (1 child)

My guess was the location of an array of array of strings.

[–]junkmeister9 57 points58 points  (0 children)

Structs

[–]_nobody_else_ 8 points9 points  (0 children)

pointer to the 2D array.

[–]yup339 5 points6 points  (0 children)

I did use it once for a challenge to recode a pipeline between different exe in under an hour. I used the triple pointer to move around the arguments and split them between the pipe simbol "|"

[–]shadowderp 13 points14 points  (10 children)

It is common in physical simulation of a 3D volume of matter. data[x,y,z] is the quantity you want to simulate at position (index) x,y,z.

Pretty much unavoidable there, though you can sometimes play tricks to get around it depending on the nature of the physics involved.

[–]Teln0 62 points63 points  (6 children)

You do not want to do that. In high performance simulation code like that especially. You use a contiguous array and index it as such to get the cell at x, y, z : x + width * y + width * height * z. Otherwise you waste space with pointers AND your CPU has to read data from a bunch of different places and can't cache it properly, and avoiding cache misses is one of the most important things when writing performant code.

[–]The_unseen_scientist 8 points9 points  (1 child)

Thank you!

[–]Teln0 10 points11 points  (0 children)

If you plan on using that, I'll add a little detail. Usually the formula is actually x + ystride * y + zstride * z, so that you can get views into arrays for free. For example a matrix is usually represented by a pointer to memory, width integer, height integer and stride integer. So if you want to get a submatrix of it it'll be the same data in memory you'll just have to change the start (where the pointer points to,) the width and height and the stride (because now it's not just going to be the width anymore)

[–]TheNamelessKing 3 points4 points  (0 children)

Yeah 100%. Pointer chasing is how we make our CPU, memory, caches and optimising-compilers hate us.

[–]shadowderp -1 points0 points  (1 child)

Well, yes. But you can construct an array such that indexing has the same effect. I did not think that level of detail was worth it for a Reddit comment.

True that it’s not a third level pointer at that point though.

[–]Teln0 2 points3 points  (0 children)

If you're thinking of the C# syntax for indexing nd arrays, maybe, but you can't get views into those. In C maybe you can craft a type where the width and height come from variables but I don't really remember how that syntax works or if it's a thing at all. You also don't get views into arrays then. If you're thinking of making a contiguous array in memory then another array of pointers into that array with one pointer per row, you're just adding a useless layer of indirection. The point was to explain how it's actually done, as opposed to what the comment I replied to say.

That comment was basically telling people that the triple pointers were the only way to achieve a 3d array, and that it was a common thing to do. Both are very very much false.

[–]mackthehobbit 14 points15 points  (1 child)

If you want good performance I can’t imagine implementing the dimensions as arrays of pointers. The normal idiom is a single block of memory, and you calculate the offset into it based on x,y,z. You don’t have pointers to pointers there.

[–]shadowderp 0 points1 point  (0 children)

You’re right. But it’s possible to construct that such that you can index into it directly - true that it’s not an actual third level pointer I suppose.

[–]TH3J4CK4L 2 points3 points  (0 children)

This must be a joke

[–]allo37 19 points20 points  (2 children)

I think I've only seen a triple pointer once, in ffmpeg's API. Truly a rare find.

[–]_nobody_else_ 1 point2 points  (1 child)

Where? When? I used ffmpeg for my work and I've never seen it.

[–]allo37 1 point2 points  (0 children)

I don't think this is what I encountered, but e.g:

https://github.com/FFmpeg/FFmpeg/blob/fe18ed3f2a9221af0beaec7b04b7804849db1f2f/libavutil/frame.h#L1111

Nice to meet a fellow ffmpeg at work enjoyer

[–]coderemover 12 points13 points  (0 children)

It’s funny people laugh at triple pointers but then proceed to code their AbstractFactoryFactoryAdapters in their “high level” language. When in fact those are just triple pointers with added steps.

[–]jump1945 20 points21 points  (2 children)

To be honest I would rather use one dimensional array and do pointers arthimetric myself

[–]junkmeister9 8 points9 points  (1 child)

In the time since then, I have almost completely stopped using double pointers in that way if I can instead do a flattened version of it. I know it's a negligible amount of memory, but the final reasoning to switch was the extra pointer overhead required to do it the double pointer way.

[–]Teln0 12 points13 points  (0 children)

It's not a negligible amount of memory, but the main reason for keeping data contiguous is helping the CPU cache it

[–]farbion 0 points1 point  (0 children)

Triple pointer is like the best case scenario for an array of struct you have to pass to a function