?
Solved

Pointer, point to pointer, again

Posted on 2007-11-25
3
Medium Priority
?
147 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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