As far as I know, the near pointers (or not far pointers) in C/C++ have address value which is very small than the actual address in RAM. So, if I keep on incrementing a pointer (say int type or any object type pointer) then at particular value it will be roll over. Now, my question is that: Is after rolling back the value pointed by it valid or not (assuming, I have a large size of data in the memory)?
I know this is a strange question to ask but I have a situation where I am continuously allocating and deallocating memory. I am finding that at particular point the binary crashes due to invalid address value like
0x20
, 0x45
or 0x10101
etc.
I was wondering that the issue is due to roll over of the pointer value and since the address is getting rollover due to pointer therefore it is showing invalid address and crashing when it is being accessed.
I hope the situation I am referring is similar to the question is being asked. Even if they are different, I would like to know answers to both. I tried searching on "continuous incrementing pointers" but didn't find my answer.
EDIT: This is a new code compiled with G++ 4.1.2 20080704 (Red Hat 4.1.2-48) on Red Hat linux.
Actually the code is very large to share. But I can brief it in words: There are 3 threads:
- First thread: It creates allocates Alert class object and pushes it into the queue.
- Second thread: It reads Alert from the queue, process it.
- Third thread: It release the memory allocated to Alert objects after 20-30 minutes of processing.
I have already verified that the 3rd thread is not deallocating it before processed by 2nd thread.
But since the Alerts are generated on regular basis (i.e. around Thousands in a second) so I was suspecting the issue mentioned in the main question. Points to note in my implementation: I am using linux pipe queue to push it from one thread to other. For that I am pushing only address value of the object from sender side, and ensured to not delete the object there. Is this a possible way of corruption? Following is the code of this particular task:
Alert* l_alert = new Alert(ADD_ACTION,
l_vehicleType,
l_vehicleNo,
l_projPolyline,
l_speed,
l_slotId);
m_ResultHandler->SendToWorker(&l_alert);
Implementation of queue functions:
S32 SendToWorker(queueDataType *p_instPtr)
{
S32 ret_val=SUCCESS;
QueueObj.Lock();
ret_val = QueueObj.Signal();
QueueObj.push(*p_instPtr);
QueueObj.UnLock();
return ret_val;
}
S32 GetFromReceiver(queueDataType *p_instPtr)
{
QueueObj.Lock();
while(QueueObj.size() == 0)
QueueObj.Wait();
*p_instPtr = QueueObj.front();
QueueObj.pop();
QueueObj.UnLock();
return SUCCESS;
}
Receiver End:
m_alertQueue->GetFromReceiver(&l_alert)