动态改变C++中指针的大小
我有以下结构
typedef struct DeviceInfo
{
char[30] name;
char[30] serial Number;
}DeviceInfo;
I am doing this
DeviceInfo* m_DeviceInfo = new DeviceInfo[4];
// Populate m_DeviceInfo
然后我想将 m_DeviceInfo
大小重新调整为 6 并希望保留 前 4 值也是如此。
如何在 C++ 中做到这一点?
I have the following structure
typedef struct DeviceInfo
{
char[30] name;
char[30] serial Number;
}DeviceInfo;
I am doing this
DeviceInfo* m_DeviceInfo = new DeviceInfo[4];
// Populate m_DeviceInfo
Then I wanted to re size m_DeviceInfo
to 6 and want to preserve the
Previous 4 Value as well.
How to do it in c++ ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你不能用常规数组做到这一点。我建议您使用 向量 它能够随着您添加更多元素而增长它(所以你甚至不必指定初始大小)。
You can't do that with regular arrays. I suggest you to use vector which is able to grow as you add more elements to it (so you don't even have to specify initial size).
好的 C++ 方法是为此使用适当的容器。显然,您应该使用 std::vector 容器,例如:
这需要一些约束在您的DeviceInfo上。特别是,它应该有一个不带参数的构造函数和复制构造函数......
而且你的问题措辞很糟糕。您当然不会更改
sizeof(DeviceInfo*)
,它在 32 位机器上可能是 4 字节,在 64 位机器上可能是 8 字节。The good C++ way is to use an appropriate container for that. Apparently, you should use the std::vector container, e.g:
This requires some constraints on your DeviceInfo. In particular, it should have a constructor without arguments, and copy constructors...
And your question is badly phrased. You certainly don't change
sizeof(DeviceInfo*)
which is probably 4 bytes on a 32 bits machine, and 8 bytes on a 64 bits one.m_DeviceInfo
指向由 4 个元素组成的DeviceInfo
数组。数组不会调整大小。相反,您应该删除并用 6 个元素创建它。但你应该使用向量。
调整它的大小
m_DeviceInfo
points to an array ofDeviceInfo
of 4 elements. There is no resizing with arrays. Instead you should delete and create it with 6 elements.But you should use a vector.
To resize it
您的问题有两个选择,这取决于您是否想使用 STL。
使用 STL:
或不使用 STL:
You have two options in your problem and it depends if you want to use STL or not.
With STL:
or without STL:
1)创建一个大小合适的新数组,并将旧数组的所有元素复制到新数组中。
2)使用
std::vector
(我的推荐)。1) Make a new array of size that fits, and copy all elements of the old array to the new one.
2) Use the
std::vector
(my recommendation).最好的解决方案是在程序中使用向量。
请参阅此站点 http://www.yolinux.com/TUTORIALS/LinuxTutorialC++ STL.html#VECTOR
这个网站将帮助您解决您的问题。
在这里你可以推送数据。无需担心结构的大小。
The best possible solution is using vector in your program.
Refer this site http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR
This site will help you to solve your problem.
Here you can push the data.No need to bother about the size of structure.
您的语法错误:
应该是:
更好的替代方案是使用
std::vector
。Your syntax is wrong:
should be:
A better alternative would be the use of
std::vector
.有几种方法可以做到这一点,但是
你应该使用c++中的realloc函数。它将重新分配连续的内存,并将先前内存的值复制到新的内存中。例如:
你执行 2 * sizeof(DeviceInfo) 因为你想再添加 2 个,加上前面的 4 就是 6。
那么你应该释放/删除以前的对象。
最后设置旧指针指向刚刚分配的新对象。
这应该是它的要点,
请查看 realloc 的文档。
well there are several ways to do this, but
you should use the realloc function in c++. it will reallocate contiguous memory and also copies the value of previous memory into the new ones. for example:
you do 2 * sizeof(DeviceInfo) because you want to add 2 more, plus the previous 4 is 6.
then you should free/delete the previous object.
and finally set old pointer to point to the new object you just allocated.
that should be the gist of it
look at the documentation of realloc.