分配指向固定大小数组的指针

发布于 2024-12-09 03:53:13 字数 683 浏览 3 评论 0原文

我对指针使用的基础知识有两个疑问。

使用以下代码,

 int (*p_b)[10];
 p_b = new int[3][10];
 // ..do my stuff
 delete [] p_b

p_b 指向一个包含 3 个元素的数组,每个元素的固定大小长度为 10 int。

Q1:

如果我希望每个元素都是指向固定数组大小的指针,如何声明 p_b ? 基本上我想要以下

  p_b[0] = pointer to a fixed-array size of 10
  p_b[1] = pointer to a fixed-array size of 10
  // ... and so on

我正在考虑的 int (** p_b)[10] 但后来我不知道如何使用 new 来分配它?我想避免回到更通用的 int** p_b

问题 2:

根据上面我的原始代码示例,如何调用 new 以便 p_b 指向 10 int other 的唯一固定大小数组而不是调用 p_b = new int[1][10]?为了释放内存,我必须调用 delete[],但我找不到只能简单调用 delete 的表达式。

I have 2 doubts regarding basics of pointers usage.

With the following code

 int (*p_b)[10];
 p_b = new int[3][10];
 // ..do my stuff
 delete [] p_b

p_b is pointing to an array of 3 elements, each having fixed-size length of 10 int.

Q1:

How to declare p_b if I want that each element be a pointer to a fixed array size?
Basically I want the following

  p_b[0] = pointer to a fixed-array size of 10
  p_b[1] = pointer to a fixed-array size of 10
  // ... and so on

I was thinking to int (** p_b)[10] but then I don't know how to use new to allocate it? I would like to avoid falling back to more general int** p_b

Q2:

Is per my original code sample above, how to call new so that p_b points to a unique fixed-size array of 10 int other than calling p_b = new int[1][10]? To free memory I have to call delete[] while I cannot find an expression where I can call only simply delete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

烟沫凡尘 2024-12-16 03:53:13

p_b 指向一个包含 3 个元素的数组,每个元素的固定大小长度为 10 int。

如果我希望每个元素都是指向固定数组大小的指针,如何声明 p_b

你的第一句话没有完全涵盖这个问题吗?

根据上面我的原始代码示例,除了调用 p_b = new int[1][10] 之外,如何调用 new 以便 p_b 指向 10 int 的唯一固定大小数组?为了释放内存,我必须调用delete[],但我找不到只能调用简单删除的表达式。

我完全不明白为什么这是一个问题,但你可以通过将数组包装在另一种类型中来做到这一点......比如 std::array, boost::arraystd::vector

p_b is pointing to an array of 3 elements, each having fixed-size length of 10 int.

How to declare p_b if I want that each element be a pointer to a fixed array size?

Does your first sentence not completely cover that question?

Is per my original code sample above, how to call new so that p_b points to a unique fixed-size array of 10 int other than calling p_b = new int[1][10]? To free memory I have to call delete[] while I cannot find an expression where I can call only simply delete.

I completely do not understand why this is a problem, but you could do it by wrapping your array inside another type... say std::array, boost::array or std::vector.

国产ˉ祖宗 2024-12-16 03:53:13

首先,如果您的 new 表达式有方括号 (new somtype[somesize]),您的 delete 也必须有方括号 ( 删除 [] your_pointer)。

其次,现在您已将 p_b 定义为指向某些数据的单个指针。如果你真正想要的是一个指针数组,那么你需要将它定义为一个数组。由于您显然需要三个独立的数组,因此您必须单独分配每个数组。如果您从 typedef 开始,这可能是最简单的:

typedef int *p_int;
p_int p_b[3];

然后您将分配三个数组:

for (int i=0; i<3; i++)
    p_b[i] = new int[10];

要删除这些数组,您需要分别删除每个数组:

for (int i=0; i<3; i++)
    delete [] p_b[i];

