473,581 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick question on smart pointers

Hi,

I'm just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined as ff:

typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client
I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}

Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can't imagine how
this is possible)
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?

Thanks

Jul 23 '05 #1
27 3378
Susan Baker wrote:

Hi,

I'm just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined as ff:

typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client

I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}
Well, You could do that. But it would probably be easier to change it to

typedef std::vector< float > Series;

cause the whole structure looks extremely like the C way to code a dynamicaly
resizable array.

Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
You can do it.
But I recommend to first take a step back and ask a question: "What is the purpose
of that struct?" and "Is there a common C++ method to achieve that purpose?"
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can't imagine how
this is possible)
It is done easily:
A std::auto_ptr is an object like any other object. When it goes out of scope, its
destructor is called. And now guess what that destructor does.
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?


You are looking for a cook book recipe. I don't think you will find one.
This is what makes programming sometimes frustrating but on the other hand
this is what makes programming interesting: "One size fits all" does seldome
apply in programming.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #2
> I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}
This is very error prone. Consider:

Series s1;
Series s2 = s1; //!!!
Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
Do you really want/need to use a smart pointer? Just use plain RAII
with private copy constructor and assingment operator.
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can't imagine how
this is possible)
The magic is called destructor (it's magic only for Java programmers).
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?


If you maintain the 'creator is deleter' rule many 'problems' disappear
in C++.

Jul 23 '05 #3
Susan Baker <sb****@no.spam .net> schrieb:
typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client

I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}


I don't understand what you are planning. Why not use a real class
instead of the series struct? What should the auto_ptr do here?

I suggest not to use this auto_ptr stuff. This is for people who's
right hand doesn't know what the left hand is doing. You can always
have a simple pointer in your class and delete it yourself. This is
what destructors are made for.

T.M.
Jul 23 '05 #4
typedef struct {
float *data ;
int count ;

} series ;

I am wondering as, what is the play of this count variable in
struct?...If it is counting number of object instanciated then better
make it static.
Panjandrum wrote:
I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}


This is very error prone. Consider:

Series s1;
Series s2 = s1; //!!!
Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)


Do you really want/need to use a smart pointer? Just use plain RAII
with private copy constructor and assingment operator.
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can't imagine how
this is possible)


The magic is called destructor (it's magic only for Java programmers).
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?


If you maintain the 'creator is deleter' rule many 'problems' disappear
in C++.


Jul 23 '05 #5
Geo


Torsten Mueller wrote:
Susan Baker <sb****@no.spam .net> schrieb:
typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client

I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}


I don't understand what you are planning. Why not use a real class
instead of the series struct? What should the auto_ptr do here?

I suggest not to use this auto_ptr stuff. This is for people who's
right hand doesn't know what the left hand is doing. You can always
have a simple pointer in your class and delete it yourself. This is
what destructors are made for.

T.M.


Surely auto_ptr is the last thing to use if you don't know what you're
doing ??

Jul 23 '05 #6


Panjandrum wrote:
I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}

This is very error prone. Consider:

Series s1;
Series s2 = s1; //!!!

Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)

Do you really want/need to use a smart pointer? Just use plain RAII
with private copy constructor and assingment operator.

2). From my understanding of auto_ptr, variable sPtr gets freed
automagical ly when class Series goes out of scope (I can't imagine how
this is possible)

The magic is called destructor (it's magic only for Java programmers).

The "magic" I was referring to (which everyone seems to have missed -
perhaps I did not make my self clear enough) was this: how can
auto_pointer correctly free memory allocated for a complicated structure
like client - which has several pointer members. It is my feeling that I
am misinterpreting the use(abuse?) of the auto_pointer and I just needed
some clarification on this point. The way I see it, using auto_pointer
in this way will lead to serious memory leaks - unless the container is
made aware of the details of the object (struct in this case), that it
is acting as a container of. This is what I mean when I say I cannot
imagine how the container finds out how to free the appropriate pointers
in the structure. The more I think abot it, the more it seems like I got
the wrong end of the stick. I don't think auto_pointers can be used this
way without memory leaks (although
http://www.parashift.com/c++-faq-lit....html#faq-17.4 appears
to suggest just that). I'll have to take another read to be sure....

3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?

If you maintain the 'creator is deleter' rule many 'problems' disappear
in C++.


True - but then what is the point of the container?. I *thought* the
container was "managing" mem resources for me - I can just as quickly
roll my own "container" using an array. What "exactly" makes it a "safe
pointer".

Jul 23 '05 #7
>> I suggest not to use this auto_ptr stuff. This is for people who's
right hand doesn't know what the left hand is doing. You can always
have a simple pointer in your class and delete it yourself. This is
what destructors are made for.


That's just not true. If you want your code to be exception safe then
naked pointers are very tricky. It's unfortunate that the majority of
the C++ crowd doesn't think this way but smart pointers (not
necessarily std::auto_ptr) should be used instead of naked pointer
unless there's a good reason not to, NOT the other way around.

Jul 23 '05 #8


ba***********@g mail.com wrote:
I suggest not to use this auto_ptr stuff. This is for people who's
right hand doesn't know what the left hand is doing. You can always
have a simple pointer in your class and delete it yourself. This is
what destructors are made for.

That's just not true. If you want your code to be exception safe then
naked pointers are very tricky. It's unfortunate that the majority of
the C++ crowd doesn't think this way but smart pointers (not
necessarily std::auto_ptr) should be used instead of naked pointer
unless there's a good reason not to, NOT the other way around.


So briefly, what do they buy me over and above naked pointers?, and how
may I use them in the example stucture I provided previously? (using the
simpler struct "series" as an example - if you please).

Thanks

Jul 23 '05 #9
Am Wed, 06 Jul 2005 13:32:36 +0200 schrieb Susan Baker
<sb****@no.spam .net>:
Hi,

I'm just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined as ff:

typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client
I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_point er<series> sPtr ;
....
}

Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
Yes
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can't imagine how
this is possible)
The auto_ptr-Object is a 'normal' object on the stack. When it get's out
of scope it's destructor gets called. The dtor deletes the owned object
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?


Yes. But be aware the ownership-policy of auto_ptr. The ownership of the
pointer is passed, if the auto_ptr gets copied. The other forgets about
the pointer. Thus, one pointer belongs allways to exactly one auto_ptr. So
auto_ptr is in common not helpful if you want to copy your
wrapper-classes. If so, use boost/shared_ptr (www.boost.org)
--
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
Jul 23 '05 #10

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

Similar topics

8
5127
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very...
92
5038
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, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5036
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 actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
54
11925
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 the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously...
0
7873
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8178
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3830
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1405
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1141
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.