| Author |
Doubt about exit()
|
|
|
| will the exit() system call deallocates the memory allocated
dynamically using new operator or malloc ??
if it wont work cant we do like this.....( here i'm using atexit() to
call bye when exit() is called before exiting the program )
for example : in the following code ....
****************************************
******************
#include<stdio.h>
#include<stdlib.h>
class A
{
public :
A(int j);
~A();
void foo();
int j;
};
A :: ~A()
{
printf("destructor called\n");
}
A :: A(int j)
{
this->j = 10;
}
void A :: foo()
{
printf("foo\n");
exit(0);
}
A *a = new A(10);
void bye();
int main()
{
int i;
i = atexit(bye);
printf(" a->j is :%d\n",a->j);
a->foo();
printf("foo over\n");
}
void bye()
{
printf("bye called\n");
printf(" a->j is :%d\n",a->j);
delete(a);
}
****************************************
*************************
if i compile the above code i'm getting error as :
****************************************
*****************
/tmp/ccAWT0Nj.o(.text+0xc6): In function `bye()':
: undefined reference to `A::~A [in-charge]()'
collect2: ld returned 1 exit status
****************************************
*****************
| |
| spibou@gmail.com 2006-07-18, 4:00 am |
|
kiran wrote:
> will the exit() system call deallocates the memory allocated
> dynamically using new operator or malloc ??
Yes.
| |
| Thomas Maier-Komor 2006-07-18, 4:00 am |
| kiran wrote:
> will the exit() system call deallocates the memory allocated
> dynamically using new operator or malloc ??
>
it will deallocate the memory in the sense of freeing up the pages
needed for providing enough heap/free store, but it won't call the
destructors of the classes. I.e. you have to use atexit as you did in
your example, if your program relies upon the execution of the destructors.
> if it wont work cant we do like this.....( here i'm using atexit() to
> call bye when exit() is called before exiting the program )
>
BTW: a function passed to atexit should be declared 'extern "C" void
something(void)'.
Tom
| |
| joe@invalid.address 2006-07-18, 4:00 am |
| Thomas Maier-Komor <thomas@maier-komor.de> writes:
> kiran wrote:
>
> it will deallocate the memory in the sense of freeing up the pages
> needed for providing enough heap/free store, but it won't call the
> destructors of the classes. I.e. you have to use atexit as you did
> in your example, if your program relies upon the execution of the
> destructors.
Destructors are called when the program either returns from main() or
calls exit(). Using atexit() can cause problems and has to be done
carefully.
Joe
| |
| Thomas Maier-Komor 2006-07-18, 4:00 am |
| joe@invalid.address wrote:
> Thomas Maier-Komor <thomas@maier-komor.de> writes:
>
>
> Destructors are called when the program either returns from main() or
> calls exit(). Using atexit() can cause problems and has to be done
> carefully.
>
> Joe
NO! The destructor is only called for objects that were allocated on
stack when the program terminates by returning from main. Calling exit()
will bypass calling _any_ destrcutors. Objects created with new are
located in Freestore and will not be destucted automatically upon
program termination in any case. C++ doesn't have an automatic memory
management like Java.
=========== Example 1:
maierkom@peejay:~$ uname -a
SunOS peejay 5.10 Generic_118855-02 i86pc i386 i86pc
maierkom@peejay:~$ cat b.cc
#include <iostream>
using namespace std;
struct A
{
A()
{
cout << "A::A()" << endl;
}
~A()
{
cout << "A::~A()" << endl;
}
};
struct B
{
B()
{
cout << "B::B()" << endl;
}
~B()
{
cout << "B::~B()" << endl;
}
};
int main(int argc, char **argv)
{
A *a = new A;
B b;
return 0;
}
maierkom@peejay:~$ CC b.cc
maierkom@peejay:~$ ./a.out
A::A()
B::B()
B::~B()
========= Example 2 =========
maierkom@peejay:~$ cat b.cc
#include <iostream>
using namespace std;
struct A
{
A()
{
cout << "A::A()" << endl;
}
~A()
{
cout << "A::~A()" << endl;
}
};
struct B
{
B()
{
cout << "B::B()" << endl;
}
~B()
{
cout << "B::~B()" << endl;
}
};
int main(int argc, char **argv)
{
A *a = new A;
B b;
exit(EXIT_SUCCESS);
}
maierkom@peejay:~$ CC b.cc
maierkom@peejay:~$ ./a.out
A::A()
B::B()
Cheers,
Tom
| |
| davids@webmaster.com 2006-07-18, 7:59 am |
|
joe@invalid.address wrote:
> Thomas Maier-Komor <thomas@maier-komor.de> writes:
>
>
> Destructors are called when the program either returns from main() or
> calls exit(). Using atexit() can cause problems and has to be done
> carefully.
He is asking about objects allocated with operator new or malloc. These
are not destroyed when the program returns from main or calls exit.
DS
| |
| joe@invalid.address 2006-07-18, 7:01 pm |
| Thomas Maier-Komor <thomas@maier-komor.de> writes:
> joe@invalid.address wrote:
>
> NO! The destructor is only called for objects that were allocated on
> stack when the program terminates by returning from main. Calling exit()
Sorry, I was pretty tired and missed the point that the objects are
dynamically allocated.
3.6.3 Temination
However, it's not just objects on the stack. "Destructors (12.4) for
initialized objects of static storage duration (declared at block
scope or at namespace scope) are called as a result of returning from
main and as a result of calling exit..."
Joe
| |
|
|
davids@webmaster.com wrote:
> joe@invalid.address wrote:
>
> He is asking about objects allocated with operator new or malloc. These
> are not destroyed when the program returns from main or calls exit.
>
> DS
Thanks for ur clarification. But i have a doubt when we exit the
process the operating system automatically removes all the stack and
heap segments of the process. then the objects will be deallocated
automatically right..??
| |
| Barry Margolin 2006-07-19, 4:00 am |
| In article <1153281701.698857.217480@p79g2000cwp.googlegroups.com>,
"kiran" <edu.mvk@gmail.com> wrote:
> davids@webmaster.com wrote:
>
> Thanks for ur clarification. But i have a doubt when we exit the
> process the operating system automatically removes all the stack and
> heap segments of the process. then the objects will be deallocated
> automatically right..??
Yes. When a process exits, all its memory is discarded, except for
shared memory segments. And all its file descriptors are closed.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| davids@webmaster.com 2006-07-20, 8:00 am |
|
kiran wrote:
> davids@webmaster.com wrote:
[color=darkred]
[color=darkred]
> Thanks for ur clarification. But i have a doubt when we exit the
> process the operating system automatically removes all the stack and
> heap segments of the process. then the objects will be deallocated
> automatically right..??
Only to the extent the system understands those objects. If they use
resources the system doesn't know about, it will not be able to free
those resources. This is not the same as destructors being called.
For example, and object may be associated with shared memory or
resources in a remote server. An object may have an associated file
that is deleted by its destructor. These cleanups will not happen.
In general, object allocated using 'new' or 'malloc' will be allocated
from memory obtained by 'sbrk' or anonymous 'mmap'. The 'sbrk' function
extends the process' segment, which ceases to exist when the process
ends. The same is true for anonymous mappings, they are discarded by
the system when their reference count gets to zero.
Because all 'malloc' can do is allocate heap/mmap space, you don't have
to worry about leaks at process termination. The memory was allocated
out of space that no longer exists. However, 'new' can have
side-effects in the constructor that need to be undone by the
destructor.
Calling 'exit' will not call the destructors of such objects; however,
you can easily make it do so. You can create an 'ExitDestroyedObject'
class and just derive classes that need their destructors called at
exit time from that. Basically, the constructor adds the object to a
linked list (or other structure, it doesn't really matter), and the
destructor removes it from the list. An 'atexit' function invokes a
virtual destructor on all objects in the linked list.
DS
|
|
|
|