473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic storage duration

Is it defined in the standard what happend to dynamic storage that has
not been deleted at program termination?

I read through the Dynamic storage section and cannot find it.
Adrian

Apr 10 '07 #1
9 2130
Adrian wrote:
Is it defined in the standard what happend to dynamic storage that has
not been deleted at program termination?

I read through the Dynamic storage section and cannot find it.
The C++ standard doesn't say anthing about what happens after the program
exits.

Apr 10 '07 #2
On Apr 10, 1:17 pm, Rolf Magnus <ramag...@t-online.dewrote:
The C++ standard doesn't say anthing about what happens after the program
exits.
Probably explains why I cannot find it :-)

For example though the C standard says that it closes all open files.
Is there anything that happens with new'ed objects? Or is this all
implementation defined.
Apr 10 '07 #3
Adrian wrote:
On Apr 10, 1:17 pm, Rolf Magnus <ramag...@t-online.dewrote:
>The C++ standard doesn't say anthing about what happens after the program
exits.

Probably explains why I cannot find it :-)

For example though the C standard says that it closes all open files.
Is there anything that happens with new'ed objects? Or is this all
implementation defined.

They don't get destructed if that is what you are asking. Now what
happens to the memory? Same as with malloc'd memory in C. It's
not specified. In practice systems nearly always reclaim all the
allocated memory upon exit. However, if your code allocates other
resources inside the object that the destructor is supposed to clean
up, it won't happen.
Apr 10 '07 #4
On Apr 10, 1:52 pm, Ron Natalie <r...@spamcop.netwrote:
They don't get destructed if that is what you are asking. Now what
happens to the memory? Same as with malloc'd memory in C. It's
not specified. In practice systems nearly always reclaim all the
allocated memory upon exit. However, if your code allocates other
resources inside the object that the destructor is supposed to clean
up, it won't happen.
I wanted a gurantee on the freeing of memory. Guess it isnt so and I
will cleanup after myself.

As long as I stay away from STL containers is there any problem with a
singleton implementation that uses a auto_ptr to hold the private
implementation as a static?

ie
class AImpl
{
};

class B
{
private:
static std::auto_ptr<AImplimpl_;
}

That should be deleted properly on exit?
Adrian

Apr 10 '07 #5
On 10 Apr, 20:30, "Adrian" <n...@bluedreamer.comwrote:
On Apr 10, 1:17 pm, Rolf Magnus <ramag...@t-online.dewrote:
The C++ standard doesn't say anthing about what happens after the program
exits.

Probably explains why I cannot find it :-)

For example though the C standard says that it closes all open files.
Is there anything that happens with new'ed objects? Or is this all
implementation defined.
Hiya Adrian,

It's all platform dependent.

A modern operating system with virtual memory management (like Linux
or Windows) will release memory (and many other types of resources)
when the process terminates. For the memory, it's pretty much just
throwing away the page tables and any backing store used when paging.

An older (or modern embedded!) operating system without VMM might not
do this, though. And of course, any other resources (like shared
memory segments or mutexes) might not be released cleanly, if at all.

So for portable code, always try to clean up the mess you make. If
it's a play project on your home PC, don't waste your time unless you
need to learn - do it a few times.

Doug

Apr 10 '07 #6
On Apr 10, 2:27 pm, "Doug" <DougTheS...@googlemail.comwrote:
Doug
It's all platform dependent.
This is commerical code that will be ported at a later date. So I need
to make sure that it is compliant to the standard.
So for portable code, always try to clean up the mess you make. If
it's a play project on your home PC, don't waste your time unless you
need to learn - do it a few times.
Is there any reason a static auto_ptr would not clean up correctly?
Instance of the object is not dependent on anything else..

I know about the gotcha's with auto_ptr's and containers but was
wondering if I missed any?

Apr 10 '07 #7
On 10 Apr, 22:19, "Adrian" <n...@bluedreamer.comwrote:

<snip>
Is there any reason a static auto_ptr would not clean up correctly?
Instance of the object is not dependent on anything else..
Hi Adrian,

