iOS 上 Objective-C 中的灵活数组成员
我正在研究一些核心音频代码,并且有一个问题可以通过结构中的变量数组来解决 - 灵活数组成员。通过环顾四周,我发现有很多关于灵活成员数组的可移植性和可行性的对话。
据我了解,Objective-C 符合 C99。因此,我认为灵活的数组成员应该是一个很好的解决方案。我还发现灵活数组成员在 C++ 中不是一个好主意。
在 Objective-C++ 中做什么?从技术上讲,我不会在 Objective-C++ 中使用它。我正在编写基于 C 和 C++ 的回调...这似乎是一个反对点。
无论如何,我可以(应该)这样做吗?如果没有,是否有其他技术具有相同的结果?
I am working on some core audio code and have a problem that could be solved by a variable array in a struct--a la Flexible Array Members. In doing a bit of looking around, I see that there is a lot of dialogue about the portability and viability of Flexible Member Arrays.
From what I understand, Objective-C is C99 compliant. For this reason, I think Flexible Array Members should be a fine solution. I also see that Flexible Array Members are not a good idea in C++.
What to do in Objective-C++? Technically, I won't use it in Objective-C++. I am writing callbacks that are C and C++ based... That seems like a point against.
Anyway, can I (should I) do it? If not, is there another technique with the same results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以声明一个大小为 1 的尾随数组。在最坏的情况下,您会浪费相当少量的内存,并且计算 malloc 的正确大小会稍微复杂一些。
You can always just declare a trailing array of size 1. In the worst case here, you waste a pretty small amount of memory, and it is very slightly more complicated to compute the right size for malloc.
别打扰。它不兼容。它很混乱并且容易出错。早在这个功能存在之前,c++ 就有更容易管理的解决方案。你要在结构的末尾添加什么?通常,您只需使用 std::vector、std::array 或固定大小数组之类的东西。
更新
好的,如果你有固定的复调音乐,那么固定大小的数组应该没问题。在大多数 iOS 合成器中,你不需要多个这样的数组。当然,“即将发布的笔记”数组大小可能会根据应用程序合成器的不同而有所不同?采样器?定序器?实时输入?
如果您需要一个可能非常大的动态大小的数组,那么使用向量。如果您只需要一个实例,并且最大复调数是(比如说)64,那么就使用它。
don't bother. it's not compatible. it is messy and error prone. c++ had solutions which are managed more easily long before this feature existed. what are you tacking onto the end of your struct? normally, you'll just use something like a std::vector, std::array, or fixed size array.
UPDATE
ok, then a fixed size array should be fine if you have fixed polyphony. you will not need more than one such array in most iOS synths. of course, 'upcoming note' array sizes could vary based on the app synth? sampler? sequencer? live input?
if you need a dynamically sized array which could be very large, then use a vector. if you need only one instance of this and the max polyphony is (say) 64, then just use this.