By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

A question about pointer

P: n/a
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Dec 12 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Bo Yang wrote:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
No. With freedom comes responsibility, but to reduce the risk of leaks,
you can avoid using raw pointers and arrays in favor RAII techniques:
never allocate a resource (e.g., memory, etc.) without assigning it to
an object that will automatically release it. One class that enables
such techniques is std::tr1::shared_ptr, and another is std::vector
(cf. http://parashift.com/c++-faq-lite/co...html#faq-34.1).

Cheers! --M

Dec 12 '06 #2

P: n/a

Bo Yang wrote:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.
Replace any allocation with the appropriate smart pointer and use STL
containers (which support copy semantics).
Replace pointers with references, prefereably - const references where
appropriate.

#include <iostream>
#include <ostream>
#include <vector>
#include <iterator>
#include <boost/shared_ptr.hpp>

template< typename T >
class A {
T t;
public:
// ctor
A(const T& val = T()) : t(val)
{
std::cout << "A()\n";
}
// copy ctor
A(const A& copy)
{
std::cout << "copy A\n";
t = copy.t;
}
// d~tor
~A() { std::cout << "~A()\n"; }
// friend op<<
friend std::ostream&
operator<<(std::ostream& os, const A& r_a)
{
return os << r_a.t;
}
};

int main() {
{
std::vector< A< double vd(5, 11.1); // 5 elements
std::copy( vd.begin(),
vd.end(),
std::ostream_iterator< A< double (std::cout, "\n") );
}

std::cout << "allocating using shred_ptr...\n";
boost::shared_ptr< A< std::string sp_a(new A< std::string >("a
short string"));
std::cout << *sp_a << std::endl;

return 0;
}

/*
A() < -temp
copy A
copy A
copy A
copy A
copy A
~A() <- temp destruction
11.1
11.1
11.1
11.1
11.1
~A() <- 5 automatic destructors
~A()
~A()
~A()
~A()
allocating using shred_ptr...
A()
a short string
~A() <- automatic
*/

And if you have any doubt about the strategy, change
std::vector< A< double vd(5, 11.1); // 5 elements
to
std::vector< A< double vd(1000000); // one million

For the sake of comparison: the equivalent of a const reference is a
const pointer to a const value.
int n;
const r_n&(n);
const p_n* const(&n);

Using pointers is iffy enough, using non-const pointers can be
downright dangerous where these should be immutable.

Dec 12 '06 #3

P: n/a

Bo Yang skrev:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Why must you pass pointers everywhere? A proper C++ program rarely
needs raw pointers, and memory leaks/invalid pointer access is not a
serious problem in correctly written code.
If you so desire, you can install a garbage collector. In that case you
do not have to maintain memory ressources. But remember that other
resources still must be maintained. I never have had a need to use a
garbage collector, as smart pointers such as those provided by boost,
but I other types of applications might have other needs.

/Peter

Dec 12 '06 #4

P: n/a

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.
Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Dec 12 '06 #5

P: n/a

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Aren't arrays really just pointers anyway?

Dec 12 '06 #6

P: n/a
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Bo Yang:

It's a sutil subject. I think there are no magic solutions here. After
~15 years of C/C++ programming, pointers are the minor concern to me.
Perhaps the advise would be to read a couple of good books to learn
good programming techniques and gain experience coding from scratch.

Hope this help.

Dec 12 '06 #7

P: n/a
Th*******@gmail.com wrote:
>
Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs
and no aspirins.
Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)
Aren't arrays really just pointers anyway?
No.
Brian

Dec 12 '06 #8

P: n/a

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Thanks for the perfect example: in the above - a - decays to a pointer.
Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

Dec 12 '06 #9

P: n/a
"Salt_Peter" <pj*****@yahoo.comwrote in
news:11**********************@73g2000cwn.googlegro ups.com:
>
Howard wrote:
>"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegr oups.com...
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs
and no aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a
pointer. Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown
How about v[1]?
Dec 12 '06 #10

P: n/a

Default User wrote:
Th*******@gmail.com wrote:

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...

>

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs
and no aspirins.
>
Hmm... no pointers means no bugs, eh? Interesting...
>
int main()
{
int a[1];
a[1] = 1;
}
>
No bugs? :-)
Aren't arrays really just pointers anyway?

No.
Brian
Oh yes, they most definitely are, through and through. Case in point:
the code above.
And the undefined behaviour that follows.

#include <iostream>
#include <ostream>

int main()
{
int array[1];
std::cout << "array = " << array << std::endl;
int* p = array;
std::cout << "p = " << p << std::endl;

array[1] = 1;
std::cout << "array + 1 = " << array + 1 << std::endl;
std::cout << "array[1] = " << array[1] << std::endl;
std::cout << "p + 1 is " << p + 1 << std::endl;
std::cout << "*(p + 1) = " << *(p + 1) << std::endl;

array[100] = 1;
std::cout << "array + 100 = " << array + 100 << std::endl;
std::cout << "p + 100 = " << p + 100 << std::endl;
std::cout << "array[100] = " << array[100] << std::endl;
std::cout << "*(p + 100) = " << *(p + 100) << std::endl;
}

/*
array = 0x7fff92835240
p = 0x7fff92835240
p + 1 is 0x7fff92835244
array + 1 = 0x7fff92835244
array[1] = 1
*(p + 1) = 1
array + 100 = 0x7fff928353d0
p + 100 = 0x7fff928353d0
array[100] = 1
*(p + 100) = 1
*/

And the compiler doesn't even blink.

Dec 12 '06 #11

P: n/a

Andre Kostur wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in
news:11**********************@73g2000cwn.googlegro ups.com:

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...



Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs
and no aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Thanks for the perfect example: in the above - a - decays to a
pointer. Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

How about v[1]?
sorry, also a pointer: usually implemented with:

reference operator[](size_type n)
{
return *(begin() + n);
}

Dec 12 '06 #12

P: n/a
Thanks for the perfect example: in the above - a - decays to a
pointer. Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

How about v[1]?
I don't believe the [] operator does bounds checking in vectors, while
the at() function does.

Dec 12 '06 #13

P: n/a
>Hmm... no pointers means no bugs, eh? Interesting...
>>
int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a
pointer. Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

How about v[1]?

sorry, also a pointer: usually implemented with:

reference operator[](size_type n)
{
return *(begin() + n);
}
That's stuff controlled by the implementor of the runtime, not the user.
If you want to continue that argument, at is probably implemented as:

reference at(size_type n)
{
if (n < capacity)
return *(begin() + n);

throw std::out_of_range();
}
If you want to continue that argument to it's conclusion, it's impossible
to write a (non-trivial) C++ program without pointers. Everything (class
instances) has a this pointer. std::list<has pointers internally. So
does std::map<and std::auto_ptr<>. Every one of your string literals
decays to a pointer.
Dec 12 '06 #14

P: n/a

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
>
Howard wrote:
>"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegr oups.com...
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a pointer.
Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown
no naked pointers means no bugs and no aspirins
Whether or not an implementation (or even ALL implementations) implement an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays, and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard
Dec 12 '06 #15

P: n/a
Salt_Peter wrote:
>
Default User wrote:
Th*******@gmail.com wrote:
>
Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
>

>
Your goal is to write an entire program without using a
pointer directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no
bugs and no aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)
Aren't arrays really just pointers anyway?
No.
Oh yes, they most definitely are, through and through. Case in point:
the code above.
Nope. In some cases, array names are converted to pointers to the first
element. However, arrays are not pointers.

