Re: [Dev-C++] dynamic allocation and structs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2002-10-24 14:46:38
|
Nope! Dyanmically allocated memory doesn't get released just because you leave the scope. An object destructor will - if correctly written - release memory allocted within the object, when the object goes out of scope. However, normal pointers doesn't have any destructors, so there will be no release of the memory. It will be the normal memory leak, i.e. a pointer no longer possible to access, that will not be released until the program ends, since unextended C++ doesn't have garbage collection. The only ways to release dynamically allocated memory is to: - manually releasing it - ending the program A destructor releasing local data from an object must be considered a =20 manual release, since the code specifically has to write a call to free() or delete. Yes, template-based containers can automatically release memory for the objects in the container, but only if the container destructor is called. If the container was created dynamically, the container will never get out of scope, so the destroctor won't run unless container is manually released... It is possible to create "smart pointers", i.e. a c++ object that looks and behaves like a pointer, but has a destructor that will release the memory when the "smart pointer" goes out of scope. Still, same thing as with template-based containers. If the "smart pointer" object is created dynamically, it won't go out of scope, and the destructor won't be called. /Per W EMA System email: pw...@ia... Per Westermark web: http://iapetus.neab.net Vargvaegen 174B phone: +46 8 410 17 198 S-906 42 Umeaa Sweden On Thu, 24 Oct 2002, Carlos Garc=EDa del Monte wrote: >=20 >=20 > Per Westermark escribi=F3: >=20 > > Well, maybe not directly forever. > > > > It will also be automatically released - for most operating systems, th= at > > is - when the application ends. >=20 > Or when exiting the scope. >=20 > > > > > > /Per W > > > > On Thu, 24 Oct 2002, Daniel Glenfield wrote: > > > > > I can help with the first - the lifetime of a variable created with m= alloc() > > > is forever - until you call free() of course! > > > > > > > > > > > > > > > >From: "Scott Williams" <se...@al...> > > > >Reply-To: <se...@al...> > > > >To: <dev...@li...> > > > >Subject: [Dev-C++] dynamic allocation and structs > > > >Date: Thu, 24 Oct 2002 08:18:24 -0400 > > > > > > > >Hi, > > > > > > > >I'm working in C (no C++ please). Got a couple of questions related= to > > > >dynamic allocation and in particular, dynamically allocating an arra= y of > > > >structs. > > > > > > > >First, what is the duration of memory allocated by malloc (etc.)? I= n > > > >particular, I want to dynamically allocate/de-allocate several globa= l > > > >variables within a single subroutine called from main and so want to= be > > > >sure > > > >that the allocated memory exists beyond the scope of that subroutine= =2E > > > > > > > >Second, it is my understanding that it is perfectly acceptable to ha= ve an > > > >array of structs. E.g., > > > > > > > >struct mystruct { > > > > double var1; > > > > long var2; > > > > etc... > > > >} > > > > > > > >. . . > > > > > > > >struct mystruct data[100]; > > > > > > > >Is this correct? If so, what then is the calling sequence to dynami= cally > > > >allocate an array of structs, and, in particularly, make the result > > > >available globally? > > > > > > > >Thanks for your help (& sorry if this is naive). > > > > > > > >Scott Williams > > > > > > > > > > > > > > > >------------------------------------------------------- > > > >This sf.net email is sponsored by: Influence the future > > > >of Java(TM) technology. Join the Java Community > > > >Process(SM) (JCP(SM)) program now. > > > >http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/j= avavote > > > >_______________________________________________ > > > >Dev-cpp-users mailing list > > > >Dev...@li... > > > >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > > > >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > > _________________________________________________________________ > > > Surf the Web without missing calls! Get MSN Broadband. > > > http://resourcecenter.msn.com/access/plans/freeactivation.asp > > > > > > > > > > > > ------------------------------------------------------- > > > This sf.net email is sponsored by: Influence the future > > > of Java(TM) technology. Join the Java Community > > > Process(SM) (JCP(SM)) program now. > > > http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/ja= vavote > > > _______________________________________________ > > > Dev-cpp-users mailing list > > > Dev...@li... > > > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > ------------------------------------------------------- > > This sf.net email is sponsored by: Influence the future > > of Java(TM) technology. Join the Java Community > > Process(SM) (JCP(SM)) program now. > > http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/java= vote > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 >=20 >=20 > ------------------------------------------------------- > This sf.net email is sponsored by: Influence the future > of Java(TM) technology. Join the Java Community > Process(SM) (JCP(SM)) program now. > http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/javavo= te > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 |