k**l 发帖数: 2966 | 1 I use a queue (deque) to store some broadcast messages for later process.
All messages were casted to char array with certain length.
say when I got a new broadcast with length n;
char* bcbuffer = new char [n];
memcpy(bcbuffer,broadcast,n);
m_broadcastQue.push(std::pair(bcbuffer,n));
Later when I process it:
char* bcbuffer =m_broadcastQue.front().first;
int n=m_broadcastQue.front().second;
m_broadcastQue.pop();//cut the link to free queue lock
processOneBroadCast(bcbuffer, n);
delete [] bcbuffer;
My question is: will the last line of code work---if it doesn't, how do I
tell the process the size of to be delete array is n?
Note: I skipped mutex lock and conditional wait stuff to make my question
easy to read. | t****t 发帖数: 6806 | 2 yes, it will, as long as there is no race condition. | i***c 发帖数: 26 | 3 delete [] bcbuffer and delete bcbuffer are the same here I think, for char
needn't deconstruction function. | t****t 发帖数: 6806 | 4 just don't do that.
【在 i***c 的大作中提到】 : delete [] bcbuffer and delete bcbuffer are the same here I think, for char : needn't deconstruction function.
|
|