Look into the sizeof and & operators.


Brian

Dec 12 '06 #16

P: n/a

Andre Kostur wrote:
Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a
pointer. Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

How about v[1]?
sorry, also a pointer: usually implemented with:

reference operator[](size_type n)
{
return *(begin() + n);
}

That's stuff controlled by the implementor of the runtime, not the user.
If you want to continue that argument, at is probably implemented as:

reference at(size_type n)
{
if (n < capacity)
return *(begin() + n);

throw std::out_of_range();
}
If you want to continue that argument to it's conclusion, it's impossible
to write a (non-trivial) C++ program without pointers. Everything (class
instances) has a this pointer. std::list<has pointers internally. So
does std::map<and std::auto_ptr<>. Every one of your string literals
decays to a pointer.
Everything is eventually a pointer or using one somewhere. Thats a
given.
Pointers are far from being useless, programs wouldn't function without
them.
In fact, pointers are quick/fast - assuming they are used correctly.

Take the OP's quest as a matter in point. Can you see his dilemna?
I can, and their are solutions - thats my point.

I'll bet that if we were to dissect his code he's got plain mutable
pointers all over the place instead of constant pointers to immutable
variables. I'll bet he's using naked pointers where he should implement
constant references. I'm also willing to bet that he can replace all or
most allocations with smart_pointers and/or load elements into STL
templated containers.

