🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #416

Re: Question about pointers in c++

#416
From: Mark Framness
Date: Sun, 15 Feb 1998 00:00
94 lines
3367 bytes
In message <MPG.f4db3e9eeb97b39896ab@news.edmonton.telusplanet.net> -
adamst@telusplanet.net (Trenton D. Adams) writes:
#>
#>If I make a pointer in C++, say to an object!  I initialized this pointer 
#>to point to a place in memory created by the new function.  Will this 
#>area in memory be release when the object is destroyed, or do I have to 
#>explicitely have to call delete?

Yes.  You must call delete.  The best thing to do would probably be to create
a "wrapper" class and then when the new'ed variables go out of scope, the
wrapper class destructor is called and deletes the objects for you.  This way
you pass the responsibility of calling the delete from yourself to the
compiler.

e.g:  

class wrapper
{
char* some_string;
int len;

public:
void show(void);  //some function to output string.
wrapper(char* a_string);
~wrapper();
};

wrapper::wrapper(char* a_string)
{
len = strlen(a_string);
if(!(some_string = new char[len]))
   {
    // memory allocation error routine
   }

memcopy(some_string,a_string,len);
}

wrapper::~wrapper()
{

/*  Attn C++ gurus:  I've been led to believe that the below is good practice.
 Is it?  */

if(some_string)
   {
   delete [] some_string;
   some_string = 0;         
   }

}

Now in the following main function:

int main(void)
{
wrapper a("Ack");

a.show();

return 0;
}

When the function main terminates, the dtor for the wrapper class is called
automatically and you do not have to call the delete [] operator yourself.  If
for some reason you have to delete the string (in the above class essentially
destructing it) delete the string first, then reset len = 0; and then lastly
set the char* to zero (i.e. some_string = 0; ) that way when the dtor is
called you are not deleting previously deleted memory, a buggy thing!

Cline & Lomow in their book C++ FAQs and in Bjarne's The C++ Programming
Language suggest that you use this technique every time you have to ask for
and give back system resources (such as memory detailed above, files and other
such things).


Take Care!

postmaster@[127.0.0.1]  
Spam booby trap!!!

FROM:  Mark Allen Framness
HOME:  framness@.NO_SPAM.EMIRATES.NET.AE
WORK:  m477@NO_SPAM.ugru.uaeu.ac.ae
HTTP:  http://netnet.net/~farmer/index.html

URL for comp.lang.c NG:  http://www.eskimo.com/~scs/C-faq/top.html

To reply via e-mail, delete the given reply to address and delete the NO_SPAM
from the above addresses.
					&
All standard disclaimers apply.  Anyone who says otherwise is itching for a
fight.
Technical Details
Message-ID: <6c5h9n$7ud@ws1.emirates.net.ae>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <MPG.f4db3e9eeb97b39896ab@news.edmonton.telusplanet.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8d38,4e3e5791e6083a64
X-Google-Attributes: gidf8d38,public
X-Google-Thread: 109fba,4e3e5791e6083a64
X-Google-Attributes: gid109fba,public
X-Google-Thread: 108ebe,4e3e5791e6083a64
X-Google-Attributes: gid108ebe,public
X-Google-Thread: 114592,4e3e5791e6083a64
X-Google-Attributes: gid114592,public
From: framness@EMIRATES.NET.AE (Mark Framness)
Subject: Re: Question about pointers in c++
Date: 1998/02/15
Message-ID: <6c5h9n$7ud@ws1.emirates.net.ae>
X-Deja-AN: 325074009
References: <MPG.f4db3e9eeb97b39896ab@news.edmonton.telusplanet.net>
Reply-To: framness@EMIRATES.NET.AE
Organization: PERSONAL
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,borland.public.cpp.owl,comp.lang.c++