0

I have a structure and want to increase an array size whenever SendMessage function calls

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;
}
3
  • 7
    Use a std::vector. Commented Jan 15, 2013 at 9:59
  • This looks like a C question, is it? Commented Jan 15, 2013 at 10:00
  • and std::wstring instead of wchar_t*. Commented Jan 15, 2013 at 10:00

1 Answer 1

2

In C you will have to deal with dynamic memory (i.e 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.

In C++ the problem is already solved for you and you have std::vector. You may call push_back to add elements dynamically to it.

Sign up to request clarification or add additional context in comments.

4 Comments

realloc(), not reallocate() and malloc(), not alloc().
And this is C and C++, not c or c++.
@kmkaplan that is why I answered what are the OP's options in either case. If there is way to improve the answer please say so and I will try to do it.
@IvayloStrandjev Sorry, I must have been unclear. I mean that the typography of the names C and C++ is wrong in your answer. You write them in lower case and with code-like font instead of upper case and normal font. You wrote c where it is supposed to be C and c++ instead of C++. Just a minor nitpick anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.