🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #2032

Re: Should I learn C or C++ first?

#2032
From: Rainer Deyke
Date: Thu, 31 Aug 1995 13:20
70 lines
3099 bytes
Darren Walker (dwalker@imtn.dsccc.com) wrote:

: 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.
: }

A recursively called delete_linked_list() function would work just as
well.

:  
: .
: .
: .
: 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!");

This is NOT how realloc works.  Realloc is used to increase or decrease
the size of an array WITHOUT changing the contents.

: 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 allocation and deallocation of memory can easily be done within a
C function.

MyApp = new_TApplication("My Application");
delete_TApplication(MyApp);

You also completely missed the point of realloc.  Your "example" of how
to realloc in C++ actually doesn't change the size of the memory block
and is thus totoally irrelavent.

--
+----------------------------------------------+
|    Rainer Deyke (rainer@mdddhd.fc.hp.com)    |
| "The Earth shall inherit the meek" - Carcass |
+----------------------------------------------+
Technical Details
Message-ID: <424cvq$rk4@deyke3.fc.hp.com>
Path: arc1-ca.pugleaf.net!fc.hp.com!hpfcmdd!deyke3!rainer
References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <41kici$p4t@deyke3.fc.hp.com> <41ngln$qau@druid.borland.com> <41t7t0$79u@tpd.dsccc.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fc.hp.com!hpfcmdd!deyke3!rainer
From: rainer@deyke3.fc.hp.com (Rainer Deyke)
Newsgroups: rec.games.programmer,comp.lang.c++
Subject: Re: Should I learn C or C++ first?
Date: 31 Aug 1995 13:20:58 GMT
Organization: my basement
Message-ID: <424cvq$rk4@deyke3.fc.hp.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> <41t7t0$79u@tpd.dsccc.com>
Reply-To: rainer@mdddhd.fc.hp.com
NNTP-Posting-Host: deyke3.fc.hp.com
X-Newsreader: TIN [version 1.2 051093BETA PL3]