473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Smart pointer with the old code.

How to use smart pointers with a function which takes raw pointer
Jul 26 '08 #1
5 1410
raj s wrote:
How to use smart pointers with a function which takes raw pointer
This depends on the smart pointer type you are using. Are you talking
about boost::shared_ptr? If so, look for get() in the official
documentation:

http://www.boost.org/doc/libs/1_35_0...shared_ptr.htm
--
Christian Hackl
Jul 26 '08 #2
On Jul 26, 11:26*am, raj s <yesr...@gmail.comwrote:
How to use smart pointers with a function which takes raw pointer
Do you mean something like

void foo(char* arg1)
{
auto_ptr<char>(arg1)
//Do something
}
Jul 27 '08 #3
How to use smart pointers with a function which takes raw pointer

Just pass the raw pointer to the function (e.g. use get () or whatever
memeber function your particular smart pointer has to retrieve the raw
pointer it holds)

A well designed stand alone function should not delete the pointer (it
didn't allocated the object did it?) and since the object exists
before the function was called (and will after the function is called)
there is no need to manage it's memory.
Jul 27 '08 #4
On Jul 27, 10:02 pm, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:
How to use smart pointers with a function which takes raw pointer
Just pass the raw pointer to the function (e.g. use get () or whatever
memeber function your particular smart pointer has to retrieve the raw
pointer it holds)
A well designed stand alone function should not delete the
pointer (it didn't allocated the object did it?) and since the
object exists before the function was called (and will after
the function is called) there is no need to manage it's
memory.
That depends on what the function is supposed to do. If the
function's role involves deleting the object, it should delete
it. Otherwise, you'll end up with a lot of memory leaks.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 27 '08 #5
On Jul 27, 4:59*pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 27, 10:02 pm, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:
How to use smart pointers with a function which takes raw pointer
Just pass the raw pointer to the function (e.g. use get () or whatever
memeber function your particular smart pointer has to retrieve the raw
pointer it holds)
A well designed stand alone function should not delete the
pointer (it didn't allocated the object did it?) and since the
object exists before the function was called (and will after
the function is called) there is no need to manage it's
memory.

That depends on what the function is supposed to do. *If the
function's role involves deleting the object, it should delete
it. *Otherwise, you'll end up with a lot of memory leaks.

--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
You are correct. But I used the term "well designed". It strikes me
as not a very good design to have smart pointers (in presumably new
code)
call a function (presumably in old/legacy code) that deletes an
object.
I assumed the OP had or was at least striving for good design.
Jul 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.