I am trying to build a messageQueue based around this messageStruct
struct MessageStruct{
public: std::string msg; // what is the msg
public: int prior; // how important is this msg
};
That lies in a MessageQueue that is needed to act like a vector/priorityQueue (self made version that is working fine), but I put a bunch of stuff on it, like so.
int size = 20;
// constructor takes number of entries to start with
MessageQueue * queue = new MessageQueue(size);
// used as a transport mechanism
MessageStruct * msg = new MessageStruct();
msg->prior = 0;
msg->msg = "queue exists";
queue->getMsg(msg);
// increasing size does not cause memory issue,
// use for example of dynamic size.
... other stuff is happening
size *= 2;
for (int ii = 0; ii < size; ii++){
msg->prior = 0;
msg->msg = "creating thing " + ii;
// puts this message in the queue,
// and increases queue size if needed
queue->getMsg(msg);
}
... // other stuff is happening
msg->prior = 10;
msg->msg = "program terminated correctly";
queue->queueDump();
delete msg;
msg = 0;
delete queue;
queue = 0;
This code gets no errors, and all memory is being released without issue, though the output of this looks like this (I generate the output in xml for sanity):
<message>
<index>0</index>
<msg>program terminated correctly</msg>
<priority>10</priority>
</message>
<message>
<index>1</index>
<msg>init(). queue exists</msg>
<priority>0</priority>
</message>
<message>
<index>2</index>
<msg>reating thing </msg>
<priority>0</priority>
</message>
<message>
<index>3</index>
<msg>eating thing </msg>
<priority>0</priority>
</message>
<message>
<index>4</index>
<msg>ating thing </msg>
<priority>0</priority>
</message>
<message>
<index>5</index>
<msg>ting thing </msg>
<priority>0</priority>
</message>
<message>
<index>6</index>
<msg>ing thing </msg>
<priority>0</priority>
</message>
<message>
<index>7</index>
<msg>ng thing </msg>
<priority>0</priority>
</message>
<message>
<index>8</index>
<msg>g thing </msg>
<priority>0</priority>
</message>
<message>
<index>9</index>
<msg> thing </msg>
<priority>0</priority>
</message>
<message>
<index>10</index>
<msg>thing </msg>
<priority>0</priority>
</message>
<message>
<index>11</index>
<msg>hing </msg>
<priority>0</priority>
</message>
<message>
<index>12</index>
<msg>ing </msg>
<priority>0</priority>
</message>
<message>
<index>13</index>
<msg>ng </msg>
<priority>0</priority>
</message>
<message>
<index>14</index>
<msg>g </msg>
<priority>0</priority>
</message>
<message>
<index>15</index>
<msg> </msg>
<priority>0</priority>
</message>
It looks like the strings are being generated and placed, but why are leading characters being removed?
needed to introduce a method to the code that converted an int to a string
then modify the line
to be
this can be costly to do in repetition, but the only other option is likely to call a string builder that doesn’t really save anything