Article View: rec.games.programmer
Article #2015Re: Should I learn C or C++ first?
From: Darren Walker
Date: Mon, 28 Aug 1995 20:11
Date: Mon, 28 Aug 1995 20:11
92 lines
4359 bytes
4359 bytes
Pete Becker (pete@borland.com) wrote:
: In article <41kici$p4t@deyke3.fc.hp.com>, rainer@deyke3.fc.hp.com says...
: >
: >P Moore906 (pmoore906@aol.com) wrote:
: >: >Yes, but "variety of styles" may very well include using purely the C
: >: >subset of C++. Some C++ features are actually pretty stupid, like
: >: >the 'new' and 'delete' operators that correspond to 'malloc' and
: >: >'free' but lack anything corresponding to 'realloc'.
: >: I don't know how you could call new and delete stupid. Dynamically
: >: allocating memory for a class and passing arguments to its constructors at
: >: the same time would be impossible without new and delete! How could you
: >: ever say:
: >
: >: TApplication* MyApp;
: >: MyApp = new TApplication("My Application");
: >
: >: without new and delete?
: >
: >Why bother with doing both at the same time?
: >
: >MyApp = (TApplication *)malloc(sizeof(TApplication));
: >create_TApplication(MyApp, "My Application");
IMHO, I have found the memory allocation shemes for C++ to be VASTLY
superior to those of C. Using a new example, expanded on from above...the
realloc issue...(or free issue for that matter.)
class TApplication
{
public:
TApplication(char *name);
~TApplication();
...(lots of other crap not needed for example)...
private:
char *name;
LinkedList *Head;
BinaryTree *Root;
};
TApplication::~TApplication()
{
if(Head)
delete Head; // this will recursively delete the entire list (if
// the list's destructor is set up right.
if(Root)
delete Root; // this will recusively delete the entire tree.
}
.
.
.
TApplication* MyApp;
MyApp = new TApplication("My Application");
// now we want to RE-ALLOC MyApp
if (MyApp)
delete MyApp; // that's it...C++ class destructors take over and no
// memory leaks occur!
MyApp = new TApplication("My Application Again!");
--------
Now, IMHO, this seems FAR superior in both concept and implementation than
having to first remember to call the deallocator for the linked list, then
calling the deallocator for the binary tree, explicitely, before reallocating
MyApp. Maybe this will explain why us C++ programmers have FAR fewer memory
leaks than C programmers. It's because we choose to let the natural design
of the language do the job it was meant for. So ANYONE who says new and
delete are useless, has no clue about C++, and should stick with C, as they
completely missed the whole point behind the newer, more powerful language.
: The difference is in the gap between the two lines, where you have something
: that claims to be a pointer to a TApplication object, but isn't. C++ makes it
: very hard to perform operations on objects that have not been initialized.
: Separating memory allocation from initalization makes it much easier to
: accidentally do things like this, as we have all done at one time or another.
: Or, to turn the question around the other way, why separate them?
Okay, between Pete and my posts, I think we've covered how new and delete
are far superior to malloc, realloc, calloc, free, etc...
BTW, did I mention that new and delete can be overloaded to implement more
advanced memory allocation schemes while still maintaining the power of
calling the constructor/destructor. In such a case you would have to use
malloc and free, but this is a rare case.
--
-------------------------------------------------------------------------------
Darren Walker DSC Communications Corporation
dwalker@imtn.tpd.dsccc.com Plano, Tx
-------------------------------------------------------------------------------
Technical Details
Message-ID:
<41t7t0$79u@tpd.dsccc.com>Path:
arc1-ca.pugleaf.net!camelot.dsccc.com!nps160!dwalkerReferences:
<DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <41kici$p4t@deyke3.fc.hp.com> <41ngln$qau@druid.borland.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!camelot.dsccc.com!nps160!dwalker From: dwalker@imtn.dsccc.com (Darren Walker) Newsgroups: rec.games.programmer,comp.lang.c++ Subject: Re: Should I learn C or C++ first? Followup-To: rec.games.programmer,comp.lang.c++ Date: 28 Aug 1995 20:11:12 GMT Organization: DSC Communications Corporation, Plano, Texas USA Message-ID: <41t7t0$79u@tpd.dsccc.com> References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <41kici$p4t@deyke3.fc.hp.com> <41ngln$qau@druid.borland.com> NNTP-Posting-Host: nps160.imtn.tpd.dsccc.com X-Newsreader: TIN [version 1.2 PL2]