我应该如何更改此声明?
我得到了一个带有以下声明的标头:
//The index of 1 is used to make sure this is an array.
MyObject objs[1];
但是,我需要在程序启动时动态调整该数组的大小。 我认为我应该将其声明为 MyObject *objs;,但我认为如果原始程序员以这种方式声明它,那么这是有原因的。
无论如何,我可以动态调整它的大小吗? 或者我应该将其更改为指针然后 malloc() 吗?
我可以使用一些 new 关键字来做到这一点吗?
I have been given a header with the following declaration:
//The index of 1 is used to make sure this is an array.
MyObject objs[1];
However, I need to make this array dynamically sized one the program is started. I would think I should just declare it as MyObject *objs;, but I figure if the original programmer declared it this way, there is some reason for it.
Is there anyway I can dynamically resize this? Or should I just change it to a pointer and then malloc() it?
Could I use some the new keyword somehow to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 STL 向量:
向量是一个 动态数组 是 标准模板库。 当您推回对象到数组中时,它会自动调整大小并可以访问就像带有
[]
运算符的普通 C 数组。 此外,&objs[0]
保证指向内存中的连续序列 - 与 list -- 如果容器不为空。Use an STL vector:
A vector is a dynamic array and is a part of the Standard Template Library. It resizes automatically as you push back objects into the array and can be accessed like a normal C array with the
[]
operator. Also,&objs[0]
is guaranteed to point to a contiguous sequence in memory -- unlike a list -- if the container is not empty.你是对的。 如果您想动态实例化其大小,则需要使用指针。
(既然您使用的是 C++,为什么不使用 new 运算符而不是 malloc 呢?)
You're correct. If you want to dynamically instantiate its size you need to use a pointer.
(Since you're using C++ why not use the new operator instead of malloc?)
如果这样做,将如何为 malloc 内存中的对象调用构造函数? 我会给你一个提示 - 他们不会 - 你需要使用 std::vector 。
If you do that, how are constructors going to be called for the objects in on the malloc'd memory? I'll give you a hint - they won't be - you need to use a std::vector.
我只见过在结构体或联合体中用作指针的数组。 这是很久以前的事了,用于将字符串的 len 和第一个字符视为散列,以提高脚本语言的字符串比较速度。
代码与此类似:
然后使用 malloc 初始化small_string,注意 c 强制转换实际上是 reinterpret_cast
并测试相等性
正如您所看到的,这不是一种非常可移植或安全的 C++ 风格。 但为由短字符串索引的关联数组提供了巨大的速度改进,这是大多数脚本语言的基础。
今天我可能会避免这种 C++ 风格。
I have only seen an array used as a pointer inside a struct or union. This was ages ago and was used to treat the len and first char of a string as a hash to improve the speed of string comparisons for a scripting language.
The code was similar to this:
Then small_string was initialised using malloc, note the c cast is effectively a reinterpret_cast
And to test for equality
As you can see this is not a very portable or safe style of c++. But offered a great speed improvement for associative arrays indexed by short strings, which are the basis of most scripting languages.
I would probably avoid this style of c++ today.
这是在某个结构的末尾吗?
我见过的一个技巧是声明一个 struct
并 malloc 比 sizeof (struct foo) 多的内存,以便 arr 成为可变大小的数组。
当我破解 C 语言时,这在 C 程序中相当常用,因为可变大小的数组不可用,并且进行额外的分配被认为太容易出错。
几乎在所有情况下,正确的做法是将数组更改为 STL 向量。
Is this at the end of a struct somewhere?
One trick I've seen is to declare a struct
and malloc more memory than
sizeof (struct foo)
so thatarr
becomes a variable-sized array.This was fairly commonly used in C programs back when I was hacking C, since variable-sized arrays were not available, and doing an additional allocation was considered too error-prone.
The right thing to do, in almost all cases, is to change the array to an STL vector.
如果您想要动态调整数组大小,则最好使用 STL,有多种选择,其中之一是 std::向量。 如果您不介意插入,也可以使用 std::list。
Using the STL is best if you want a dynamically sizing array, there are several options, one is std::vector. If you aren't bothered about inserting, you can also use std::list.
看起来 - 是的,你可以做这个改变。
但请检查您的代码 sizeof( objs );
也许这个事实在您的代码中的某个地方使用过。
Its seems - yes, you can do this change.
But check your code on sizeof( objs );
Maybe this fact used somewhere in your code.
这个评论真是太糟糕了。 即使注释另有说明,单元素数组也是一个数组。
我从未见过有人试图以这种方式强制执行“是一个数组”。 数组语法主要是语法糖(
a[2]
给出与2[a]
相同的结果:即a
中的第三个元素(注意 这是一个有趣且有效的语法,但通常这是一种非常不好使用的形式,因为你会无缘无故地让程序员感到困惑))。因为数组语法很大程度上是语法糖,所以切换到指针也是有意义的。 但如果您打算这样做,那么使用
new[]
更有意义(因为您可以免费调用构造函数),并使用std::vector
> 更有意义(因为您不必记住在数组因 return、break、语句结束、引发异常等而超出范围的每个地方调用delete[]
.)。That comment is incredibly bad. A one-element array is an array even though the comment suggests otherwise.
I've never seen anybody try to enforce "is an array" this way. The array syntax is largely syntactic sugar (
a[2]
gives the same result as2[a]
: i.e., the third element ina
(NOTE this is an interesting and valid syntax but usually a very bad form to use because you're going to confuse programmers for no reason)).Because the array syntax is largely syntactic sugar, switching to a pointer makes sense as well. But if you're going to do that, then going with
new[]
makes more sense (because you get your constructors called for free), and going withstd::vector
makes even more sense (because you don't have to remember to calldelete[]
every place the array goes out of scope due to return, break, the end of statement, throwing an exception, etc.).