用std :: valarray替换POD C风格的数组是个坏主意吗? [英] Is it a bad idea to replace POD C-style array with std::valarray?

查看:285
本文介绍了用std :: valarray替换POD C风格的数组是个坏主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



它使用了很多包含原始指针的结构体,这些结构体包含原始指针



尽管结构体通常在函数之间传递,但是这些指针的分配和释放位置放在随机位置,不容易被跟踪/推理/了解。



我将其中一些改为类,并且这些指针由类自己修改。他们工作得很好,看起来很丑陋,除了我禁止拷贝构造和这些类的副本分配,因为我不想花时间实现它们。



现在我在想,我是重新发明轮子吗?为什么不用std:array或std :: valarray替换C风格的数组?



我更喜欢std :: valarray,因为它使用堆内存和RAIIed。

Edit1 :std :: array的另外一个加法就是,在我的开发环境中,大多数动态数组是POD(主要是int16_t,int32_t和float)数组,而数字API可能使生活更容易。



在我开始之前,请注意?



我可以想到的是,可能没有一个简单的方法来转换std :: valarray或std :: array回到C - 类型数组,我们的代码部分使用指针算术,需要数据作为简单的C风格数组。



还有什么?



编辑2



我遇到了此问题。关于 std :: valarray 的一个非常糟糕的事情是,它不是安全地可复制分配,直到C ++ 11。



正如在答案中引用的,在C ++ 03及更早版本中,如果源和目标的大小不同,则为UB。

解决方案

C风格数组的标准替换为 std :: vector std :: valarray 是一些奇怪的数学向量做类似数字计算的东西。它不是真正设计为存储任意对象的数组。



话虽如此,使用 std :: vector 很可能是一个很好的主意。 它会解决你的泄漏,使用堆,是可调整大小,有很大的异常安全等等。



它还保证数据存储在一个连续的块记忆。您可以使用 data() 成员函数,如果你是pre-C ++ 11,用& v [0] c> v 。你可以像往常一样处理你的指针业务。


I'm working with a code base that is poorly written and has a lot of memory leaks.

It uses a lot of structs that contains raw pointers, which are mostly used as dynamic arrays.

Although the structs are often passed between functions, the allocation and deallocation of those pointers are placed at random places and cannot be easily tracked/reasoned/understood.

I changed some of them to classes and those pointers to be RAIIed by the classes themselves. They works well and don't look very ugly except that I banned copy-construct and copy-assignment of those classes simply because I don't want to spend time implementing them.

Now I'm thinking, am I re-inventing the wheel? Why don't I replace C-style array with std:array or std::valarray?

I would prefer std::valarray because it uses heap memory and RAIIed. And std::array is not (yet) available in my development environment.

Edit1: Another plus of std::array is that the majority of those dynamic arrays are POD (mostly int16_t, int32_t, and float) arrays, and the numeric API can possibility make life easier.

Is there anything that I need to be aware of before I start?

One I can think of is that there might not be an easy way to convert std::valarray or std::array back to C-style arrays, and part of our code does uses pointer arithmetic and need data to be presented as plain C-style arrays.

Anything else?

EDIT 2

I came across this question recently. A VERY BAD thing about std::valarray is that it's not safely copy-assignable until C++11.

As is quoted in that answer, in C++03 and earlier, it's UB if source and destination are of different sizes.

解决方案

The standard replacement of C-style array would be std::vector. std::valarray is some "weird" math-vector for doing number-calculation-like stuff. It is not really designed to store an array of arbitrary objects.

That being said, using std::vector is most likely a very good idea. It would fix your leaks, use the heap, is resizable, has great exception-safety and so on.

It also guarantees that the data is stored in one contiguous block of memory. You can get a pointer to said block with the data() member function or, if you are pre-C++11, with &v[0] for a non-empty vector v. You can then do your pointer business with it as usual.

这篇关于用std :: valarray替换POD C风格的数组是个坏主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