C++ and pointer

J

John

Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John
 
J

John Carson

John said:
Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John


It is not true that avoiding pointers entirely will necessarily produce less
bugs or code that is easy to debug.

By using the standard library (vectors, lists etc.), you can (and should)
drastically reduce the use of pointers. However, pointers are needed in at
least the following four circumstances

1. if you want polymorphic behaviour (you can also use references, but
references need to be initialised when declared and standard containers
cannot store references --- in any case, references don't solve the memory
leak problem associated with pointers),
2. when two classes/structs refer to each other and you have a catch 22
problem where each class/struct needs the other to be declared before it can
itself be declared,
3. when overloading operator new for reasons of efficiency, debugging or
whatever,
4. when interfacing with many C libraries.

You can avoid the use of pointers entirely, but at the cost of complicating
your program, producing more bugs and making your program less easy to
debug.

Use of "smart pointers" can avoid most of the problems (in particular,
memory leaks) associated with the use of raw pointers.
 
I

Ian

John said:
Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?
If you want.

Or you could code test first and not have any bugs or have to debug!

Ian
 
J

JKop

John posted:
Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John


You could also spend your life going around in a wheelchair, you won't fall
down as much. Up to you.


I myself *like* pointers. They're a very very simple concept once you've
grasped it. I assuming you're human, and therefore you're more than
intelligent enough to figure this stuff out:

int main(void)
{
int chocolate = 5;

int icecream = 7;


int* pDesert;


pDesert = &chocolate;

*pDesert = 2;


pDesert = &icecream;

*pDesert = 3;


//Now:
//chocolate == 2
//icecream == 3
}


-JKop
 
A

ak

actually I dont understand the connection pointers and bugs.. read to
many Java books lately?

the truth is that the language itself has nothing to do with the bugs
the bottom line its the programmer that produces the bugs not the
language or any particular feature of a language.

/ak
 
P

Phlip

John said:
Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Prefer the weakest language structure unless the stronger one permits less
complex code.

Prefer references to pointers, unless you need pointer's extra features
(which are it can point with NULL, and be deleted).

Prefer delegation to inheritance, stack objects to heap objects,
event-driven code to threaded code, etc.
 
P

Phlip

JKop said:
int main(void)
{
int chocolate = 5;

int icecream = 7;


int* pDesert;


pDesert = &chocolate;

*pDesert = 2;


pDesert = &icecream;

*pDesert = 3;

//Now:
//chocolate == 2
//icecream == 3

Never write a comment if there's an alternative in code:

assert( 2 == chocolate );
assert( 3 == icecream );
 
G

Gianni Mariani

ak said:
actually I dont understand the connection pointers and bugs.. read to
many Java books lately?

the truth is that the language itself has nothing to do with the bugs
the bottom line its the programmer that produces the bugs not the
language or any particular feature of a language.

corollary: I can write bad code in any language.
 
J

John

Thanks a lot.

John

John Carson said:
It is not true that avoiding pointers entirely will necessarily produce less
bugs or code that is easy to debug.

By using the standard library (vectors, lists etc.), you can (and should)
drastically reduce the use of pointers. However, pointers are needed in at
least the following four circumstances

1. if you want polymorphic behaviour (you can also use references, but
references need to be initialised when declared and standard containers
cannot store references --- in any case, references don't solve the memory
leak problem associated with pointers),
2. when two classes/structs refer to each other and you have a catch 22
problem where each class/struct needs the other to be declared before it can
itself be declared,
3. when overloading operator new for reasons of efficiency, debugging or
whatever,
4. when interfacing with many C libraries.

You can avoid the use of pointers entirely, but at the cost of complicating
your program, producing more bugs and making your program less easy to
debug.

Use of "smart pointers" can avoid most of the problems (in particular,
memory leaks) associated with the use of raw pointers.
 
J

James Moughan

Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John

Well, pointers, not really. But you can certainly avoid using new and
malloc, which removes most of the problems with pointers. (Alex
Stepanov, one of the creators of the STL, says he does this -
presumably, excluding when he's actually writing STL classes.)

Jam
 
M

Marcin Kalicinski

Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

You probably can avoid using raw pointers in majority of situations. But
it's not true that it will make the code less buggy or easier to debug. The
truth is that serious (=hard to track down) bugs are very seldom caused by
pointers, they are caused by bad design. Not using pointers will not help
it.

I wonder why people so often think that pointers are the the cause of all
evil?

Best regards,
Marcin
 
T

Thomas Matthews

John said:
Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John

Just remember that one can create severly buggy code
that is hard to debug without using pointers.

Search the web for "Test Driven Development".


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Jussi Jumppanen

John said:
Can I completely avoid using pointer in C++ code, so that the
code has less bugs and is easy to debug?

What has the relationship between bugs and pointers? I can see
how pointers may lead to memory leaks but how do they create
bugs? I make use of pointers extensively and my bugs are just
that bugs.

If you are using pointers and creating too many bugs then it
is time to learn how to use pointers correctly.

Jussi Jumppanen
Author of: Zeus for Windows (All new version 3.92 out now)
"The C/C++, Cobol, Java, HTML, Python, PHP, Perl programmer's editor"
Home Page: http://www.zeusedit.com
 
A

AngleWyrm

John, it is possible to use other data structures that insulate the
new/delete/dereferencing scenario. One of the most convienient is the
STL container classes. I don't have a generic answer for you; there
may be situations that require a pointer. Perhaps a collection of
polymorphic shapes would require pointers. But I've managed to get
along fine with minimum contact :)
 
J

John

Thanks.
I am fighting with segmentation fault problems. I thought pointer may
cause the problem.

John
 
D

Default User

John said:

Please don't top-post, your replies belong following properly trimmed
quotes.
I am fighting with segmentation fault problems. I thought pointer may
cause the problem.


That may be the case, but maybe not.\

A better solution would be to debug your program. Reduce it to the
smallest compilable subset that demostrates the problem, then post it.
Avoiding pointers out of irrational fear is a poor strategy. Learning
the language and fixing problems as they come up is a good strategy.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top