I think a static would be cleaned up, but I'm not absolutely sure
w.r.t. the standard. Someone here more knowledgable can tell us
that. I hope...

This wee program:

#include <iostream>
using namespace std;

class MX
{
public:
MX() { cout << "ctor" << endl; }
~MX() { cout << "~ctor" << endl; }
};

static auto_ptr<MXgMX(new MX());

int main(void)
{
return 0;
}

.... produces ...

[ds@boxa tmp]# gcc test.cpp -lstdc++
[ds@boxa tmp]# ./a.out
ctor
~ctor

.... on my old linux box. I imagine a static class member var would
also be cleaned up, but I'm off to bed now.

Doug

Apr 10 '07 #8
Adrian wrote:
On Apr 10, 1:52 pm, Ron Natalie <r...@spamcop.netwrote:
>They don't get destructed if that is what you are asking. Now what
happens to the memory? Same as with malloc'd memory in C. It's
not specified. In practice systems nearly always reclaim all the
allocated memory upon exit. However, if your code allocates other
resources inside the object that the destructor is supposed to clean
up, it won't happen.

I wanted a gurantee on the freeing of memory. Guess it isnt so and I
will cleanup after myself.
Well, if you want a guarantee, you might find it with your OS. However, as
Ron already wrote, even if the memory is reclaimed, destructors of the
dynamic objects aren't called.
As long as I stay away from STL containers is there any problem with a
singleton implementation that uses a auto_ptr to hold the private
implementation as a static?
You might run into something similar to the problem mentioned in
http://www.parashift.com/c++-faq-lit...html#faq-10.14 .
ie
class AImpl
{
};

class B
{
private:
static std::auto_ptr<AImplimpl_;
}

That should be deleted properly on exit?

Apr 11 '07 #9
On Apr 10, 10:35 pm, Rolf Magnus <ramag...@t-online.dewrote:
You might run into something similar to the problem mentioned inhttp://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14.
Checking the FAQ I dont think I will. Below is a trivial example of
what I am actually doing.

#include <iostream>
#include <memory>

class Impl;

class pImpl
{
private:
friend class Impl;
friend class std::auto_ptr<pImpl>;
pImpl() {};
~pImpl() {};
pImpl(const pImpl &);
pImpl &operator=(const pImpl &);

void print_some_stuff() { std::cout << "Hi\n"; };
};

class Impl
{
public:
Impl()
{
if(_pimpl.get()==0)
{
std::auto_ptr<pImpltemp(new pImpl);
_pimpl=temp;
}
};

void print_some_stuff() { _pimpl->print_some_stuff(); };
private:
Impl(const Impl &);
Impl &operator=(const Impl &);
static std::auto_ptr<pImpl_pimpl;
};

std::auto_ptr<pImplImpl::_pimpl;

void foo();
void bar();

int main(int argc, char *argv[])
{
foo();
Impl().print_some_stuff();
bar();

return 0;
}
void foo()
{
Impl().print_some_stuff();
}

void bar()
{
foo();
Impl().print_some_stuff();
}

Apr 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
1
by: ravi_shankar | last post by:
I know java,but I am just beginner in C.I have some confusions regarding extern storage specifier and default storage class specifier for a variable when it has file scope that is ,when it is not...
4
by: dominic.connor | last post by:
Ok, pX = new Stuff N]; Is dynamically created. what about Stuff x ; ? To me saying it's statically allocated is wrong, because that goes to static variables. I can't call it stack...
1
by: martinezfive | last post by:
Hi, According to ISO C++ (7.11/2), "An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default." What about...
2
by: asit | last post by:
Can functions have static storage clas ?? If yes, what is it's advantage ??
5
by: Triple-DES | last post by:
I've seen this question raised more than once, but I have not yet seen a definite, conclusive answer. Consider the following code: struct C { C() {} // might throw }; int main() { try {...
7
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
1
by: liquidator | last post by:
I'm in the process of updating a medium-sized Fortran program. I've just finished replacing the random number generator, which is now written in C. The C source code uses variables with "static...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.