 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
toton Guest
|
Posted: Fri Aug 04, 2006 9:10 am Post subject: static & dynamic allocation problem |
|
|
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
};
class Foo{
private:
Bar bar;
public:
explicit Foo(){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
//delete bar;
cout<<"foo dtor"<<endl;
}
};
alternative way to write class Foo as,
class Foo{
private:
Bar* bar;
public:
explicit Foo() : bar(new Bar()){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(new Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
delete bar;
cout<<"foo dtor"<<endl;
}
};
which Foo class is to be used when? Most of the GUI library (like
wxWindows and Qt) I find the second kind is used (.i.e dynamic
allocation).
Note, in this case the bar objects lifetime is same as Foo's, i.e its
created at Foo constructor & destroyed at Foo's destructor. I know that
if Bar to be initializd any other place that Foo's ctor & destroyed
other than Foo's dtor, the second method is the only way. But if that
is not the case, which one is preferred? any problem wth the virtual
function of Bar for the first implementation?
moreover,
if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so?
Also how the memory layout is different between first and second case?
thanks in advance
abir |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 04, 2006 9:10 am Post subject: Re: static & dynamic allocation problem |
|
|
"if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so? "
The first sizeof(foo) == 1, because for some reason, when c++ compile
meet a class with no member variable, means sizeof(myClass) == 0, it
will fake something into myClass, to make it larger than 0. That is,
You will never get a sizeof(myClass) == 0. I think I have read this
from C++ Primier.
The next sizeof(foo) is actually the size of Bar*.
toton wrote:
| Quote: | Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
}; |
|
|
| Back to top |
|
 |
wei8010@gmail.com Guest
|
Posted: Fri Aug 04, 2006 9:10 am Post subject: Re: static & dynamic allocation problem |
|
|
toton 写道:
| Quote: | Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
};
class Foo{
private:
Bar bar;
public:
explicit Foo(){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
//delete bar;
cout<<"foo dtor"<<endl;
}
};
alternative way to write class Foo as,
class Foo{
private:
Bar* bar;
public:
explicit Foo() : bar(new Bar()){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(new Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
delete bar;
cout<<"foo dtor"<<endl;
}
};
which Foo class is to be used when? Most of the GUI library (like
wxWindows and Qt) I find the second kind is used (.i.e dynamic
allocation).
Note, in this case the bar objects lifetime is same as Foo's, i.e its
created at Foo constructor & destroyed at Foo's destructor. I know that
if Bar to be initializd any other place that Foo's ctor & destroyed
other than Foo's dtor, the second method is the only way. But if that
is not the case, which one is preferred? any problem wth the virtual
function of Bar for the first implementation?
moreover,
if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so?
Also how the memory layout is different between first and second case?
thanks in advance
abir
|
You can try:
class temp
{
public:
temp(void) {}
~temp(void) {}
};
sizeof(temp) = 1;
class temp1
{
public:
temp1(void) {}
~temp1(void) {}
private:
temp t;
};
sizeof(temp1) = 1;
class temp2
{
public:
temp2(void) {}
~temp2(void) {}
private:
temp *t;
};
sizeof(temp2) = 4; |
|
| Back to top |
|
 |
toton Guest
|
Posted: Mon Aug 07, 2006 9:10 am Post subject: Re: static & dynamic allocation problem |
|
|
peter koch wrote:
| Quote: | toton skrev:
mlimber wrote:
toton wrote:
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
[snip example with member as pointer or as variable]
which Foo class is to be used when? Most of the GUI library (like
wxWindows and Qt) I find the second kind is used (.i.e dynamic
allocation).
Note, in this case the bar objects lifetime is same as Foo's, i.e its
created at Foo constructor & destroyed at Foo's destructor. I know that
if Bar to be initializd any other place that Foo's ctor & destroyed
other than Foo's dtor, the second method is the only way. But if that
is not the case, which one is preferred?
Either could be useful depending on the situation, though in the second
case I'd suggest using a smart pointer/container that gives exception
safety (cf.
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2) and
automatic cleanup instead of explicit news and deletes (cf.
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1).
In general, you should use explicit dynamic allocation only when you
have to (e.g., with polymorphic objects, with objects intialized
outside the constructor, etc.), but sometimes using a pointer can
reduce compile-time dependencies and speed up build times. See
http://www.gotw.ca/publications/mill04.htm.
[snip]
Thanks for the reply.
Thus can I safely assume that using heap allocation (may be with
auto_ptr ) will not have n any performance problem?
No you can not. Follow mlimbers advice and avoid pointers unless you
have to. Using new and delete will invariably incur a cost in memory as
well as cpu-time. If it turns up to be a performance problem, i cant
tell - it depends on lots of things.
But more importantly, using pointers require far more code and is not
completely trivial to implement (Your original code was buggy).
In fact I am using container classes for arrays, but again there are
two types of allocation can be done.
like vector<MyClass> and vector<MyClass*
I have flexibility and easeness for second type ( I never forget to
delete memory, also I use all the boost pointers whichone suitable )
Again, use the simplest method available and refactor if it turns out
to be insufficient.
Only thing, most of the time people say stack based alocation is much
much faster than heap mased one. I am not sure how much true it is
It is accurate. A stack allocation is practically a free lunch in the
environments I work in (Windows/Unix). This is not true for a heap
allocation.
, but
like heap based as it offers flexibility, as I can initialize the
object when I need and the way I want.
[snip]
My reason of asking the question is, no book refers dynamic memory |
allocation in detail, and nearly no framework (like MFC Qt GTKmm VCF
VCL wxWindows and many others) uses stack based allocation . Don't you
see a little difference between the textbook examples and commercial
implementation? Most of them dont use stl containers (use their own).
But the modern one like VCF uses, but they store pointer to the class
inside the container, rather than the class itself. Moreover the
standard they follow like, the class which need to acquire system
resource like gui, filesystem , network etc need to be initialized in
heap, on the otherhand classes like Point, Dimension, Rect etc
initialize in stack (many of them use a struct syntax for them).
Many of them use reference count rather than explicit delete.
Thus when I program using such a framework, the concept is not matching
properly with same coding guideline. Moreover I am seeing no escape
fromusing pointer to initialize things, and passing the classes as
reference.
Infact how a few big class can be allocated in a vector whitout
pointer?
If I say vector<Component> wont is occupy memory of a few Component
even though no component is added? None of the existing library do it,
they do vector<Compoent*>.
Problems comes like, how to do static memory allocation for a Image,
image data need to be a pointer, and allocated dynamically. even the
frameworks write Image* image = new Image(filename) , rather than Image
image = Image(filename) most of the time .
The whole things look little strange to me, one c++ std thing, who
thinks differently, and other the commercial/ oss frameworks.
Infact most of the framework (not only GUI, the framework contains much
more) ,dont use std iostream, very less use even std string, they dont
go for template, they implement their own RTTI, very less of them use
STL containers, almost none use boost serialization, etc.
I am not sure why they are pole apart. I hadn't used C++ for no big
application, except the standard console based little programs. But I
used Java for some time for large enough project, were everything
looked consistent.
Now back to C++ for one of my personal project (will be GPLed!!) , I
decided to use VCF for GUI, networking, filesystem support etc, and I
find simply they are different than book examples. In fact they look
'Javaish ' to me rather than C++ish They are simple, they are subset
of C++, and simply different.
So when I use one of the framework (VCF) in my application, and try to
use the stack based alocation & template etc, the whole thing becomes
chaotic.
Can anyone say about the experience with any of the framework, and if
any problem they faced with the standard C++ type of implementation? Or
any book dealt with this?
thanks a lot.
> /Peter |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|