分配指向固定大小数组的指针
我对指针使用的基础知识有两个疑问。
使用以下代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的第一句话没有完全涵盖这个问题吗?
我完全不明白为什么这是一个问题,但你可以通过将数组包装在另一种类型中来做到这一点......比如
std::array
,boost::array
或std::vector
。Does your first sentence not completely cover that question?
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
orstd::vector
.首先,如果您的
new
表达式有方括号 (new somtype[somesize]
),您的delete
也必须有方括号 (删除 [] your_pointer
)。其次,现在您已将
p_b
定义为指向某些数据的单个指针。如果你真正想要的是一个指针数组,那么你需要将它定义为一个数组。由于您显然需要三个独立的数组,因此您必须单独分配每个数组。如果您从 typedef 开始,这可能是最简单的:然后您将分配三个数组:
要删除这些数组,您需要分别删除每个数组:
我绝对同意@Tomalak,您几乎不应该自己搞乱这样的事情尽管。目前尚不清楚您真正想要完成什么,但仍然很容易猜测标准容器很可能是一种更简单、更干净的方法来实现这一目标。
First of all, if your
new
expression has square brackets (new somtype[somesize]
), yourdelete
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:Then you'll allocate your three arrays:
To delete those, you'll need to delete each one separately:
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.
下面是如何实现 Q1 的示例:
对于 Q2,删除使用 new[] 分配的内存的唯一方法是使用 delete[]。如果你个人不想写delete[],你可以使用向量或者其他STL容器。实际上,除非这是一些硬核的超级优化,否则无论如何你都应该使用向量。除非万不得已,否则切勿手动管理内存。
Here's an example of how to implement Q1:
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.
要使用原始指针来管理二维数组,您必须首先创建一个指向数组元素类型的指针,该指针将指向数组的每一行。接下来,必须将每个行指针分配给该行的实际数组元素。
一个更简单的解决方案是使用 std::array。如果您的编译器没有提供该类,您也可以使用 std::vector 。
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.
A far easier solution is to use
std::array
. If your compiler does not provide that class you can usestd::vector
also.对于第一季度,我认为
当您不确定时,您需要尝试 cdecl 。
其他答案似乎很好地回答了您的其他问题。
问候,
亚蒂·萨加德
For Q1, I think you want
Try cdecl when you're unsure.
Your other question seems to be well answered by other answers.
regards,
Yati Sagade
事实上,还没有人对你的确切问题给出答案。
or代替
我建议使用
如果您不需要移动它并且不需要运行时长度,
: Q1
该代码的现代变体是
或者如果您不需要移动它并且不需要运行时长度:
Q2
除非将数组包装成另一种类型。
您可以将
std::array
替换为您最喜欢的数组包装类型,但不能将其替换为固定大小的数组别名。该代码的现代变体是:Actually, nobody posted an answer to your exact question, yet.
Instead of
I suggest using
or if you don't need to move it around and don't need runtime length:
Q1
The modern variant of that code is
or if you don't need to move it around and don't need runtime length:
Q2
Not without wrapping the array into another type.
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: