
----Original Message---- From: H. van Niekerk [mailto:h.v.niekerk@hccnet.nl] Sent: 20 June 2005 19:22 To: boost@lists.boost.org Subject: [boost] shared_ptr question
Hi,
I'm rather new to shared_ptr and I'm not very experienced in programmer with C++ but have to do it anyway. My problem is this: I have 3 .cpp files, e.g. Master.cpp, Client.cpp and Satellite.cpp.
Originally, Master.cpp is the only .cpp file, and uses auto_ptr to enable writing and reading from an interface. Like this:
{ .... auto_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); fti->getIfInfo(ii); IfOutputs out; // to set interface outputs (ctor clears // everything to 0) IfInputs inp; // to be read from interface fti->writeAndReadAllData(out, inp); ... }
This works well with every action taking place in this file. Now, as said I have those 3 .cpp files. Master.cpp sends an argument to Client.cpp and Client.cpp sends it to Satellite.cpp. Satellite.cpp reads from the interface, and sends it back via Client.cpp to Master.cpp. I can't use auto_ptr with this and from the Boost-documentation I can't figure out exactly how to use shared_ptr for this. Can somebody please help me out?
I hope I explained my problem clear enough.
I'm afraid you didn't. Could you write some sample (pseudo-) code just using raw C pointers and not worrying about memory leaks? Then we can probably work out how to rewrite using smart pointers. Incidentally, I don't think the presence of three .cpp files is directly relevent. I suspect that the relevent factor is that you have three *functions*, and your problem is passing the data between them. -- Martin Bonner Martin.Bonner@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 441434

Martin Bonner wrote:
----Original Message---- From: H. van Niekerk [mailto:h.v.niekerk@hccnet.nl] Sent: 20 June 2005 19:22 To: boost@lists.boost.org Subject: [boost] shared_ptr question
Hi,
I'm rather new to shared_ptr and I'm not very experienced in programmer with C++ but have to do it anyway. My problem is this: I have 3 .cpp files, e.g. Master.cpp, Client.cpp and Satellite.cpp.
Originally, Master.cpp is the only .cpp file, and uses auto_ptr to enable writing and reading from an interface. Like this:
{ .... auto_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); fti->getIfInfo(ii); IfOutputs out; // to set interface outputs (ctor clears // everything to 0) IfInputs inp; // to be read from interface fti->writeAndReadAllData(out, inp); ... }
This works well with every action taking place in this file. Now, as said I have those 3 .cpp files. Master.cpp sends an argument to Client.cpp and Client.cpp sends it to Satellite.cpp. Satellite.cpp reads from the interface, and sends it back via Client.cpp to Master.cpp. I can't use auto_ptr with this and from the Boost-documentation I can't figure out exactly how to use shared_ptr for this. Can somebody please help me out?
I hope I explained my problem clear enough.
I'm afraid you didn't. Could you write some sample (pseudo-) code just using raw C pointers and not worrying about memory leaks? Then we can probably work out how to rewrite using smart pointers.
Incidentally, I don't think the presence of three .cpp files is directly relevent. I suspect that the relevent factor is that you have three *functions*, and your problem is passing the data between them.
Thank you for your response. I hope the code below explains more: Master.cpp { Client c; c.readFromSatellite(input); cout << "input = " << input << endl; } Client.cpp { Satellite s; readFromSatellite(input) { s.readFromInterface(input); return input; } } Satellite.cpp { readFromInterface(input) { // Here the actual reading is done fti->writeAndReadAllData(output, input); return input; } }

On 6/21/05, H <h.v.niekerk@hccnet.nl> wrote:
Thank you for your response. I hope the code below explains more:
I don't think it does. You're leaving out far too much information about the types of things and the prototypes of the functions you are calling. There's no way to tell what is going on here. -- Caleb Epstein caleb dot epstein at gmail dot com

From: H <h.v.niekerk@hccnet.nl>
Martin Bonner wrote:
From: H. van Niekerk [mailto:h.v.niekerk@hccnet.nl]
Incidentally, I don't think the presence of three .cpp files is directly relevent. I suspect that the relevent factor is that you have three *functions*, and your problem is passing the data between them.
Thank you for your response. I hope the code below explains more:
Master.cpp { Client c; c.readFromSatellite(input); cout << "input = " << input << endl; }
Client.cpp { Satellite s;
readFromSatellite(input) { s.readFromInterface(input); return input; } }
Satellite.cpp { readFromInterface(input) { // Here the actual reading is done fti->writeAndReadAllData(output, input); return input; } }
As Martin said, the separate files aren't relevant. Also, the usage and declarations of your functions aren't consistent. Let's see if I can recast your problem to something a little simpler to discuss: void function2(some_type data) // stores stuff in caller's object { read_something_from_somewhere(data); } void function1(some_type data) // stores stuff in caller's object { function2(data); } void function3(some_type data) { // do something useful with data } int main() { some_type data; // some pointer type function1(data); // stores stuff in data function3(data); } Is that a good approximation of what you want? If so, some_type should be boost::shared_ptr<something> and the rest should just work. Of course, you can avoid using boost::shared_ptr if function1() and function2() take non-const references to some_type: void function2(some_type & data) // stores stuff in caller's object { read_something_from_somewhere(data); } void function1(some_type & data) // stores stuff in caller's object { function2(data); } void function3(some_type const & data) { // do something useful with data } int main() { some_type data; // not a pointer type function1(data); // stores stuff in data function3(data); } -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Let's see if I can recast your problem to something a little simpler to discuss:
void function2(some_type data) // stores stuff in caller's object { read_something_from_somewhere(data); }
void function1(some_type data) // stores stuff in caller's object { function2(data); }
void function3(some_type data) { // do something useful with data }
int main() { some_type data; // some pointer type function1(data); // stores stuff in data function3(data); }
Is that a good approximation of what you want?
Yes, that looks like it.
If so, some_type should be boost::shared_ptr<something> and the rest should just work. Of course, you can avoid using boost::shared_ptr if function1() and function2() take non-const references to some_type:
void function2(some_type & data) // stores stuff in caller's object { read_something_from_somewhere(data); }
void function1(some_type & data) // stores stuff in caller's object { function2(data); }
void function3(some_type const & data) { // do something useful with data }
int main() { some_type data; // not a pointer type function1(data); // stores stuff in data function3(data); }
OK. The original way was: auto_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); With shared_ptr would it become like this: void function2(boost::shared_ptr fti (ftGetIfFromFactory(device, connection)) { ... fti->writeAndReadAllData(out,inp); } void function1(boost::shared_ptr fti (ftGetIfFromFactory(device, connection)) { function2(data); } int main() { boost::shared_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); function1(fti (ftGetIfFromFactory(device, connection)); } What would be the benefit of using non-const references? Thank you for helping. Huub

From: H <h.v.niekerk@hccnet.nl>
Let's see if I can recast your problem to something a little simpler to discuss:
void function2(some_type data) // stores stuff in caller's object { read_something_from_somewhere(data); }
void function1(some_type data) // stores stuff in caller's object { function2(data); }
void function3(some_type data) { // do something useful with data }
int main() { some_type data; // some pointer type function1(data); // stores stuff in data function3(data); }
Is that a good approximation of what you want?
Yes, that looks like it.
If so, some_type should be boost::shared_ptr<something> and the rest should just work. Of course, you can avoid using boost::shared_ptr if function1() and function2() take non-const references to some_type:
void function2(some_type & data) // stores stuff in caller's object { read_something_from_somewhere(data); }
void function1(some_type & data) // stores stuff in caller's object { function2(data); }
void function3(some_type const & data) { // do something useful with data }
int main() { some_type data; // not a pointer type function1(data); // stores stuff in data function3(data); }
OK. The original way was:
auto_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection));
With shared_ptr would it become like this:
void function2(boost::shared_ptr fti (ftGetIfFromFactory(device, connection)) { ... fti->writeAndReadAllData(out,inp); }
Absolutely not. I'm not sure where you got the idea that you should somehow get the object in the formal parameter list, but that's for the object passed from the caller.
void function1(boost::shared_ptr fti (ftGetIfFromFactory(device, connection)) { function2(data); }
int main() { boost::shared_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); function1(fti (ftGetIfFromFactory(device, connection)); }
That should be something like this: void function1(boost::shared_ptr<FtInterface> fti) { function2(fti); } int main() { boost::shared_ptr<FtInterface> fti(ftGetIfFromFactory(...)); function1(fti); } Do you see that "boost::shared_ptr<FtInterface>" appears everwhere I wrote "some_type" previously in this version and not in yours?
What would be the benefit of using non-const references?
I told you that already. You don't need pointers (since you're using a factory function to create the instance, you'll need one pointer, but the other uses don't have to be pointers). void function1(FtInterface & fti) { function2(fti); } int main() { std::auto_ptr<FtInterface> fti(ftGetIfFromFactor(...)); function1(*fti); } The bottom line is that your problems stem not from boost::shared_ptr usage but from fundamental misunderstandings of how C++ works. The Boost Users List or comp.lang.c++.moderated are better places to handle such questions. It also appears that you need to find a good tutorial on C++ to help you understand things better. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Rob Stewart <stewart@sig.com> writes: <snip long quotation>
Absolutely not. I'm not sure where you got the idea that you should somehow get the object in the formal parameter list, but that's for the object passed from the caller.
Please limit the amount of quoted text in your replies. -- Dave Abrahams Boost Consulting www.boost-consulting.com

From: David Abrahams <dave@boost-consulting.com>
Rob Stewart <stewart@sig.com> writes:
<snip long quotation>
Absolutely not. I'm not sure where you got the idea that you should somehow get the object in the formal parameter list, but that's for the object passed from the caller.
Please limit the amount of quoted text in your replies.
I do. I thought the context was appropriate in that case. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

I told you that already. You don't need pointers (since you're using a factory function to create the instance, you'll need one pointer, but the other uses don't have to be pointers).
void function1(FtInterface & fti) { function2(fti); }
int main() { std::auto_ptr<FtInterface> fti(ftGetIfFromFactor(...)); function1(*fti); }
Thank you for this. Question tho: when function1 is in a different .cpp file than main(), I would still have to declare FtInterface in that .cpp file, right?
The bottom line is that your problems stem not from boost::shared_ptr usage but from fundamental misunderstandings of how C++ works. The Boost Users List or comp.lang.c++.moderated are better places to handle such questions. It also appears that you need to find a good tutorial on C++ to help you understand things better.
As I pointed out when I started this thread, I'm not very experienced and have been using about 6 different (online) tutorials about C++. Thank you for your help. H.

From: H <h.v.niekerk@hccnet.nl>
Thank you for this. Question tho: when function1 is in a different .cpp file than main(), I would still have to declare FtInterface in that .cpp file, right?
FtInterface.h: #ifndef _included_FtInterface_h #define _included_FtInterface_h struct FtInterface { ... }; #endif function1.cpp: #include "FtInterface.h" void function1(FtInterface & fti) { function2(fti); } main.cpp: #include <memory> #include "FtInterface.h" int main() { std::auto_ptr<FtInterface> fti(...); function1(*fti); }
are better places to handle such questions. It also appears that you need to find a good tutorial on C++ to help you understand things better.
As I pointed out when I started this thread, I'm not very experienced and have been using about 6 different (online) tutorials about C++.
I was confirming that the problem wasn't with shared_ptr, but was more fundamental. BTW, don't look at the implementation of Boost libraries to help you figure out C++. They are too advanced for that purpose.
Thank you for your help.
No problem. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Rob Stewart wrote:
From: H <h.v.niekerk@hccnet.nl>
Martin Bonner wrote:
From: H. van Niekerk [mailto:h.v.niekerk@hccnet.nl]
Incidentally, I don't think the presence of three .cpp files is directly relevent. I suspect that the relevent factor is that you have three *functions*, and your problem is passing the data between them.
Thank you for your response. I hope the code below explains more:
Master.cpp { Client c; c.readFromSatellite(input); cout << "input = " << input << endl; }
Client.cpp { Satellite s;
readFromSatellite(input) { s.readFromInterface(input); return input; } }
Satellite.cpp { readFromInterface(input) { // Here the actual reading is done fti->writeAndReadAllData(output, input); return input; } }
As Martin said, the separate files aren't relevant. Also, the usage and declarations of your functions aren't consistent.
Let's see if I can recast your problem to something a little simpler to discuss:
void function2(some_type data) // stores stuff in caller's object { read_something_from_somewhere(data); }
void function1(some_type data) // stores stuff in caller's object { function2(data); }
void function3(some_type data) { // do something useful with data }
int main() { some_type data; // some pointer type function1(data); // stores stuff in data function3(data); }
Is that a good approximation of what you want?
If so, some_type should be boost::shared_ptr<something> and the rest should just work. Of course, you can avoid using boost::shared_ptr if function1() and function2() take non-const references to some_type:
void function2(some_type & data) // stores stuff in caller's object { read_something_from_somewhere(data); }
void function1(some_type & data) // stores stuff in caller's object { function2(data); }
void function3(some_type const & data) { // do something useful with data }
int main() { some_type data; // not a pointer type function1(data); // stores stuff in data function3(data); }
After reviewing the groups on this news-server, I think I'm posting to the wrong group. If you agree with that view, please let me know. I'll follow up to the user group then. H.

H <h.v.niekerk@hccnet.nl> writes: <snip long quoted section>
After reviewing the groups on this news-server, I think I'm posting to the wrong group. If you agree with that view, please let me know. I'll follow up to the user group then.
That might be more appropriate, but on whichever group you choose to post, please try to limit the amount of quoted text in your postings. -- Dave Abrahams Boost Consulting www.boost-consulting.com

From: H <h.v.niekerk@hccnet.nl>
After reviewing the groups on this news-server, I think I'm posting to the wrong group. If you agree with that view, please let me know. I'll follow up to the user group then.
Yep. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
participants (5)
-
Caleb Epstein
-
David Abrahams
-
H
-
Martin Bonner
-
Rob Stewart