473,387 Members | 1,700 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,387 software developers and data experts.

Indestructible pointer: Sample code for feedback

Hi all,

Most smart pointers remove the need to call delete. What I want is a
pointer that turns delete into a no-op for "unauthorized" clients.

The reason is that I'm generating code from pre-existing legacy code that
assumes the caller deletes the pointer. However, I want to reduce memory
copies for the case where there are multiple clients fetching a pointer to
the same memory.

Admittedly, this is a rare scenario, but I thought I'd share it with you
all for posterity and any feedback you can give me. (It seems like a bit
of a hack, especially if you notice that operator<< doesn't actually print
out the pointer.)

David

First, here's a toy program demonstrating the situation in the original code:

--------------%<---------------
#include <iostream>

using namespace std;

// Old code returned a copy for clients to delete
int* My_Library_To_Optimize()
{
static int *some_private_data = new int(10);

// Create a new copy for every caller, which is slow
return new int(*some_private_data);
}

int main()
{
int *p1 = My_Library_To_Optimize();

// This is the client code generated from legacy code. The client code
// can either delete the pointer, or pass it off to someone else to be
// deleted.
{
cout << *p1 << endl;
delete p1;
}

int *p2 = My_Library_To_Optimize();

// More client code
{
cout << *p2 << endl;
delete p2;
}

return 0;
}
--------------%<---------------

Here's the new code:

--------------%<---------------
#include <iostream>
#include "tough_ptr"

using namespace std;

// New code will return an indestructible pointer
tough_ptr<int>* My_Library_To_Optimize()
{
static int *some_private_data = new int(10);

// Copies of tough_ptr are cheaper
return new tough_ptr<int>(some_private_data);
}

int main()
{
tough_ptr<int> *p1 = My_Library_To_Optimize();

// Client code is unchanged! Delete does nothing!
{
cout << *p1 << endl;
delete p1;
}

tough_ptr<int> *p2 = My_Library_To_Optimize();

// Client code is unchanged! Delete does nothing!
{
cout << *p2 << endl;
delete p2;
}

return 0;
}
--------------%<---------------

Here's tough_ptr:

--------------%<---------------
#ifndef TOUGH_PTR_H
#define TOUGH_PTR_H

#include <iostream>

template<typename _Tp>
class tough_ptr
{
private:
_Tp* _M_ptr;

public:
explicit
tough_ptr(_Tp* __p = 0) throw() : _M_ptr(__p) { }

tough_ptr(tough_ptr& __a) throw() : _M_ptr(__a._M_ptr) { }

template<typename _Tp1>
tough_ptr(tough_ptr<_Tp1>& __a) throw() : _M_ptr(__a._M_ptr) { }

tough_ptr&
operator=(tough_ptr& __a) throw()
{
_M_ptr = __a._M_ptr;
return *this;
}

template<typename _Tp1>
tough_ptr&
operator=(tough_ptr<_Tp1>& __a) throw()
{
_M_ptr = __a._M_ptr;
return *this;
}

~tough_ptr() {}

const _Tp* get() const { return _M_ptr; }

_Tp&
operator*() const throw()
{
return *_M_ptr;
}

_Tp*
operator->() const throw()
{
return _M_ptr;
}

template<typename _Tp1>
operator tough_ptr<_Tp1>() throw()
{ return tough_ptr<_Tp1>(this->_M_ptr); }
};

template<typename _Tp>
std::ostream& operator<< (std::ostream& in_ostream, const tough_ptr<_Tp>&
in)
{
in_ostream << *(in.get());
return in_ostream;
}

#endif // TOUGH_PTR_H
--------------%<---------------
Jul 23 '05 #1
5 1458
Here's an updated version with more realistic code. Also, it's more
efficient because the returned pointer is not created on-the-fly. Note the
override of operator delete in tough_ptr.

David

Old code:

-------------------%<-------------------
#include <iostream>

using namespace std;

class Data_Feed
{
public:
Data_Feed() {
}

~Data_Feed() {
}

void Set_Data(const int in_data) {
some_private_data = in_data;
}

// Old code returned a copy for clients to delete
int* Get_Data() {
return new int(some_private_data);
}

private:
int some_private_data;
};

int main()
{
Data_Feed feeder;

feeder.Set_Data(1);

int *p1 = feeder.Get_Data();

// This is the client code generated from legacy code. The client code
// can either delete the pointer, or pass it off to someone else to be
// deleted.
{
cout << *p1 << endl;
delete p1;
}

feeder.Set_Data(2);

int *p2 = feeder.Get_Data();

// More client code
{
cout << *p2 << endl;
delete p2;
}

return 0;
}
-------------------%<-------------------

New code:

-------------------%<-------------------
#include <iostream>
#include "tough_ptr"

using namespace std;

class Data_Feed
{
public:
Data_Feed() {
return_ptr = new tough_ptr<int>(&some_private_data);
}

~Data_Feed() {
::delete(return_ptr);
}

void Set_Data(const int in_data) {
some_private_data = in_data;
}

// New code will return an indestructible pointer
tough_ptr<int>*& Get_Data() {
return return_ptr;
}

private:
tough_ptr<int> *return_ptr;
int some_private_data;
};

int main()
{
Data_Feed feeder;

feeder.Set_Data(1);

tough_ptr<int> *p1 = feeder.Get_Data();

// Client code is unchanged! Delete does nothing!
{
cout << *p1 << endl;
delete p1;
}

feeder.Set_Data(2);

tough_ptr<int> *p2 = feeder.Get_Data();

// Client code is unchanged! Delete does nothing!
{
cout << *p2 << endl;
delete p2;
}

return 0;
}
-------------------%<-------------------

tough_ptr code:

-------------------%<-------------------
#ifndef TOUGH_PTR_H
#define TOUGH_PTR_H

#include <iostream>

template<typename _Tp>
class tough_ptr
{
private:
_Tp* _M_ptr;

public:
explicit
tough_ptr(_Tp* __p = 0) throw() : _M_ptr(__p) { }

tough_ptr(tough_ptr& __a) throw() : _M_ptr(__a._M_ptr) { }

template<typename _Tp1>
tough_ptr(tough_ptr<_Tp1>& __a) throw() : _M_ptr(__a._M_ptr) { }

tough_ptr&
operator=(tough_ptr& __a) throw()
{
_M_ptr = __a._M_ptr;
return *this;
}

template<typename _Tp1>
tough_ptr&
operator=(tough_ptr<_Tp1>& __a) throw()
{
_M_ptr = __a._M_ptr;
return *this;
}

~tough_ptr() {}

const _Tp* get() const { return _M_ptr; }

_Tp&
operator*() const throw()
{
return *_M_ptr;
}

_Tp*
operator->() const throw()
{
return _M_ptr;
}

template<typename _Tp1>
operator tough_ptr<_Tp1>() throw()
{ return tough_ptr<_Tp1>(this->_M_ptr); }

static void operator delete(void *p){}
};

template<typename _Tp>
std::ostream& operator<< (std::ostream& in_ostream, const tough_ptr<_Tp>& in)
{
in_ostream << *(in.get());
return in_ostream;
}

#endif // TOUGH_PTR_H
-------------------%<-------------------
Jul 23 '05 #2
* David Coppit:

tough_ptr<int>* My_Library_To_Optimize()
{
static int *some_private_data = new int(10);

// Copies of tough_ptr are cheaper
return new tough_ptr<int>(some_private_data);
}

int main()
{
tough_ptr<int> *p1 = My_Library_To_Optimize();

// Client code is unchanged! Delete does nothing!
{
cout << *p1 << endl;
delete p1;
}

tough_ptr<int> *p2 = My_Library_To_Optimize();

// Client code is unchanged! Delete does nothing!
{
cout << *p2 << endl;
delete p2;
}

return 0;
}


How is this different from
#include <iostream>
using namespace std;

typedef int** IntPtrPtr;

IntPtrPtr My_Library_To_Optimize()
{
static int *some_private_data = new int(10);
return new IntPtrPtr(some_private_data);
}

int main()
{
IntPtrPtr p1 = My_Library_To_Optimize();

cout << *p1 << endl;
delete p1;

IntPtrPtr p2 = My_Library_To_Optimize();

cout << *p2 << endl;
delete p2;

return 0;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
On Fri, 18 Mar 2005, Alf P. Steinbach wrote:
How is this different from


I couldn't get your code to compile. I did a pointer to tough_ptr so that
its class delete would be called. Unfortunately, this approach doesn't
work in general, because you can't do things like ptr->begin() for STL
data types.

In hindsight, a better way might be to override global operator delete.
That way I can register plain pointers to be protected in some global data
store, and check it before deleting.

Unfortunately, I can't seem to get it to work right using g++ 3.3. Below
is sample code.

David

Here is the test program:

-------------%<------------------
#include <string>
#include <iostream>

using namespace std;

#include "protected_pointer.h"

int main()
{
string* indestructible = new string("testing");
protect_pointer( indestructible );

cout << "Before attempted delete: " << *indestructible << endl;

// Does nothing in operator delete
delete indestructible;

// Prints out junk and hangs (?!)
cout << "After attempted delete: " << *indestructible << endl;

unprotect_pointer( indestructible );
delete indestructible;

return 0;
}
-------------%<------------------

Here is the implementation of operator delete:

-------------%<------------------
#include <set>

#include "protected_pointer.h"

std::set< void* > undeletable_pointers;

void protect_pointer(void *a_pointer)
{
undeletable_pointers.insert(a_pointer);
}

void unprotect_pointer(void *a_pointer)
{
undeletable_pointers.erase(a_pointer);
}

void operator delete(void *a_pointer) throw()
{
if (undeletable_pointers.find(a_pointer) == undeletable_pointers.end())
free(a_pointer);
}
-------------%<------------------

Here is the header:

-------------%<------------------
#ifndef PROTECTED_POINTER_H
#define PROTECTED_POINTER_H

void protect_pointer(void *a_pointer);
void unprotect_pointer(void *a_pointer);

void operator delete(void *a_pointer) throw();

#endif // PROTECTED_POINTER_H
-------------%<------------------
Jul 23 '05 #4
* David Coppit:
On Fri, 18 Mar 2005, Alf P. Steinbach wrote:
How is this different from


I couldn't get your code to compile.


Replace 'new IntPtrPtr' with 'new int*'.

How is your code different?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
On Fri, 18 Mar 2005, Alf P. Steinbach wrote:
Replace 'new IntPtrPtr' with 'new int*'.

How is your code different?


Well, for one,

cout << *p1 << endl;

prints "10" not a pointer location. My goal is to have something that
looks and acts like a pointer to the client, but can't be deleted.

I investigated overloading global delete, but then realized that I can't
stop the destructor of a class from being called (although I can stop the
memory from being deallocated afterwards). So in cases where I'm trying to
protect an arbitrary pointer, this method won't work.

So I guess it's back to the tough_ptr method I was trying to get to work
earlier.

David
Jul 23 '05 #6

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

Similar topics

12
by: Yin99 | last post by:
Why am I still able to use this object even though I have deleted it? The Output I get is: Cat Constructs Cat De-structs //deleteing object Cat Eats. //My question- why does object still...
1
by: Rhino | last post by:
What is the correct way to notify the tech writers that one of the sample programs has errors in it? Unless I am mistaken, there is an error in the DtLob.java program in the V8.1 Java/JDBC...
4
by: kamran | last post by:
Hi I was wondering if it is possible to recast a pointer in C ? What I mean in detail is that I have compressed data coming from an instrument. These are mostly sample differences from the...
3
by: scorpion53061 | last post by:
Hi, I am looking for a code sample that communicates from a client to a remote Windows Service on another computer over the internet, that is the Windows Service performing instructions the...
7
by: morz | last post by:
i just search for a while in c++ groups but cannot find good answer. i have code like this: int **ptr; ptr = new char *; ptr = new int(5); ptr = new int(16);
7
by: randy1200 | last post by:
I have some buttons that increment and decrement the index of a DataGridView. The code below gets the updated index value. The code below highlights the first column of the current row. ...
14
by: Glen Dayton | last post by:
While looking at some old code I ran across a snippet that increments a pointer to access different members of a structure: .... struct Badness { std::string m_begin; std::string m_param1;...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
6
by: WT | last post by:
Hello, Using url rewritting and ajax.net, I tried to circumvent some potential problems with postback url using a code from a sample given by Scott. The idea is to use a control Adapter on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.