我有一个结构,想在调用 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/