以局部阵列为参数的性能和安全性
我只是想确保这没有任何潜在的问题。它编译并运行良好,但我是否会冒任何奇怪的记忆效应的风险?在这种情况下我需要特别关注异常情况吗?
//...constructor of some class
myobj(int length, int *vars)
{
// do stuff with vars
}
// inside some other code somewhere
int vars[3] = {5,6,7};
myobj *o = new myobj(3,vars);
(编辑。)我只是担心,因为我知道指向堆栈上对象的指针应该始终谨慎使用。具体来说我的用法,我主要希望以最快的方式传递可变数量的相同类型的参数。那么这是一个不好的方法吗?谢谢..
后记。所有的答案都非常有帮助,谢谢!我将看看性能是否足够重要以使用此方法,或者我是否可以完全以另一种方式解决问题。
I just want to make sure there aren't any potential problems with this. It compiles and runs fine, but am I risking any weird memory effects? Do I need to be especially concerned about exceptions in this case?
//...constructor of some class
myobj(int length, int *vars)
{
// do stuff with vars
}
// inside some other code somewhere
int vars[3] = {5,6,7};
myobj *o = new myobj(3,vars);
(Edit.) I'm just concerned because I know pointers to objects on the stack should always be used with caution. To be specific about my usage, I'd mainly like the fastest way to pass in a variable number of arguments of the same type. So is this a bad way to do it? Thanks..
Postscript. All the answers were very helpful, thanks! I'll see whether the performance matters enough to use this method, or perhaps if I can solve the problem another way altogether.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这没有什么本质上的错误。但是,有一些事情需要注意:
vars
的指针。vars
仅在您离开声明它的作用域之前有效。vars
数组的末尾。这是 C++ 中更常见的样式,例如
std::vector; &
参数,或其他一些容器类型。这有两个主要原因:通过作为引用传递,可以更明显地看出您可能不打算保留指向对象的指针,并且通过传递向量,数组的大小与数组一起保留本身(如果需要,您也可以扩展数组)。然而,这是一个风格问题,并且向量会产生开销,因此如果性能非常重要,那么这种方法是完全合理的。There's nothing inherently wrong with this. However, there are a few things you need to be careful of:
vars
around after you return (or at least, after it goes out of scope).vars
is only valid until you leave the scope that it was declared in.vars
array.It's more common style in C++ to use, e.g. a
std::vector<int> &
parameter, or some other container type. This has two major reasons for it: By passing as a reference, it makes it more obvious that you may not be meant to keep a pointer to the object, and by passing a vector, the size of the array is kept along with the array itself (and you can expand the array if need be as well). However, this is a matter of style, and there is overhead tovector
, so if performance is at a premium, this approach is perfectly reasonable.如果您的
myobj
类由于某种原因存储了指向int[]
的指针,那么您就打开了各种麻烦的大门——当原始的vars< /code> 数组超出范围,你只剩下一个悬空指针;你不能轻易复制你的课程;你必须考虑异常安全;等等。
最好使用标准库容器来解决您的问题,就像史蒂夫建议的那样。
参考你的问题的标题,如果你真的只是将数组传递给“一些自由函数”,那么也许这可能是可以原谅的,但你明确地说你在动态分配对象的构造函数中使用它。这简直就是麻烦的味道。
If your
myobj
class for some reason stores the pointer to theint[]
, then you open the doors to all sorts of trouble -- when the originalvars
array goes out of scope, you're left with a dangling pointer; you cannot easily copy your class; you have to think about exception safety; etc etc.Much preferable to use a standard library container that takes care of your problems, like Steve suggests.
Referring to the title of your question, if you really just pass the array to "some free function" then perhaps this may be excusable, but you explicitly say that you're using it in the constructor of a dynamically allocated object. That just reeks of trouble.
只要您不超出被调用构造函数中的数组,或者滥用所涉及的内存,就可以了。
虽然 C++ 不太好 - 使用 const vector& 作为参数有什么问题吗?
特别是,不要将指向基于堆栈的数组的指针存储在从它创建的
myobj
实例中 - 即。您必须将输入int
值复制到类中,除非您使用类似 boost::shared_array。This is fine so long as you don't overrun the array in the called constructor, or otherwise abuse the memory involved.
It's not great C++ though - what's wrong with using
const vector<int>&
as the parameter?In particular, don't store a pointer to your stack-based array in the
myobj
instance that's created from it - ie. you have to copy the inputint
values into the class, unless you use something like boost::shared_array.