Solved

Pointer, point to pointer, again

Posted on 2007-11-25
3
140 Views
Last Modified: 2010-04-01
Hi everyone,

Sorry to beat this subject to death, I want to make sure I am right in doing this. Here's my setup:

struct ThreadData {
    ThreadData (Connection* pConnection)
    {
         m_pConnection = pConnection;
    }
    Connection* m_pConnection;
    // some other junk here too.
};

int main()
{
     while (connections)
     {
          Connection* pConnection  =  0;
          ThirdPartyLibConnectFunction(pConnection); // this calls malloc inside on pConnection.

           // Now I want a thread to handle the connection.
           ThreadData* pThreadData = new ThreadData(pConnection);
           RunThread(pThreadData);
      }
}

void RunThread(void* pParam)
{
     auto_ptr<ThreadData*> pData((ThreadData*)pParam);

     // Do stuff with the connection object now.
     pData->m_pConnection->Whatever();
}
         
My question is, is my use of pointers here doing what I want? (tought to read my mind I know). Ok I want the data structure allocated in the main thread to be used only by the handler thread, and to be cleaned up in the thread handler. I think this is doing it right?:

     m_pConnection = pConnection;

So the actual memory is being allocated in the main thread for it, but the main thread cannot touch it anymore after the current iteration of the while loop goes through. Then the handler thread can play with it however it wants to and can deallocate it properly.

Is this correct?

Thanks for your help as always.
0
Comment
Question by:DJ_AM_Juicebox
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20346865
>>Is this correct?

In short: Yes.

Hard to add anything, you wrote it all.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20346893
Alright thanks just needed someone to proof read it,

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 20346944
You're most welcome ;o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now