c++ - 如何在运行时增加结构数组大小

标签 c++ c

我有一个结构,想在调用 SendMessage 函数时增加数组大小

struct MQStruct {
    wchar_t *serviceName; 
    int durability; 
    int msgType; 
    int msgHeader; 
    wchar_t *msgId; 
    wchar_t *payload; 
    int payloadSize; 
    int ttl; 
    int priority;
}MQStructObj[1];


int SendMessage(wchar_t *serviceName, int durability, int msgType, int msgHeader, wchar_t *msgId, wchar_t *payload, int payloadSize, int ttl, int priority) {

//Want to add one more array object and also preserve data of previous
MQStructObj[MAX+1]

return 0;
}

最佳答案

在 C 中,您将不得不处理动态内存(即使用 malloc() 分配数组,然后在您停止使用它时注意调用 free()等)自己,并可能使用 realloc() 来增加分配。

在 C++ 中,问题已经为您解决,您拥有 std::vector。您可以调用 push_back 向其动态添加元素。

关于c++ - 如何在运行时增加结构数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14335127/

相关文章:

c++ - 当我尝试将范围值存储为8位固定宽度整数时,它向我显示了其他一些值[ASCII]

c - 如何从csv文件中获取特定行?

c - PIC18F2220 LED闪烁程序错误

c - 从字符串打印字符值时出错

c++ - 为什么两次删除指针会导致崩溃?

c# - C++ 中是否有 BinaryReader 来读取从 C# 中的 BinaryWriter 写入的数据?

c++ - 基本套接字服务器

c++ - 在初始化中使用新声明的变量 (int x = x+1)?

c - 如何使用最少数量的赋值运算符构建 OneTwoThree 链表?

c - 我不明白这个简单的循环如何改变文件指针位置