Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy pointer question

Status
Not open for further replies.

EvilCabal

Programmer
Jul 11, 2002
206
CA
Hello,


I have a program with a thread that continuously check the parallel port for incoming data and after receiving a complete block of data it sends it to the main application. The data is send as an interger array, whose size depends on the data received.

The problem I have is that the thread sends the data as an event (using Qt customEvent class). So if I send the address of the array, there is always a possibilty the thread will erase it with new data from the port BEFORE the application process it. How can I pass a complete copy of the array and not only the address of the first cell (The size of the array is non constant, the first cell contains the size)

Right now here is the code I use :

if (DataReceived)
MyCustomEvent->setData(intArray)
postEvent(mainApp, MyCustomEvent)
end if

...
//As soon as new data is received
delete intArray
intArray = new int[Whatever size the new data is]

If you need more info tell me

Thanks.
 
Well your input thread only does
[tt] intArray = new int[Whatever size the new data is];[/tt]

At the moment it does
[tt] postEvent(mainApp, MyCustomEvent);[/tt]
it is no longer responsible for freeing that memory. It is the responsibility of the receiving thread to free the memory when it has finished with it.

Your main loop does this
[tt] delete [] array;[/tt]
when it has finished processing the data.

--
 
If you are using the Qt framework, you can use a QValueVector<int> object.
See trolltech's documentation at :

This class redefines operator=, allowing to copy QValueVector objects.
Code:
QValueVector<int> data;
data.push_back (50);
data.push_back (60);
//etc.

QValueVector<int> copy = data;//data is assigned to copy

You can pass a QValueVector<int> object to your event through procedure setData. Listeners of this event should make a copy of the vector passed in argument.
Code:
void ListenerClass::handle_custom_event (const CustomEvent& event)
{
   QValueVector<int> data = event.data ();//set previously by setData
   //do stuff with data
}
From the documentation you may read that :
<< QValueVector is shared implicitly, which means it can be copied in constant time( O(1) ). If multiple QValueVector instances share the same data and one needs to modify its contents, this modifying instance makes a copy and modifies its private copy; it thus does not affect the other instances. This is often called "copy on write". >>
Another solution you could consider is using a QMutex object to lock modification and access to the data.
I've never used it, but it might be worth looking at it :


--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top