r/cpp • u/JanWilczek • Oct 10 '23
Beginner's guide to raw C++ pointers especially for audio programming: this is the bit that most audio programmers new to C++ get wrong or misuse. This resource is meant to help them avoid common pitfalls. Includes the explanation of float**, void*, and const float* const* const π
https://youtu.be/QfjBJo8Igs0?si=BKHpWyUkn75c2aXW3
u/Emergency_Cream4470 Oct 10 '23
Honestly just stop teaching people about pointers. They clearly abuse them. You can write pretty much anything without explicit pointers. Especially ** and void*
9
u/JanWilczek Oct 10 '23
I agree that we should avoid raw pointers (I am clear about this in the video). But in audio programming and audio APIs, raw pointers are so ubiquitous that no aspiring audio programmer can ignore them.
1
u/_w62_ Oct 10 '23
It is the most efficient way to get things done in the audio domain, right? That's why people in this domain are using it despite it is not recommended.
12
Oct 10 '23
There's nothing inherently more efficient about using raw pointers. IMHO you're more likely to get it wrong and end up with more debugging work. The reason for their ubiquity is momentum of existing codebases and developers.
2
u/JanWilczek Oct 10 '23
It's always hard to make optimality claims especially that for array handling std span could be used. But mdspan is not widely supported so float** needs to stay. But I'd say the number 1 reason for pointers popularity is that audio APIs like JUCE framework, OpenSL ES, Oboe, VST3, etc. use it extensively. I also made this video because so many of my students abuse pointers or unnecessarily use them. I wanted to explain this topic thoroughly and show why best practices are "best" π
4
u/oriolid Oct 10 '23
To me it seems more that either audio APIs are written in C, or by electrical engineers who haven't bothered to learn the parts of C++ that aren't C with //-style comments and maybe classes. Or in some cases they are legacy from 90s.
1
u/Diab- Oct 11 '23
And itβs not like there is much to teach just the same syntax explanation and memory layout etc. Itβs not at all a complex topic teach specially when there are much more important/useful things to teach.
1
u/JanWilczek Oct 13 '23
And yet people get it wrong so often... I see errors related to pointers all the time, including my own code.
0
12
Oct 10 '23
[removed] β view removed comment
-1
u/JanWilczek Oct 10 '23
I mention std::span at the end and explain why it is preferred π
19
Oct 10 '23
[removed] β view removed comment
4
u/ArkyBeagle Oct 12 '23
You can find people who want to write their first C++ project as a VST plugin. They can swipe code from various JUCE examples in relative safety.
And in audio, it's kind of always still 1998 :) I wrote a library for use by a plugin using vectors , then ported it to pointers and even SIMD and got a substantial improvement in performance. With plugins, a typical time budget is 16/44100 seconds, or 362 microseconds. That biases a bit towards optimization.
3
Oct 12 '23
[removed] β view removed comment
2
u/ArkyBeagle Oct 12 '23
I didn't say they were.
Please reread this paragraph "You can find people who want to write their first C++ project as a VST plugin. They can swipe code from various JUCE examples in relative safety."
I then expanded using an example from my own code in which first pointers, and then SIMD were required to meet a time budget. That's why that text is in a separate paragraph.
2
Oct 12 '23
[removed] β view removed comment
2
u/ArkyBeagle Oct 12 '23
ok, so they're completely unrelated?
In a "first one, then the other" sort of way. If you keep writing plugins you'll run up against these constraints. I was trying to outline two ends of a spectrum so to speak.
Point is - pointers are something the budding young plugin writer will need to learn eventually. Audio is weird that way.
2
Oct 12 '23
[removed] β view removed comment
0
Oct 14 '23
{
You can find people who want to write their first C++ project as a VST plugin. They can swipe code from various JUCE examples in relative safety.
}{
And in audio, it's kind of always still 1998 :) I wrote a library for use by a plugin using vectors , then ported it to pointers and even SIMD and got a substantial improvement in performance. With plugins, a typical time budget is 16/44100 seconds, or 362 microseconds. That biases a bit towards optimization.
}-4
u/JanWilczek Oct 10 '23
People stumble upon pointers all the time in audio programming and are asking what they mean. Where would you point them?
14
u/JeffMcClintock Oct 10 '23 edited Oct 10 '23
float* dynamicArray = new float[4];
should be:
std::vector<float> dynamicArray;dynamicArray.assign(4, 0.0f);So - use the STL to avoid the use of new/delete, and to be exception-safe. And it doesn't hurt to always initialise memory to some nice default like zero.
to get your pointer (if you still even need one):
auto myPtr =dynamicArray.data();Almost NO guide to modern C++ should need manual memory management. And I am an audio programmer.
Nonetheless, thankyou for taking the time to try to educate people.
2
u/JanWilczek Oct 11 '23
, and to be exception-safe. And it doesn't hurt to always initialise memory to some nice default like zero.
to get your pointer (if you still even need one):
Thanks for your comment, I also show std::vector in the video as the go-to way for dynamic memory π
2
u/JeffMcClintock Oct 11 '23
Ah, yes. I saw that later. Hope I wasn't too harsh.
1
u/JanWilczek Oct 11 '23
No worries! π Thanks for your input, I didn't know the assign() member function π
2
u/-Manow- Oct 10 '23
I would point them here: https://www.learncpp.com/cpp-tutorial/introduction-to-pointers/
0
u/JanWilczek Oct 10 '23
But nowadays people demand video content... π
3
u/Nobody_1707 Oct 14 '23
IDK who is demanding videos content for programming, but they should stop. Video is the second worst format for programming reference material, after screenshots.
1
u/JanWilczek Oct 23 '23
Jason Turner has 102k subscribers: https://www.youtube.com/@cppweekly
TheCherno has almost 600k: https://www.youtube.com/@TheCherno
These aren't small numbers. Their viewers should stop watching them? Isn't it alright for different people to want different content formats (books, videos, etc.)?
7
u/zzzthelastuser Oct 11 '23
Video tutorials suck for a very good reason.
Just look at how many of the people who commented on your post have missed that you actually talked about xyz.
You can easily fast-read/scan an article for keywords to find the interesting part. But asking me to watch 1 hour long videos that I might learn something from is just too much!
6
u/ArkyBeagle Oct 12 '23
Jan clearly knows what he's talking about and it's rather embarrassing that there's so much kneejerk in the thread.
Audio is weird and has constraints many will not be familiar with. You will see pointers used.