There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

tobogganablaze ,

If I had a penny for every pixel … I’d have around $1088. Which I would take, but really it’s not enough.

JPDev OP ,
@JPDev@programming.dev avatar

just edited to upscale the image

SpaceNoodle ,

I don’t think I’ve ever explicitly gone four deep. Two is common enough, and three happens on some rare occasions, but four seems like sheer madness.

Ephera ,

Dumb question, but when would you need two deep? Is it when you store a pointer as a field in a struct?

If so, isn’t that a massive footgun, because the pointer might go invalid at any point? 🫠

SpaceNoodle ,

Pointers to arrays or arrays of pointers are common examples.

Your pointers won’t just magically become invalid. You gotta fuck 'em up first.

Ephera ,

Ah, you mean “pointers to arrays”, because arrays are themselves just pointers in C/C++. That one still feels like it shouldn’t be needed in practice, because you already got a pointer, why can’t you use that directly? But yeah, I have no practical experience with C/C++.

And you do gotta fuck 'em up, as in free what they’re pointing to before you free the struct/array containing the pointers.
But when you do stick them into a struct/array, that often means you want to move them out of the scope with the malloc and potentially store them, too.
At that point, surely, it becomes rather difficult for your whole team to know or track down when it’s legal to free that.

I only know from Rust that if you want to store a pointer/reference in a struct, it makes you specify the lifetime of that struct, which has to be greater or equal to the lifetime of the thing the pointer is pointing to. Hairy stuff. We’ve basically told the Rust newbies on our team to just not store pointers in structs and that’s working rather alright.

SpaceNoodle ,

Arrays may be implemented as pointers on C, but the distinction is on how they are used, which is why I used the verbiage I did.

What if you need to modify a reference to a pointer, e.g. change the value of a value referencing a certain place in an array? strtol(), for example, uses a pointer to a pointer to a char to indicate the end of the parsed portion of the input string.

Major codebases performing high-level operations on data that’s shared in barely trackable scopes certainly aren’t best implemented in C. It’s still the language of choice for low-level code, especially on embedded systems, where allocations are not taken lightly.

onlinepersona ,

I know a friend who can point you to a region in memory where you can insert your exploit. You’re welcome.

Anti Commercial-AI license

xmunk ,

Once your pointer definition looks like a censored swear word you’re doing something awful.

In my entire programming career I’ve used int ** less than a handful of times and I’ve always been borderline about refactoring when I need it.

CanadaPlus , (edited )

Okay, but what if you’re dealing with a rather high-dimensional tensor? In some kinds of coding it can happen, and you usually don’t want to sacrifice performance when it does.

You can also do increasingly elaborate pointer arithmetic, but that seems worse, not better to me.

MajinBlayze ,

<span style="color:#323232;">*char // I heard it from a friend
</span><span style="color:#323232;">**char //who heard it from a friend
</span><span style="color:#323232;">***char // who heard it from another
</span><span style="color:#323232;">"You were messing around"
</span>
RonSijm ,
@RonSijm@programming.dev avatar

Me: building a fluent interface framework…
I already support a WrapperOf<T, T, T, T>
User: Can I have a WrapperOf<T, T, T, T, T> because I’m doing something weird?
Me: sigh god-damnit. You’re right but I still hate it.

https://programming.dev/pictrs/image/847ff9ff-1bb8-4a6a-9f86-24aa41e95b29.png

CetaceanNeeded ,

int

I am a friend.

pineapplelover ,

Is there a code example of this?

renormalizer ,

I’ve been a four-star programmer a few times. Imagine a blocked symmetric matrix where the rows and columns are indexed by triples (u,v,w). The entries are zero whenever u != u’ or v != v’, and because of symmetry you only store entries with w <= w’. But the range of v depends on the value of u and the range of w on the value of v. So you do


<span style="font-weight:bold;color:#a71d5d;">double ****</span><span style="color:#323232;">mat </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#62a35c;">calloc </span><span style="color:#323232;">(UMAX, </span><span style="font-weight:bold;color:#a71d5d;">sizeof</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">*</span><span style="color:#323232;">mat));
</span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> u </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">; u </span><span style="font-weight:bold;color:#a71d5d;"><</span><span style="color:#323232;"> UMAX; </span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">u) {
</span><span style="color:#323232;">  mat[u] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#62a35c;">calloc </span><span style="color:#323232;">(u </span><span style="font-weight:bold;color:#a71d5d;">+ </span><span style="color:#0086b3;">1</span><span style="color:#323232;">, </span><span style="font-weight:bold;color:#a71d5d;">sizeof</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">**</span><span style="color:#323232;">mat));
</span><span style="color:#323232;">  </span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> v </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">; v </span><span style="font-weight:bold;color:#a71d5d;"><=</span><span style="color:#323232;"> u; </span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">v) {
</span><span style="color:#323232;">    mat[u][v] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#62a35c;">calloc </span><span style="color:#323232;">(v </span><span style="font-weight:bold;color:#a71d5d;">+ </span><span style="color:#0086b3;">1</span><span style="color:#323232;">, </span><span style="font-weight:bold;color:#a71d5d;">sizeof</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">***</span><span style="color:#323232;">mat));
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> w </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">; w </span><span style="font-weight:bold;color:#a71d5d;"><=</span><span style="color:#323232;"> v; </span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">w) {
</span><span style="color:#323232;">      mat[u][v][w] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#62a35c;">calloc </span><span style="color:#323232;">(w </span><span style="font-weight:bold;color:#a71d5d;">+ </span><span style="color:#0086b3;">1</span><span style="color:#323232;">, </span><span style="font-weight:bold;color:#a71d5d;">sizeof</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">****</span><span style="color:#323232;">mat));
</span><span style="color:#323232;">      </span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> ww </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">; ww </span><span style="font-weight:bold;color:#a71d5d;"><=</span><span style="color:#323232;"> w; </span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">ww)
</span><span style="color:#323232;">        mat[u][v][w][ww] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">some_function (u, v, w, ww);
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">  }
</span><span style="color:#323232;">}
</span>

and weep a little. In reality, this gets a bit optimized by allocating a single chunk of memory and carving that up into the pointer and data arrays, so everything is reasonably close together in memory.

Agent641 ,

uwu, got it.

pineapplelover ,

My brain hurts

jvw ,

Isn’t that like, how you declare different dimensionally sized arrays? If you don’t care about memory integrity?

It’s been literally decades since I had to deal with code like that, so I may have jumped my stack.

calcopiritus ,

If you do it with fixed-size arrays you can accomplish multi-dimension with just int*. Lots of pointer arithmetic needed though. Probably still faster than n levels of indirection.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines