Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,702 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,504 people online right now. Registration is fast and FREE... Join Now!




The amount of pointers to an address

 
Reply to this topicStart new topic

The amount of pointers to an address, c++

yogi
post 25 Dec, 2005 - 11:05 AM
Post #1


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Hi all,

Quick question is it possible to check the amount of pointers to a pointing to a particular address. If so whats the syntax

Thanks Yogi
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 25 Dec, 2005 - 07:42 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


Are you referring to the number of pointers generated by one application (namely the one which you are in) to the address, or are you asking if you can determine the total number of pointers from all running programs to a particular point in memory?
User is online!Profile CardPM

Go to the top of the page

yogi
post 26 Dec, 2005 - 09:40 AM
Post #3


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Hi thanks for your quick responce,

Yes im referring to the number of pointers generated by one application (namely the one which you are in) to the address.

A possible example to try to clarify wat i mean

int a = 1000;
int *p;
int *q;

p = &a;
q = &a;

So the number of pointers to a is 2;
Thanks Yogi
User is offlineProfile CardPM

Go to the top of the page

1lacca
post 26 Dec, 2005 - 03:02 PM
Post #4


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,822



Thanked 11 times
My Contributions


There is no built in mechanism for that in C/C++, but search for the "smart pointer" concept if you need it badly.
User is offlineProfile CardPM

Go to the top of the page

yogi
post 27 Dec, 2005 - 10:38 AM
Post #5


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Thanks, think the counted_ptr.h should do the job exactly.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 28 Dec, 2005 - 05:58 AM
Post #6


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Hey I don't have that header file.
Can you post it here?
User is offlineProfile CardPM

Go to the top of the page

yogi
post 28 Dec, 2005 - 11:22 AM
Post #7


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Sure i found it off the following website

http://ootips.org/yonat/4dev/smart-pointers.html

Haven't had a chance to implement it though.

here is the code:

/*
* counted_ptr - simple reference counted pointer.
*
* The is a non-intrusive implementation that allocates an additional
* int and pointer for every counted object.
*/

#ifndef COUNTED_PTR_H
#define COUNTED_PTR_H

/* For ANSI-challenged compilers, you may want to #define
* NO_MEMBER_TEMPLATES or explicit */

template <class X> class counted_ptr
{
public:
typedef X element_type;

explicit counted_ptr(X* p = 0) // allocate a new counter
: itsCounter(0) {if (p) itsCounter = new counter(p);}
~counted_ptr()
{release();}
counted_ptr(const counted_ptr& r) throw()
{acquire(r.itsCounter);}
counted_ptr& operator=(const counted_ptr& r)
{
if (this != &r) {
release();
acquire(r.itsCounter);
}
return *this;
}

#ifndef NO_MEMBER_TEMPLATES
template <class Y> friend class counted_ptr<Y>;
template <class Y> counted_ptr(const counted_ptr<Y>& r) throw()
{acquire(r.itsCounter);}
template <class Y> counted_ptr& operator=(const counted_ptr<Y>& r)
{
if (this != &r) {
release();
acquire(r.itsCounter);
}
return *this;
}
#endif // NO_MEMBER_TEMPLATES

X& operator*() const throw() {return *itsCounter->ptr;}
X* operator->() const throw() {return itsCounter->ptr;}
X* get() const throw() {return itsCounter ? itsCounter->ptr : 0;}
bool unique() const throw()
{return (itsCounter ? itsCounter->count == 1 : true);}

private:

struct counter {
counter(X* p = 0, unsigned c = 1) : ptr(p), count© {}
X* ptr;
unsigned count;
}* itsCounter;

void acquire(counter* c) throw()
{ // increment the count
itsCounter = c;
if © ++c->count;
}

void release()
{ // decrement the count, delete if it is 0
if (itsCounter) {
if (--itsCounter->count == 0) {
delete itsCounter->ptr;
delete itsCounter;
}
itsCounter = 0;
}
}
};

#endif // COUNTED_PTR_H
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 28 Dec, 2005 - 10:27 PM
Post #8


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Great.
Thanks.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 09:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month