Solved

Pointer, point to pointer, again

Posted on 2007-11-25
3
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

695 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