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

C++ and pointer

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

Thanks.

John
Jul 22 '05 #1
16 1195
"John" <jo*********@yahoo.com> wrote in message
news:c3*************************@posting.google.co m
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
Ian
John wrote:
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
Jul 22 '05 #3
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
Jul 22 '05 #4
ak
On 5 Jun 2004 23:38:29 -0700, jo*********@yahoo.com (John) wrote:
Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John


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
Jul 22 '05 #5
John wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #6
JKop wrote:
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 );
}


--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #7
ak <ak@workmail wrote:
On 5 Jun 2004 23:38:29 -0700, jo*********@yahoo.com (John) wrote:

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

Thanks.

John

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.
Jul 22 '05 #8
Thanks a lot.

John

"John Carson" <do***********@datafast.net.au> wrote in message news:<40********@usenet.per.paradox.net.au>...
"John" <jo*********@yahoo.com> wrote in message
news:c3*************************@posting.google.co m
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.

Jul 22 '05 #9
jo*********@yahoo.com (John) wrote in message news:<c3*************************@posting.google.c om>...
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
Jul 22 '05 #10
> 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


Jul 22 '05 #11
John wrote:
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

Jul 22 '05 #12
John wrote:
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
Jul 22 '05 #13
"Jussi Jumppanen" <ju****@zeusedit.com> wrote in message
news:40***********@zeusedit.com...
John wrote:
Can I completely avoid using pointer in C++ code, so that the
code has less bugs and is easy to debug?


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 :)
Jul 22 '05 #14
Thanks.
I am fighting with segmentation fault problems. I thought pointer may
cause the problem.

John

Jussi Jumppanen <ju****@zeusedit.com> wrote in message news:<40***********@zeusedit.com>...
John wrote:
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

Jul 22 '05 #15
John wrote:

Thanks.
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
Jul 22 '05 #16

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

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.