Board index » cppbuilder » how to solve a stack overflow

how to solve a stack overflow


2004-10-26 07:46:30 PM
cppbuilder60
hi,
I write scientific type software that uses 2 arrays of float with two
dimensions ; the first one is expected to be between 1 and 200, second from
200 to 100000 almost 20 000 000 data.
the arrays are created dynamically. After they are filled inside a loop
calling each time one exe.
This provocs a stack overflow error.
I must mention also that I'm using Code Guard.
The help is telling me that it can be provoced by :
- a local big array : i've got
- a recursive call : i haven't
- an assembler procedure that ... : i haven't
I try to modify the stacks limits (maximum) but I must also modify the pile
limit (maximum) to make it run. I thought only the stack was involved for my
problem. I try also to change the definition of the variables : members of a
class to local variables and it seems to be able to run further.
Do I miss something ?
As I'll have to do the same with double in a few months, can someone help me
?
thanks
mary
 
 

Re:how to solve a stack overflow

On Tue, 26 Oct 2004 13:46:30 +0200, "mary" <no>wrote:
Quote
hi,
I write scientific type software that uses 2 arrays of float with two
dimensions ; the first one is expected to be between 1 and 200, second from
200 to 100000 almost 20 000 000 data.
the arrays are created dynamically. After they are filled inside a loop
calling each time one exe.
This provocs a stack overflow error.
I must mention also that I'm using Code Guard.
The help is telling me that it can be provoced by :
- a local big array : i've got
How about creating them on the heap using malloc (C) or new[] (C++)?
Could even be that execution is faster.
HTH
Helmut Giese
 

Re:how to solve a stack overflow

Quote
How about creating them on the heap using malloc (C) or new[] (C++)?
Could even be that execution is faster.
HTH
Helmut Giese
In fact, when I write that the arrays are created dynamically, it means that
I do :
valeurFacteur = new float *[nbFactVar];
for (int i=0; i<nbFactVar;i++)
valeurFacteur[i] = new float[Nf];
Is "using malloc (C) or new[] (C++)" different ? Can you explain ? Is malloc
better ?
thanks
Mary
 

{smallsort}

Re:how to solve a stack overflow

mary wrote:
Quote
Is "using malloc (C) or new[] (C++)" different ? Can you explain ? Is
malloc better ?
malloc() is the older 'C' heap allocator function. The disadvantages of
malloc() are:
* It doesn't know about types.
You have to tell it how many bytes to allocate from the heap. 'new[]'
can work that out for itself.
* It doesn't know about classes. This means that constructors and
destructors won't get called on array elements unless you call them.
tbh this isn't often an issue.
* C++ developers will keep asking you why you're using deprecated
functions.
* It's possible that with 'new' being the approved allocation method
it may on some platforms be faster than malloc.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:how to solve a stack overflow

On Tue, 26 Oct 2004 14:44:22 +0200, "mary" <no>wrote:
Quote

>How about creating them on the heap using malloc (C) or new[] (C++)?
>Could even be that execution is faster.
>HTH
>Helmut Giese

In fact, when I write that the arrays are created dynamically, it means that
I do :
valeurFacteur = new float *[nbFactVar];
for (int i=0; i<nbFactVar;i++)
valeurFacteur[i] = new float[Nf];

Is "using malloc (C) or new[] (C++)" different ? Can you explain ? Is malloc
better ?
thanks

Mary

This code runs OK.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int nbFactVar = 200;
int Nf = 100000;
float **f;
f = new float *[nbFactVar];
for (int i=0; i<nbFactVar; ++i)
f[i] = new float[Nf];
for(int i=0; i<nbFactVar; i++)
for(int j = 0; j<Nf; ++j)
f[i][j] = 1.0;
Memo1->Lines->Text = " Done!";
}
//---------------------------------------------------------------------------
There must be something else {*word*89} you stack up. Remember that stack and heap are *not* the same thing and are *not*
allocated in the same way.
"Inusfficient data for meaningful answer" - UNIVAC