[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

The dreaded segfault

Prasad

10/16/2008 2:30:00 AM

Hi folks,

I am trying to debug the following program. Debugging the core file
revealed segfault at possibleFlows[i][k][m]=0 when values of
states=36, labels=40. It works fine for lesser values.

What could be the reason? Integer overflow or out of memory? Am i
missing something obvious?
I am using ubuntu and GNU g++.

int main()
{
const int states=36, labels=40;
int fromLabels=labels, toLabels=labels;
int fromStates=states, toStates=states;
int successors[fromStates][toStates];
int stateMatrix[fromStates][toStates][fromLabels][toLabels];
int possibleFlows[states][fromLabels][toLabels];
//Initialise all possible flows to 0
for(int i=0;i<states;i++)
for(int k=0;k<fromLabels;k++)
for(int m=0; m<toLabels;m++)
possibleFlows[i][k][m]=0; //Segmentation fault here

//Populate with given values
//S0
possibleFlows[0][1][0]=1;
possibleFlows[0][2][0]=1;
.........................rest of the
code....................................
18 Answers

Ian Collins

10/16/2008 2:39:00 AM

0

Prasad wrote:
> Hi folks,
>
> I am trying to debug the following program. Debugging the core file
> revealed segfault at possibleFlows[i][k][m]=0 when values of
> states=36, labels=40. It works fine for lesser values.
>
> What could be the reason? Integer overflow or out of memory? Am i
> missing something obvious?
> I am using ubuntu and GNU g++.
>
> int main()
> {
> const int states=36, labels=40;
> int fromLabels=labels, toLabels=labels;
> int fromStates=states, toStates=states;
> int successors[fromStates][toStates];

This isn't legal C++, are you build this as C?

--
Ian Collins

Stephen Horne

10/16/2008 3:35:00 AM

0

On Wed, 15 Oct 2008 19:30:23 -0700 (PDT), Prasad
<prasadmpatil@gmail.com> wrote:

> int possibleFlows[states][fromLabels][toLabels];
> //Initialise all possible flows to 0
> for(int i=0;i<states;i++)
> for(int k=0;k<fromLabels;k++)
> for(int m=0; m<toLabels;m++)
> possibleFlows[i][k][m]=0; //Segmentation fault here

My brains hurting so I may be wrong, but you might want to try...

possibleFlows[m][k][i]=0;

I always mix up my multi-dimensional subscripting, so maybe you have
to.

Prasad

10/16/2008 8:33:00 AM

0

On Oct 15, 9:39 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Prasad wrote:
> > Hi folks,
>
> > I am trying to debug the following program.  Debugging the core file
> > revealed segfault at possibleFlows[i][k][m]=0 when values of
> > states=36, labels=40. It works fine for lesser values.
>
> > What could be the reason? Integer overflow or out of memory? Am i
> > missing something obvious?
> > I am using ubuntu and GNU g++.
>
> > int main()
> > {
> >    const int states=36, labels=40;
> >    int fromLabels=labels, toLabels=labels;
> >    int fromStates=states, toStates=states;
> >    int successors[fromStates][toStates];
>
> This isn't legal C++, are you build this as C?
>
> --
> Ian Collins

This is a c++ program. I have removed all the namespace declaration,
headers and other code which was not relevant to the problem.

Ian Collins

10/16/2008 8:56:00 AM

0

Prasad wrote:
> On Oct 15, 9:39 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> Prasad wrote:
>>> Hi folks,
>>> I am trying to debug the following program. Debugging the core file
>>> revealed segfault at possibleFlows[i][k][m]=0 when values of
>>> states=36, labels=40. It works fine for lesser values.
>>> What could be the reason? Integer overflow or out of memory? Am i
>>> missing something obvious?
>>> I am using ubuntu and GNU g++.
>>> int main()
>>> {
>>> const int states=36, labels=40;
>>> int fromLabels=labels, toLabels=labels;
>>> int fromStates=states, toStates=states;
>>> int successors[fromStates][toStates];
>> This isn't legal C++, are you build this as C?
>>
Please don't quote signatures.
>
> This is a c++ program. I have removed all the namespace declaration,
> headers and other code which was not relevant to the problem.

No it is not, C++ does not have VLAs and successors is a VLA.

--
Ian Collins

Bernd Strieder

10/16/2008 9:08:00 AM

0

Hello,

Prasad wrote:

> I am trying to debug the following program. Debugging the core file
> revealed segfault at possibleFlows[i][k][m]=0 when values of
> states=36, labels=40. It works fine for lesser values.
>
> What could be the reason? Integer overflow or out of memory? Am i
> missing something obvious?
> I am using ubuntu and GNU g++.
>
> int main()
> {
> const int states=36, labels=40;
> int fromLabels=labels, toLabels=labels;
> int fromStates=states, toStates=states;
> int successors[fromStates][toStates];
> int stateMatrix[fromStates][toStates][fromLabels][toLabels];

That is 36*36*40*40*4 bytes or over 8MB for stateMatrix alone, which
might be over the maximum stack size of your platform. Try ulimit -s in
a shell, it tells the max stacksize in kiB. Exceeding stacksize results
in segfaults.

You will have to use dynamic allocation or global variables to solve
that problem portably. Usually overusing stack is not a sign of
professional style in programming.

Bernd Strieder

Kai-Uwe Bux

10/16/2008 9:10:00 AM

0

Prasad wrote:

> Hi folks,
>
> I am trying to debug the following program. Debugging the core file
> revealed segfault at possibleFlows[i][k][m]=0 when values of
> states=36, labels=40. It works fine for lesser values.
>
> What could be the reason? Integer overflow or out of memory? Am i
> missing something obvious?
> I am using ubuntu and GNU g++.
>
> int main()
> {
> const int states=36, labels=40;
> int fromLabels=labels, toLabels=labels;
> int fromStates=states, toStates=states;
> int successors[fromStates][toStates];
> int stateMatrix[fromStates][toStates][fromLabels][toLabels];

The above tries to allocate 2073600 ints. That could burst limits of what
g++ is willing to do.

Commenting out that line, the code runs with g++ on my computer.

> int possibleFlows[states][fromLabels][toLabels];
> //Initialise all possible flows to 0
> for(int i=0;i<states;i++)
> for(int k=0;k<fromLabels;k++)
> for(int m=0; m<toLabels;m++)
> possibleFlows[i][k][m]=0; //Segmentation fault here
>
> //Populate with given values
> //S0
> possibleFlows[0][1][0]=1;
> possibleFlows[0][2][0]=1;
> ........................rest of the
> code....................................


Best

Kai-Uwe Bux

Ian Collins

10/16/2008 9:24:00 AM

0

Kai-Uwe Bux wrote:
> Prasad wrote:
>
>> Hi folks,
>>
>> I am trying to debug the following program. Debugging the core file
>> revealed segfault at possibleFlows[i][k][m]=0 when values of
>> states=36, labels=40. It works fine for lesser values.
>>
>> What could be the reason? Integer overflow or out of memory? Am i
>> missing something obvious?
>> I am using ubuntu and GNU g++.
>>
>> int main()
>> {
>> const int states=36, labels=40;
>> int fromLabels=labels, toLabels=labels;
>> int fromStates=states, toStates=states;
>> int successors[fromStates][toStates];
>> int stateMatrix[fromStates][toStates][fromLabels][toLabels];
>
> The above tries to allocate 2073600 ints. That could burst limits of what
> g++ is willing to do.
>
And it ain't C++!

--
Ian Collins

Kai-Uwe Bux

10/16/2008 9:43:00 AM

0

Ian Collins wrote:

> Kai-Uwe Bux wrote:
>> Prasad wrote:
>>
>>> Hi folks,
>>>
>>> I am trying to debug the following program. Debugging the core file
>>> revealed segfault at possibleFlows[i][k][m]=0 when values of
>>> states=36, labels=40. It works fine for lesser values.
>>>
>>> What could be the reason? Integer overflow or out of memory? Am i
>>> missing something obvious?
>>> I am using ubuntu and GNU g++.
>>>
>>> int main()
>>> {
>>> const int states=36, labels=40;
>>> int fromLabels=labels, toLabels=labels;
>>> int fromStates=states, toStates=states;
>>> int successors[fromStates][toStates];
>>> int stateMatrix[fromStates][toStates][fromLabels][toLabels];
>>
>> The above tries to allocate 2073600 ints. That could burst limits of what
>> g++ is willing to do.
>>
> And it ain't C++!

I know. The compiler is required to issue a diagnostic and can then go on a
compile anyway. But we know that the compiler compiles anyway; and that the
code is not C++ is somewhat besides the point of why a segfault happens.


Best

Kai-Uwe Bux

Prasad

10/16/2008 10:02:00 PM

0

Thanks Bernd. That is indeed the problem. The stack limit is 10 MB. My
bad for not taking it into account.
I will use malloc() to overcome the problem.

On Oct 16, 4:08 am, Bernd Strieder <strie...@informatik.uni-kl.de>
wrote:
> Hello,
>
>
>
> Prasad wrote:
> > I am trying to debug the following program.  Debugging the core file
> > revealed segfault at possibleFlows[i][k][m]=0 when values of
> > states=36, labels=40. It works fine for lesser values.
>
> > What could be the reason? Integer overflow or out of memory? Am i
> > missing something obvious?
> > I am using ubuntu and GNU g++.
>
> > int main()
> > {
> > const int states=36, labels=40;
> > int fromLabels=labels, toLabels=labels;
> > int fromStates=states, toStates=states;
> > int successors[fromStates][toStates];
> > int stateMatrix[fromStates][toStates][fromLabels][toLabels];
>
> That is 36*36*40*40*4 bytes or over 8MB for stateMatrix alone, which
> might be over the maximum stack size of your platform. Try ulimit -s in
> a shell, it tells the max stacksize in kiB. Exceeding stacksize results
> in segfaults.
>
> You will have to use dynamic allocation or global variables to solve
> that problem portably. Usually overusing stack is not a sign of
> professional style in programming.
>
> Bernd Strieder

Stephen Horne

10/17/2008 6:28:00 AM

0

On Thu, 16 Oct 2008 05:43:04 -0400, Kai-Uwe Bux <jkherciueh@gmx.net>
wrote:

>I know. The compiler is required to issue a diagnostic and can then go on a
>compile anyway. But we know that the compiler compiles anyway; and that the
>code is not C++ is somewhat besides the point of why a segfault happens.

Probably agreed, but I'm confused. What is going on with that example,
exactly?

I tried compiling - GCC 3.4.5 didn't give me that required diagnostic
even with -Wall. All it told me about was a couple of unused
variables.

The obvious mistake is that fromLabels etc are not marked const. To be
honest, I was surprised that the use of those variables in what should
surely need to be constant expressions compiled at all.

Havn't tried VC++, but I'm pretty confident it will choke.

Adding the two missing const flags doesn't fix the segfault, so
assuming I'm on the right lines, I agree that it's irrelevant to the
problem at hand - especially as this is probably just a
hastily-prepared-simple-example thing.

I'd love to know what's going on, but surely it's just a
compiler-specific extension?

Being over-pedantic myself, I'm confident that compiler-specific
extensions aren't outlawed by the standard, so I don't see how their
use can make something "not C++". Not portable, of course, and
off-topic, but that's not the same thing.

BTW - I've looked in the GCC manual, but for some reason I can't find
the option to turn off all compiler-specific extensions. The dialect
section gives plenty of options, but they all seem to need me to know
which extension I want to turn off. Don't read the manual for me, of
course, but if anyone knows off the top of their head?