我绝对同意@Tomalak,您几乎不应该自己搞乱这样的事情尽管。目前尚不清楚您真正想要完成什么,但仍然很容易猜测标准容器很可能是一种更简单、更干净的方法来实现这一目标。

First of all, if your new expression has square brackets (new somtype[somesize]), your delete has to have square brackets as well (delete [] your_pointer).

Second, right now you've defined p_b to be a single pointer to some data. If what you really want is an array of pointers, then you need to define it as an array. Since you apparently want three independent arrays, you'll have to allocate each of them separately. It's probably easiest if you start with a typedef:

typedef int *p_int;
p_int p_b[3];

Then you'll allocate your three arrays:

for (int i=0; i<3; i++)
    p_b[i] = new int[10];

To delete those, you'll need to delete each one separately:

for (int i=0; i<3; i++)
    delete [] p_b[i];

I definitely agree with @Tomalak that you should almost never mess with things like this yourself though. It's not clear what you really want to accomplish, but it's still pretty easy to guess that chances are quite good that a standard container is likely to be a simpler, cleaner way to do it anyway.

酒中人 2024-12-16 03:53:13

下面是如何实现 Q1 的示例:

int main()
{
    typedef int foo[10];

    foo* f = new foo[3];

    f[0][5] = 5;
    f[2][7] = 10;

    delete [] f;
}

对于 Q2,删除使用 new[] 分配的内存的唯一方法是使用 delete[]。如果你个人不想写delete[],你可以使用向量或者其他STL容器。实际上,除非这是一些硬核的超级优化,否则无论如何你都应该使用向量。除非万不得已,否则切勿手动管理内存。

Here's an example of how to implement Q1:

int main()
{
    typedef int foo[10];

    foo* f = new foo[3];

    f[0][5] = 5;
    f[2][7] = 10;

    delete [] f;
}

As for Q2, the only way to delete memory allocated with new[] is with delete[]. If you personally don't want to write delete [], you can use a vector or another STL container. Really, unless this is some hardcore uber-optimisation, you should be using vectors anyway. Never manage memory manually unless you are absolutely forced to.

调妓 2024-12-16 03:53:13

要使用原始指针来管理二维数组,您必须首先创建一个指向数组元素类型的指针,该指针将指向数组的每一行。接下来,必须将每个行指针分配给该行的实际数组元素。

int main()
{
  int **p;

  // declare an array of 3 pointers
  p = new int*[3];

  // declare an array of 10 ints pointed to by each pointer
  for( int i = 0; i < 3; ++i ) {
    p[i] = new int[10];
  }

  // use array as p[i][j]

  // delete each array of ints
  for( int i = 0; i < 3; ++i ) {
    delete[] p[i];
  }

  // delete array of pointers
  delete[] p;
}

一个更简单的解决方案是使用 std::array。如果您的编译器没有提供该类,您也可以使用 std::vector 。

std::array<std::array<int,10>,3> myArr; 
myArr[0][0] = 1;

To use a raw pointer to manage a 2-d array you must first create a pointer to a pointer to array element type that will point to each row of the array. Next, each row pointer must be assigned to the actual array elements for that row.

int main()
{
  int **p;

  // declare an array of 3 pointers
  p = new int*[3];

  // declare an array of 10 ints pointed to by each pointer
  for( int i = 0; i < 3; ++i ) {
    p[i] = new int[10];
  }

  // use array as p[i][j]

  // delete each array of ints
  for( int i = 0; i < 3; ++i ) {
    delete[] p[i];
  }

  // delete array of pointers
  delete[] p;
}

A far easier solution is to use std::array. If your compiler does not provide that class you can use std::vector also.

std::array<std::array<int,10>,3> myArr; 
myArr[0][0] = 1;
逆光下的微笑 2024-12-16 03:53:13

对于第一季度,我认为

int (*p[3])[10];

