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-26 10:28:56
|
Unless you work in embedded environments - set-top boxes, cameras, burglar
alarms, ... - you can be pretty sure that you have an OS that will release
all memory uppon termination of your program.
However, you normally should take care of releasing your memory anyway for
the following reasons:
- If the program does a lot of alloc/free, and you want to make sure that
the program isn't leaking memory, constantly growing in size. There are a
number of memory heap check programs that on termination of the program
prints out a reference to all memory not released. Obviously, this isn't
meaningful if you intentionally ignores the release of some memory. The
printout will contain a lot of nice then.
- If your aim is to change from C to C++. When the OS releases memory, it
will never call any object destructors, and a number of C++ objects might
have other side effects besides just consuming memory :-) such as
=> Closing and removing temporary files.
=> Removing a hourglass icon, and replacing with normal arrow icon.
=> Terminating another program (IDE don't need a compiler anymore).
When you do implement a C++ class, you really do expect the destructor to
be called, and consider it quite rude if someone don't release the object
before ending the program.
/Per W
On Fri, 25 Oct 2002, Scott Williams wrote:
> Don't know if anyone responded yet but I think the answer is: Yes, unless
> your OS messes up. The release of the memory wasn't actually my concern,
> however, I just wanted to be sure memory I allocated in one part of my
> program was going to be available elsewhere.
>
> Scott
>
> -----Original Message-----
> From: Louis Godio [mailto:lg...@ya...]
> Sent: Friday, October 25, 2002 8:30 PM
> To: se...@al...; Per Westermark
> Cc: dev...@li...
> Subject: RE: [Dev-C++] dynamic allocation and structs
>
> What was the final answer to the the original question asked:
> "what is the duration of memory allocated by malloc (etc.)? "
>
> If not freed manually or by a destructor, Does the memory ALWAYS get
> released when the main() program ends with a normal exit ?
>
> --- Scott Williams <se...@al...> wrote:
> > Thanks for the replies to my question (and the entertaining
> > discussion it led to). It was all very helpful.
> >
> > Scott
> >
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Thursday, October 24, 2002 9:13 AM
> > To: Scott Williams
> > Cc: dev...@li...
> > Subject: Re: [Dev-C++] dynamic allocation and structs
> >
> > // Alternative 1, array of entries.
> > mystruct *myarray;
> > void func(int count) {
> > int i;
> > myarray = (mystruct*)malloc(count*sizeof(mystruct));
> > for (i = 0; i < count; i++) {
> > myarray[i].xx = yy;
> > }
> > }
> >
> > // Alternative 2, array of pointers to entries.
> > mystruct **myarray;
> > void func(int count) {
> > int i;
> > myarray = (mystruct**)malloc(count*sizeof(*mystruct));
> > for (i = 0; i < count; i++) {
> > myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> > myarray[i]->xx = yy;
> > }
> > }
> >
> > /Per W
> >
> > On Thu, 24 Oct 2002, Scott Williams wrote:
> >
> >> Hi,
> >>
> >> I'm working in C (no C++ please). Got a couple of questions
> >> related to dynamic allocation and in particular, dynamically
> >> allocating an array of structs.
> >>
> >> First, what is the duration of memory allocated by malloc
> >> (etc.)? In particular, I want to dynamically allocate/de-
> >> allocate several global 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.
> >>
> >> Second, it is my understanding that it is perfectly acceptable
> >> to have 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
> >> dynamically allocate an array of structs, and, in particularly,
> >> make the result available globally?
> >>
> >> Thanks for your help (& sorry if this is naive).
> >>
> >> Scott Williams
>
>
|