[英]How to increase struct array size at runtime
I have a structure and want to increase an array size whenever SendMessage function calls 我有一个结构,想在每次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;
}
In C you will have to deal with dynamic memory (ie allocate the array using malloc()
, then take care to call free()
when you stop using it etc.) yourself, and possibly use realloc()
to grow an allocation. 在C语言中,您必须自己处理动态内存(即,使用
malloc()
分配数组,然后在停止使用它时小心调用free()
等),并可能使用realloc()
来增加分配。
In C++ the problem is already solved for you and you have std::vector
. 在C ++中,该问题已经为您解决,您拥有了
std::vector
。 You may call push_back
to add elements dynamically to it. 您可以调用
push_back
向其中动态添加元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.