For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > Class and instances (Stack overflow)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Class and instances (Stack overflow)
Jack

2005-11-19, 3:58 am

No. of class instances too big, x5000xmany bytes each
stack overflows and crashes
How do I create class instance on the heap?
Thanks
Jack


Jack

2005-11-19, 3:58 am

Looks like it crashes right at the beginning of winmain. Is my conclusion
that the class is created on stack correct? or I am wrong that all classes
will be created on heap? Thanks
Jack

"Jack" <jl@knight.com> 撰寫於郵件新聞:uNN0KSO7FHA.2092@TK2MSFTNGP12.phx.gbl...
> No. of class instances too big, x5000xmany bytes each
> stack overflows and crashes
> How do I create class instance on the heap?
> Thanks
> Jack
>
>



David Wilkinson

2005-11-19, 7:57 am

Jack wrote:

> Looks like it crashes right at the beginning of winmain. Is my conclusion
> that the class is created on stack correct? or I am wrong that all classes
> will be created on heap? Thanks
> Jack
>
> "Jack" <jl@knight.com> 撰寫於郵件新聞:uNN0KSO7FHA.2092@TK2MSFTNGP12.phx.gbl...
>
>


Jack:

It would help if you showed us some code. But

1. If you create an object with "new", it is on the heap. Otherwise it
is on the stack.

2. You should not place large objects on the stack.

3. Some might disagree, but IMHO objects should normally be on the
stack. Two reasons:

(a) Using "new" makes you look like a Java or C# programmer!

(b) You open the possiblity of memory leaks if you forget to call "delete".

So what to do? Th solution is not to use large objects. Do not make a
class like

class MyClass
{
double m_data[10000];
};

but rather do

class MyClass
{
public:
MyClass(): m_data(10000){}

private:
std::vector<double> m_data;
};

Now your object is very small, and you can place it on the stack. The
memory used by m_data is on the heap, but that is taken care of by the
standard library.

Although C++ is a complex language, if you use the standard library it
can be much simpler than programming in C. Most programs can avoid the
explicit use of "new" and "delete" entirely (these calls are contained
inside the standard library, but someone else has taken care of that).

HTH,

David Wilkinson



Lucas Galfaso

2005-11-19, 7:57 am

David wrote:

> 3. Some might disagree, but IMHO objects should normally be on the stack.
> Two reasons:
>
> (a) Using "new" makes you look like a Java or C# programmer!


This is a very poor objection.

>
> (b) You open the possiblity of memory leaks if you forget to call
> "delete".


Now we are getting to something.

>
> So what to do? Th solution is not to use large objects. Do not make a
> class like
>
> class MyClass
> {
> double m_data[10000];
> };
>
> but rather do
>
> class MyClass
> {
> public:
> MyClass(): m_data(10000){}
>
> private:
> std::vector<double> m_data;
> };



I think this is not a good practice, and a practice that is not easy to
generalized. Sometimes you just _have_ to use new, that is why we have
auto_ptr and {TR1|boost}::shared_ptr. Use them and you will be close to
never have to write a "delete".


>
> Now your object is very small, and you can place it on the stack. The
> memory used by m_data is on the heap, but that is taken care of by the
> standard library.
>
> Although C++ is a complex language, if you use the standard library it can
> be much simpler than programming in C. Most programs can avoid the
> explicit use of "new" and "delete" entirely (these calls are contained
> inside the standard library, but someone else has taken care of that).



Same idea, but auto_ptr/shared_ptr are very small objects and the solution
does not involve code changes to the underlying classes, is more flexible,
and general.



>
> HTH,
>
> David Wilkinson
>
>
>



David Wilkinson

2005-11-19, 7:01 pm

Lucas Galfaso wrote:

> David wrote:
>
>

[snip]
>
>
>
>
> I think this is not a good practice, and a practice that is not easy to
> generalized. Sometimes you just _have_ to use new, that is why we have
> auto_ptr and {TR1|boost}::shared_ptr. Use them and you will be close to
> never have to write a "delete".
>
>
>
>
>
>
> Same idea, but auto_ptr/shared_ptr are very small objects and the solution
> does not involve code changes to the underlying classes, is more flexible,
> and general.
>


Lucas:

Interesting... I would ceratinly agree that if you are going to use
"new" you should "always wrap the raw pointer in some kind of smart
pointer, but this is quite an advanced C++ technique.

For a beginner, IMHO, it is much more important to know how to make use
of simple aspects of the C++ library, like vector and string, and
thereby write correct programs that do not leak memory or overflow the
stack.

But I think I also disagree with you in general. You seeem to be saying:
always put the object on the heap, because it might be big. I say: don't
make big objects in the first place, and let the client of the class
decide if they want to use the stack or the heap.

Personally, I believe that "never new unless you have to" is a good
working principle; it allows automatic destruction of resources, and in
general provides better efficiency.

Interestingly, the designers of the vew C++/CLI language (including some
top quality C++ talent) have gone to great lengths to provide "stack
semantics" for reference classes (which in fact are always allocated on
the GC heap). So it would seem that they consider the stack method to be
the preferred idiom in C++.

David Wilkinson
Lucas Galfaso

2005-11-23, 7:04 pm

Hi David,

>
> Lucas:
>
> For a beginner, IMHO, it is much more important to know how to make use of
> simple aspects of the C++ library, like vector and string, and thereby
> write correct programs that do not leak memory or overflow the stack.
>
> But I think I also disagree with you in general. You seeem to be saying:
> always put the object on the heap, because it might be big. I say: don't
> make big objects in the first place, and let the client of the class
> decide if they want to use the stack or the heap.
>
> Personally, I believe that "never new unless you have to" is a good
> working principle; it allows automatic destruction of resources, and in
> general provides better efficiency.


I agree with you, but I think my original post might be misunderstood.
Using smart pointers is a coding practice, making the objects big or small
is a design decision.
What I tried to say with mi post was that if you have to use new, use a
smart pointer, and nothing more.

Lucas

>
> Interestingly, the designers of the vew C++/CLI language (including some
> top quality C++ talent) have gone to great lengths to provide "stack
> semantics" for reference classes (which in fact are always allocated on
> the GC heap). So it would seem that they consider the stack method to be
> the preferred idiom in C++.
>
> David Wilkinson



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com