toton said:
peter said:
toton skrev:
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,
Huh? What sort of detail are you looking for that is not found in, say,
Stroustrup's _The C++ Programming Language_ 3rd ed. or Josuttis' _The
Standard Library_?
and nearly no framework (like MFC Qt GTKmm VCF
VCL wxWindows and many others) uses stack based allocation .
True, but many of them have constraints that may force their hand in
that department (e.g., supporting multiple platforms with polymorphism,
supporting legacy code that is not written according to current best
practice, supporting C and C++, etc.).
Don't you
see a little difference between the textbook examples and commercial
implementation? Most of them dont use stl containers (use their own).
Some of them were written before the STL was finalized as part of the
language, and rather than rewrite everything, they kept their in-house
containers. The STL was added to obviate the need for everyone to write
their own containers for every project, i.e. to standardize practice
across the industry.
But the modern one like VCF uses [STL], 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.
Like I said, avoid pointers when you can. But sometimes you need them,
and one of those times is when the OS or library requires a pointer.
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?
No. A declaration such as
vector<BigClass> v;
doesn't allocate space for anything (except the internal members of
std::vector, which are usually a few pointers). You might be thinking
of the way vector grows by doubling its size, but that effect can be
mitigated by using reserve() or by stripping excess capacity at
appropriate points.
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 .
I would prefer
Image image( filename );
The whole things look little strange to me, one c++ std thing, who
thinks differently, and other the commercial/ oss frameworks.
This is the most important line in this post: Different groups have
different goals, different tools, and different compentancies. You'd
have to ask each vendor why they chose to use or not use standard
containers (see above for some of my guesses as to their answers), but
I'd say you should use the best practices for software development that
you can and convert to lesser styles when you must. For instance, in my
current project, I have to interface with a C-style library for array
processing, but all my data is contained in std::vectors. So I must
convert when I make that library call:
void CallSomeLib( int*, unsigned );
vector<int> v;
// ... fill v here ...
CallSomeLib( &v[0], v.size() );
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.
See above for why this is.
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.
C++ is a general purpose language that runs natively on many platforms,
and unlike Java, C++ does not have standard GUI support. Indeed, the
Creator says of the future of GUI support: "Sadly, I cannot offer hope
for the most frequently wished for new standard library: a standard GUI
library. A GUI library is simply too large a task for the volunteers of
the C++ standards committee to handle and too difficult a task given
the many (non-standard but huge, useful, and supported) GUI libraries
available. Please notice that even though they are not standard, the
major C++ GUIs have more users than most programming languages and are
often better supported." (
http://www.artima.com/cppsource/cpp0x2.html)
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?
Your question is too broad, but you can search the web for discussions
about using std::string with MFC's CString, for instance. There are
plenty of problems interfacing the standard library with third-party
libraries of all sorts, but those discussions are often off-topic here
(see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9).
I have used standard C++ with various third-party libraries and
interfaces (MFC, COM, C-style number crunching libraries, OS system
calls, etc.), and I design the system to keep my "business logic,"
which is written in standard C++ according to current best practices,
separate from the from all third-party library code. Sometimes, this
means that I wrap the third-party calls in standard C++, e.g., by
creating an abstract factory that returns a pointer to an object that
implements the interface for that particular platform (for instance, I
did this for synchronization primitives for multithreading on different
platforms). Sometimes it means that I keep the two as separate as
possible and convert between my interface and theirs when crossing the
boundary (cf. std::string vs. CString and the CallSomeLib example
above).
Cheers! --M