The issue is not wheter we have the choice to use them or not. We
don't.
The choice we do have, is how we use them.

Dec 13 '06 #17

P: n/a

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...



Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Thanks for the perfect example: in the above - a - decays to a pointer.
Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

no naked pointers means no bugs and no aspirins

Whether or not an implementation (or even ALL implementations) implement an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays, and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard
Let me answer your question this way, after all, you brought arrays
into the topic for some reason.

If a client asked me to implement a safe solution that must absolutely
use arrays, i'ld probably wrap a primitive array in a class and
implement range-checking through some exception mechanism. I'm certain
you'ld do the same. Why? Cause its a relatively simple solution which
will/should provide the safety requirement.

In other words, to answer the OP's question, there are alternatives
other than having to manage a bunch of pointers directly. Those
alternatives, for the most part, will not complicate the code. In many
cases, they will simplify it. Example: boost::shared_ptr<or
std::deque, etc.

That doesn't mean don't use pointers. Thats impossible. That doesn't
mean you can't use them safely.

Instead of the all-too common:
void foo(int* p_n) {... }
....use...
void foo(int const * const p_n) { ... }
....or in the case *p_n needs to be mutable...
void foo(int* const p_n) { ... }
.... and even better ...
void foo(const int& r_n) { ... }
void foo(int& r_n) { ... }

And we can go on and on.

Dec 13 '06 #18

P: n/a
Apart from all the solutions suggested above, You can use Smart
Pointers, you can make your own class of pointers with constructors
and destructors and prevent memory leaks.

Iterators too are a good alternative .
While designing classes you can associate iterators with them.

Iterators are much safer way of referrencing and dereferrencing things
in a c++ program.
You can use the following link to read about iterators.

http://cppreference.com/iterators.html

I suggest you to search google for "smart pointers" and " iterators" .
Hope this helps.

Saurabh :-)

Dec 13 '06 #19

P: n/a

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>
Howard wrote:
>"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegr oups.com...
>
Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegr oups.com...

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and
no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a pointer.
Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown

>no naked pointers means no bugs and no aspirins

Whether or not an implementation (or even ALL implementations) implement
an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays,
and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard

Let me answer your question this way, after all, you brought arrays
into the topic for some reason.
Geez... You said, and I quote again: "no naked pointers means no bugs and no
aspirins". So I presented a simple program with _no_ naked pointers, but an
obvious bug. I then followed that with a "smiley face", indicating I was
joking with you, pointing out the (to me) obvious humor inherent in such a
statement. If you don't get it by now, forget it.

-Howard


Dec 13 '06 #20

P: n/a

Salt_Peter skrev:
Bo Yang wrote:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.
Well,,, we all know that this last statement is an exaggeration.
Replace any allocation with the appropriate smart pointer and use STL
containers (which support copy semantics).
Replace pointers with references, prefereably - const references where
appropriate.
[snip]
Whereas the rest of this quote is sound and very good advice:
- Do not use pointers unless you have to - often you don't.
- When you do have to use a pointer, use a smart pointer of some sort.
- do not use low-level C features: prefer modern C++ equivalents. This
includes in particular casts (which better be rare in any case). But it
is normally also a good idea not to use raw C-style arrays. Use
std::vector or a boost fixed-size vector.

Kind regards
Peter

Dec 13 '06 #21

P: n/a

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...

Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...



Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and
no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a pointer.
Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_error thrown


no naked pointers means no bugs and no aspirins

Whether or not an implementation (or even ALL implementations) implement
an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays,
and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard
Let me answer your question this way, after all, you brought arrays
into the topic for some reason.

Geez... You said, and I quote again: "no naked pointers means no bugs and no
aspirins". So I presented a simple program with _no_ naked pointers, but an
obvious bug. I then followed that with a "smiley face", indicating I was
joking with you, pointing out the (to me) obvious humor inherent in such a
statement. If you don't get it by now, forget it.
lol, all input is wellcome, including yours.

Dec 14 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.