Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > The dreaded segfault

Reply
Thread Tools

The dreaded segfault

 
 
Prasad
Guest
Posts: n/a
 
      10-16-2008
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....................................
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-16-2008
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
 
Reply With Quote
 
 
 
 
Stephen Horne
Guest
Posts: n/a
 
      10-16-2008
On Wed, 15 Oct 2008 19:30:23 -0700 (PDT), Prasad
<> 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.

 
Reply With Quote
 
Prasad
Guest
Posts: n/a
 
      10-16-2008
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-16-2008
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
 
Reply With Quote
 
Bernd Strieder
Guest
Posts: n/a
 
      10-16-2008
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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-16-2008
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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-16-2008
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-16-2008
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
 
Reply With Quote
 
Prasad
Guest
Posts: n/a
 
      10-16-2008
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
TB, Sunbird, and the dreaded Pocket PC - OpenSource help needed! Kneewax Firefox 1 10-13-2004 06:48 PM
The dreaded asp.clipboard =?Utf-8?B?QmlsbCBCZWxsaXZlYXU=?= ASP .Net 3 05-02-2004 04:05 PM
dreaded OE losing emails? asifi'dtell Computer Support 3 04-23-2004 12:44 PM
The dreaded Rex X Golden Oldie Computer Support 6 09-30-2003 02:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57