我应该如何更改此声明?

发布于 2024-07-16 08:23:02 字数 307 浏览 3 评论 0原文

我得到了一个带有以下声明的标头:

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

陌上青苔 2024-07-23 08:23:02

使用 STL 向量

#include <vector>

std::vector<MyObject> objs(size);

向量是一个 动态数组标准模板库。 当您推回对象到数组中时,它会自动调整大小并可以访问就像带有 [] 运算符的普通 C 数组。 此外,&objs[0] 保证指向内存中的连续序列 - 与 list -- 如果容器不为空。

Use an STL vector:

#include <vector>

std::vector<MyObject> objs(size);

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.

柒夜笙歌凉 2024-07-23 08:23:02

你是对的。 如果您想动态实例化其大小,则需要使用指针。

(既然您使用的是 C++,为什么不使用 new 运算符而不是 malloc 呢?)

MyObject* objs = new MyObject[size];

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?)

MyObject* objs = new MyObject[size];
因为看清所以看轻 2024-07-23 08:23:02

或者我应该将其更改为
指针然后 malloc() 吗?

如果这样做,将如何为 malloc 内存中的对象调用构造函数? 我会给你一个提示 - 他们不会 - 你需要使用 std::vector 。

Or should I just change it to a
pointer and then malloc() it?

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.

找个人就嫁了吧 2024-07-23 08:23:02

我只见过在结构体或联合体中用作指针的数组。 这是很久以前的事了,用于将字符串的 len 和第一个字符视为散列,以提高脚本语言的字符串比较速度。

代码与此类似:

union small_string {
   struct {
      char len;
      char buff[1];
   };
   short hash;
};

然后使用 malloc 初始化small_string,注意 c 强制转换实际上是 reinterpret_cast

small_string str = (small_string) malloc(len + 1);
strcpy(str.buff, val);

并测试相等性

int fast_str_equal(small_string str1, small_string str2)
{
   if (str1.hash == str2.hash)
      return strcmp(str1.buff, str2.buff) == 0;
   return 0;
}

正如您所看到的,这不是一种非常可移植或安全的 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:

union small_string {
   struct {
      char len;
      char buff[1];
   };
   short hash;
};

Then small_string was initialised using malloc, note the c cast is effectively a reinterpret_cast

small_string str = (small_string) malloc(len + 1);
strcpy(str.buff, val);

And to test for equality

int fast_str_equal(small_string str1, small_string str2)
{
   if (str1.hash == str2.hash)
      return strcmp(str1.buff, str2.buff) == 0;
   return 0;
}

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.

羁拥 2024-07-23 08:23:02

这是在某个结构的末尾吗?

我见过的一个技巧是声明一个 struct

struct foo {
/* optional stuff here */
int arr[1];
}

并 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

struct foo {
/* optional stuff here */
int arr[1];
}

and malloc more memory than sizeof (struct foo) so that arr 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.

酸甜透明夹心 2024-07-23 08:23:02

如果您想要动态调整数组大小,则最好使用 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.

霊感 2024-07-23 08:23:02

看起来 - 是的,你可以做这个改变。
但请检查您的代码 sizeof( objs );

MyObj *arr1 = new MyObj[1];
MyObj arr2[1];

sizeof(arr1) != sizeof(arr2)

也许这个事实在您的代码中的某个地方使用过。

Its seems - yes, you can do this change.
But check your code on sizeof( objs );

MyObj *arr1 = new MyObj[1];
MyObj arr2[1];

sizeof(arr1) != sizeof(arr2)

Maybe this fact used somewhere in your code.

煮酒 2024-07-23 08:23:02

这个评论真是太糟糕了。 即使注释另有说明,单元素数组也是一个数组。

我从未见过有人试图以这种方式强制执行“是一个数组”。 数组语法主要是语法糖(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 as 2[a]: i.e., the third element in a (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 with std::vector makes even more sense (because you don't have to remember to call delete[] every place the array goes out of scope due to return, break, the end of statement, throwing an exception, etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文