Apparent flaws in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 4
Reputation: tintin.iitk is an unknown quantity at this point 
Solved Threads: 0
tintin.iitk tintin.iitk is offline Offline
Newbie Poster

Apparent flaws in C++

 
0
  #1
Nov 9th, 2009
[code]
class A {
A& a;
public:
A(): a(* new A()) {}
};
[code]

There is a huge flaw here and before I compiled this code I was hoping that it will fail to compile, But it didn't. At first I felt like I had a big flaw in C++ as it allowed data member pointers of the same kind in a class, but then I realized the flaw was in that it is a recursive function call, which will form an infinite loop. Apart from recursive functions, which other methods do you ppl know about C++?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,273
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 543
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator
 
-7
  #2
Nov 9th, 2009
C is designed with the assumption that if the the programmer does something, however strange it seems, it is because he means to do it.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: tintin.iitk is an unknown quantity at this point 
Solved Threads: 0
tintin.iitk tintin.iitk is offline Offline
Newbie Poster
 
0
  #3
Nov 9th, 2009
That's a good way of looking at it.. but anyway I was more interested in knowing what other such necessary-to-avoid situations are.. like dynamic memory allocation is another such thing. Well, I'm a beginner as far as C++ programming is concerned(mostly used java) and was curious to know what all things I should be cautious about. Anyway, Java did solve the problem of leaked dynamic memory allocation through its internal junk management but C/C++ are not much safe (ok, smart pointers and std::auto_ptr are alternatives, but still not foolproof)about that. Returning to the original problem, even Java suffers from this risk of falling into infinite loops dues to recursive function calls.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 95
Reputation: gusano79 is on a distinguished road 
Solved Threads: 9
gusano79 gusano79 is offline Offline
Junior Poster in Training
 
2
  #4
Nov 9th, 2009
Originally Posted by tintin.iitk View Post
Returning to the original problem, even Java suffers from this risk of falling into infinite loops dues to recursive function calls.
Originally Posted by jbennet View Post
C is designed with the assumption that if the the programmer does something, however strange it seems, it is because he means to do it.
C is notorious for giving you "enough rope to shoot yourself in the foot"--but it doesn't matter what language you're talking about... if it allows you to define a recursive function, you can always define one that never terminates.

Allowing recursion without this possibility is sort of like inventing a language in which untruthful statements aren't grammatical--a warm, fuzzy thought, but such a thing simply doesn't exist.

Sometimes recursion loops are detectable, but no development environment I've ever worked with has even bothered to try.

The halting problem is at the heart of this issue... I recommend you study it if you haven't seen it already. This version is more entertaining.
--smg
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum


Views: 245 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC