Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
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
 ****************************************
*****************


Report this thread to moderator Post Follow-up to this message
Old Post
kiran
07-18-06 09:00 AM


Re: Doubt about exit()
kiran wrote:

> will the exit() system call deallocates the memory allocated
> dynamically using new operator or malloc ??

Yes.


Report this thread to moderator Post Follow-up to this message
Old Post
spibou@gmail.com
07-18-06 09:00 AM


Re: Doubt about exit()
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

Report this thread to moderator Post Follow-up to this message
Old Post
Thomas Maier-Komor
07-18-06 09:00 AM


Re: Doubt about exit()
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

Report this thread to moderator Post Follow-up to this message
Old Post
joe@invalid.address
07-18-06 09:00 AM


Re: Doubt about exit()
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

Report this thread to moderator Post Follow-up to this message
Old Post
Thomas Maier-Komor
07-18-06 09:00 AM


Re: Doubt about exit()
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


Report this thread to moderator Post Follow-up to this message
Old Post
davids@webmaster.com
07-18-06 12:59 PM


Re: Doubt about exit()
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

Report this thread to moderator Post Follow-up to this message
Old Post
joe@invalid.address
07-19-06 12:01 AM


Re: Doubt about exit()
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..??


Report this thread to moderator Post Follow-up to this message
Old Post
kiran
07-19-06 09:00 AM


Re: Doubt about exit()
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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
07-19-06 09:00 AM


Re: Doubt about exit()
kiran 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..??

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


Report this thread to moderator Post Follow-up to this message
Old Post
davids@webmaster.com
07-20-06 01:00 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:21 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.