当您不确定时,您需要尝试 cdecl

其他答案似乎很好地回答了您的其他问题。

问候,
亚蒂·萨加德

For Q1, I think you want

int (*p[3])[10];

Try cdecl when you're unsure.

Your other question seems to be well answered by other answers.

regards,
Yati Sagade

翻身的咸鱼 2024-12-16 03:53:13

事实上,还没有人对你的确切问题给出答案。

or代替

int (*p_arr)[10] = new int[3][10];
// use, then don't forget to delete[]
delete[] p_arr;

我建议使用

std::vector<std::array<int, 10>> vec_of_arr(3);

如果您不需要移动它并且不需要运行时长度,

std::array<std::array<int, 10>, 3> arr_of_arr;

Q1

如果我希望每个元素都是指向固定数组大小的指针,如何声明 p_b?

int(**pp_arr)[10] = new std::add_pointer_t<int[10]>[3];
for (int i = 0; i < 3; ++i)
    pp_arr[i] = new int[1][10];
// use, then don't forget to delete[]
for (int i = 0; i < 3; ++i)
    delete[] pp_arr[i];
delete[] pp_arr;

该代码的现代变体是

std::vector<std::unique_ptr<std::array<int, 10>>> vec_of_p_arr(3);
for (auto& p_arr : vec_of_p_arr)
    p_arr = std::make_unique<std::array<int, 10>>();

或者如果您不需要移动它并且不需要运行时长度:

std::array<std::unique_ptr<std::array<int, 10>>, 3> arr_of_p_arr;
for (auto& p_arr : arr_of_p_arr)
    p_arr = std::make_unique<std::array<int, 10>>();

Q2

根据上面我的原始代码示例,除了调用 p_b = new int[1][10] 之外,如何调用 new 以便 p_b 指向 10 int 的唯一固定大小数组?

除非将数组包装成另一种类型。

std::array<int, 10>* p_arr = new std::array<int, 10>;
// use, then don't forget to delete
delete p_arr;

您可以将 std::array 替换为您最喜欢的数组包装类型,但不能将其替换为固定大小的数组别名。该代码的现代变体是:

auto p_arr = std::make_unique<std::array<int, 10>>();

Actually, nobody posted an answer to your exact question, yet.

Instead of

int (*p_arr)[10] = new int[3][10];
// use, then don't forget to delete[]
delete[] p_arr;

I suggest using

std::vector<std::array<int, 10>> vec_of_arr(3);

or if you don't need to move it around and don't need runtime length:

std::array<std::array<int, 10>, 3> arr_of_arr;

Q1

How to declare p_b if I want that each element be a pointer to a fixed array size?

int(**pp_arr)[10] = new std::add_pointer_t<int[10]>[3];
for (int i = 0; i < 3; ++i)
    pp_arr[i] = new int[1][10];
// use, then don't forget to delete[]
for (int i = 0; i < 3; ++i)
    delete[] pp_arr[i];
delete[] pp_arr;

The modern variant of that code is

std::vector<std::unique_ptr<std::array<int, 10>>> vec_of_p_arr(3);
for (auto& p_arr : vec_of_p_arr)
    p_arr = std::make_unique<std::array<int, 10>>();

or if you don't need to move it around and don't need runtime length:

std::array<std::unique_ptr<std::array<int, 10>>, 3> arr_of_p_arr;
for (auto& p_arr : arr_of_p_arr)
    p_arr = std::make_unique<std::array<int, 10>>();

Q2

Is per my original code sample above, how to call new so that p_b points to a unique fixed-size array of 10 int other than calling p_b = new int[1][10]?

Not without wrapping the array into another type.

std::array<int, 10>* p_arr = new std::array<int, 10>;
// use, then don't forget to delete
delete p_arr;

You can replace std::array<int, 10> with your favourite array-wrapping type, but you cannot replace it with a fixed-size array alias. The modern variant of that code is:

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