动态改变C++中指针的大小

发布于 2024-12-21 14:40:52 字数 314 浏览 6 评论 0原文

我有以下结构

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

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

发布评论

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

评论(8

不及他 2024-12-28 14:40:52

你不能用常规数组做到这一点。我建议您使用 向量 它能够随着您添加更多元素而增长它(所以你甚至不必指定初始大小)。

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

枯寂 2024-12-28 14:40:52

好的 C++ 方法是为此使用适当的容器。显然,您应该使用 std::vector 容器,例如:

std::vector<DeviceInfo> m_DeviceInfo;
m_DeviceInfo.resize(4);

这需要一些约束在您的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:

std::vector<DeviceInfo> m_DeviceInfo;
m_DeviceInfo.resize(4);

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.

雾里花 2024-12-28 14:40:52

m_DeviceInfo 指向由 4 个元素组成的 DeviceInfo 数组。数组不会调整大小。相反,您应该删除并用 6 个元素创建它。

DeviceInfo * m_DeviceInfo2 = new DeviceInfo[6]; 
memcopy( m_DeviceInfo,m_DeviceInfo2, 4 );
delete[] m_DeviceInfo;

但你应该使用向量。

std::vector<DeviceInfo> m_DeviceInfo;
// or 
std::vector<std::shared_ptr<DeviceInfo>> m_DeviceInfo;

调整它的大小

m_DeviceInfo.resize(m_DeviceInfo.size()+ 2);

m_DeviceInfo points to an array of DeviceInfo of 4 elements. There is no resizing with arrays. Instead you should delete and create it with 6 elements.

DeviceInfo * m_DeviceInfo2 = new DeviceInfo[6]; 
memcopy( m_DeviceInfo,m_DeviceInfo2, 4 );
delete[] m_DeviceInfo;

But you should use a vector.

std::vector<DeviceInfo> m_DeviceInfo;
// or 
std::vector<std::shared_ptr<DeviceInfo>> m_DeviceInfo;

To resize it

m_DeviceInfo.resize(m_DeviceInfo.size()+ 2);
柳若烟 2024-12-28 14:40:52

您的问题有两个选择,这取决于您是否想使用 STL。

typedef struct DeviceInfo
{
   char[30] name;
   char[30] serial Number;

} DeviceInfo;

使用 STL:

//requires vector.h
vector<DeviceInfo> m_deviceInfo;

DeviceInfo dummy;
dummy.name = "dummyName";
dummy.serialNumber = "1234"; 

m_deviceInfo.insert(m_deviceInfo.begin(), dummy); 
//add as many DeviceInfo instance you need the same way

或不使用 STL:

//implement this 
DeviceInfo* reallocArray(DeviceInfo* arr, int curItemNum, int newItemNumber)
{
   DeviceInfo* buf = new DeviceInfo[newItemNumber];

   for(int i = 0; i < curItemNum; i++)
     buf[i] = arr[i];

   for(int i = curItemNum; i < newItemNumber; i++)
     buf[i] = null;
}

//and in your main code
DeviceInfo m_DeviceInfo = new DeviceInfo[4];

m_DeviceInfo = reallocArray( m_DeviceInfo, 4, 6 );

You have two options in your problem and it depends if you want to use STL or not.

typedef struct DeviceInfo
{
   char[30] name;
   char[30] serial Number;

} DeviceInfo;

With STL:

//requires vector.h
vector<DeviceInfo> m_deviceInfo;

DeviceInfo dummy;
dummy.name = "dummyName";
dummy.serialNumber = "1234"; 

m_deviceInfo.insert(m_deviceInfo.begin(), dummy); 
//add as many DeviceInfo instance you need the same way

or without STL:

//implement this 
DeviceInfo* reallocArray(DeviceInfo* arr, int curItemNum, int newItemNumber)
{
   DeviceInfo* buf = new DeviceInfo[newItemNumber];

   for(int i = 0; i < curItemNum; i++)
     buf[i] = arr[i];

   for(int i = curItemNum; i < newItemNumber; i++)
     buf[i] = null;
}

//and in your main code
DeviceInfo m_DeviceInfo = new DeviceInfo[4];

m_DeviceInfo = reallocArray( m_DeviceInfo, 4, 6 );
っ左 2024-12-28 14:40:52

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

怎会甘心 2024-12-28 14:40:52

最好的解决方案是在程序中使用向量

请参阅此站点 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.

岛歌少女 2024-12-28 14:40:52

您的语法错误:

DeviceInfo m_DeviceInfo = new DeviceInfo[4];

应该是:

DeviceInfo* m_DeviceInfo = new DeviceInfo[4];

更好的替代方案是使用 std::vector

std::vector<DeviceInfo> vec;

//populate:
DeviceInfo inf;
vec.push_back(inf);
vec.push_back(inf);
//....

Your syntax is wrong:

DeviceInfo m_DeviceInfo = new DeviceInfo[4];

should be:

DeviceInfo* m_DeviceInfo = new DeviceInfo[4];

A better alternative would be the use of std::vector.

std::vector<DeviceInfo> vec;

//populate:
DeviceInfo inf;
vec.push_back(inf);
vec.push_back(inf);
//....
不疑不惑不回忆 2024-12-28 14:40:52

有几种方法可以做到这一点,但是
你应该使用c++中的realloc函数。它将重新分配连续的内存,并将先前内存的值复制到新的内存中。例如:

temp_DevInfo = (DeviceInfo*) realloc (m_DeviceInfo, (2) * sizeof(struct DeviceInfo));
free(m_DeviceInfo);
m_deviceInfo = temp_DevInfo;

你执行 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:

temp_DevInfo = (DeviceInfo*) realloc (m_DeviceInfo, (2) * sizeof(struct DeviceInfo));
free(m_DeviceInfo);
m_deviceInfo = temp_DevInfo;

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.

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