473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

The dreaded segfault

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....................................
Oct 16 '08 #1
18 1488
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
Oct 16 '08 #2
On Wed, 15 Oct 2008 19:30:23 -0700 (PDT), Prasad
<pr**********@gmail.comwrote:
> 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.

Oct 16 '08 #3
On Oct 15, 9:39*pm, Ian Collins <ian-n...@hotmail.comwrote:
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.
Oct 16 '08 #4
Prasad wrote:
On Oct 15, 9:39 pm, Ian Collins <ian-n...@hotmail.comwrote:
>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
Oct 16 '08 #5
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

Oct 16 '08 #6
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
Oct 16 '08 #7
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
Oct 16 '08 #8
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
Oct 16 '08 #9
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
Oct 16 '08 #10
On Thu, 16 Oct 2008 05:43:04 -0400, Kai-Uwe Bux <jk********@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?

Oct 17 '08 #11
Stephen Horne wrote:
On Thu, 16 Oct 2008 05:43:04 -0400, Kai-Uwe Bux <jk********@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?
An array declaration has the form

D1 [constant-expression_opt];

and the array bound in the example are not constant-expressions.

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?
Probably. It is a little disconcerting that there seems to be no compiler
switch to turn off the extension. (At least I did not find anything that
rings a bell in the man page.)

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.
Right. [1.4/8] puts forth the requirements for extensions. A diagnostic is
required.
[snip]
Best

Kai-Uwe Bux
Oct 17 '08 #12
Stephen Horne wrote:
On Thu, 16 Oct 2008 05:43:04 -0400, Kai-Uwe Bux <jk********@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?
To restore the snipped code:

const int states=36, labels=40;
int fromLabels=labels, toLabels=labels;
int fromStates=states, toStates=states;
int successors[fromStates][toStates];

successors is a variable length array (in C99 terms). C++ does not have
VLAs.
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.
gcc has had its own version of VLAs for a very long time.

gcc 4 will give the required diagnostic if you invoke it in a conforming
mode:

g++ /tmp/x.cc -ansi -pedantic
/tmp/x.cc: In function 'int main()':
/tmp/x.cc:11: error: ISO C++ forbids variable-size array 'successors'

--
Ian Collins
Oct 17 '08 #13
On Fri, 17 Oct 2008 19:52:37 +1300, Ian Collins <ia******@hotmail.com>
wrote:
>successors is a variable length array (in C99 terms). C++ does not have
VLAs.
Thanks.
>g++ /tmp/x.cc -ansi -pedantic
Strange that those options aren't in the "C++ Dialect" section - just
double checked. I still should have found them though. Oh well.

Thanks again.

Oct 17 '08 #14
On Fri, 17 Oct 2008 07:28:04 +0100, Stephen Horne wrote:

[...]
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?
For the current C++ standard (precisely, ISO/IEC 14882:1998 + ISO/IEC
14882:2003) you probably want (at least):

-ansi -pedantic

or equivalently:

-std=c++98 -pedantic

See subsection "2.2 C++ language" of:

http://gcc.gnu.org/onlinedocs/gcc-4....html#Standards

Confusingly, the actual options are documented in Section 3.4 "Options
Controlling C Dialect" (since this includes options for both C and C++):

http://gcc.gnu.org/onlinedocs/gcc-4....t-Options.html

If you use the GNU Standard C++ library (you probably do) then you might
also want to define the macro -D_GLIBCXX_CONCEPT_CHECKS for more
diagnostics. See:

http://gcc.gnu.org/onlinedocs/libstd...1pt03ch08.html

HTH,

--
Lionel B
Oct 17 '08 #15
On Oct 17, 8:52 am, Ian Collins <ian-n...@hotmail.comwrote:
Stephen Horne wrote:
[...]
gcc 4 will give the required diagnostic if you invoke it in a
conforming mode:
g++ /tmp/x.cc -ansi -pedantic
/tmp/x.cc: In function 'int main()':
/tmp/x.cc:11: error: ISO C++ forbids variable-size array 'successors'
That works, but if I understand correctly, the exact meaning of
-ansi may change in time. A more precise replacement would be
-std=c++98.

In practice, with any compiler, you'll need a lot of options for
everyday use. For g++ under Solaris, for example, I use:

$ echo $GppFlags
-std=c++98 -pedantic -ffor-scope -fno-gnu-keywords -foperator-
names -pipe -Wall -W -Woverloaded-virtual -Wno-sign-compare -Wno-
deprecated -Wno-non-virtual-dtor -Wpointer-arith -Wno-unused -Wno-
switch -Wno-missing-braces -Wno-long-long -static-libgcc -ggdb3 -
D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC
$ echo $GppOFlags
-std=c++98 -pedantic -ffor-scope -fno-gnu-keywords -foperator-
names -pipe -Wall -W -Woverloaded-virtual -Wno-sign-compare -Wno-
deprecated -Wno-non-virtual-dtor -Wpointer-arith -Wno-unused -Wno-
switch -Wno-missing-braces -Wno-long-long -static-libgcc -O3 -fomit-
frame-pointer -finline-functions

I'm not sure that all of them are (still) necessary, and I'm
probably missing a few useful ones because I use the same
environment variable with older versions of g++. (Note too that
at least one of the options, -Wno-long-long, is there to turn on
an extension.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 17 '08 #16
On Fri, 17 Oct 2008 06:03:41 -0700, James Kanze wrote:
On Oct 17, 8:52 am, Ian Collins <ian-n...@hotmail.comwrote:
>Stephen Horne wrote:

[...]
>gcc 4 will give the required diagnostic if you invoke it in a
conforming mode:
>g++ /tmp/x.cc -ansi -pedantic
/tmp/x.cc: In function 'int main()':
/tmp/x.cc:11: error: ISO C++ forbids variable-size array 'successors'

That works, but if I understand correctly, the exact meaning of -ansi
may change in time. A more precise replacement would be -std=c++98.
My understanding was that "-ansi" will always refer to the current
standard, which at this moment is the one enforced by -std=c++98...
having said which, I've just re-read the relevant section in the latest
docs and it doesn't actually say that. In fact it doesn't say anything
beyond "-ansi is equivalent to -std=c++98". Hmm...

--
Lionel B
Oct 17 '08 #17
On Oct 17, 3:51 pm, Lionel B <m...@privacy.netwrote:
On Fri, 17 Oct 2008 06:03:41 -0700, James Kanze wrote:
On Oct 17, 8:52 am, Ian Collins <ian-n...@hotmail.comwrote:
Stephen Horne wrote:
[...]
gcc 4 will give the required diagnostic if you invoke it in
a conforming mode:
g++ /tmp/x.cc -ansi -pedantic
/tmp/x.cc: In function 'int main()':
/tmp/x.cc:11: error: ISO C++ forbids variable-size array 'successors'
That works, but if I understand correctly, the exact meaning
of -ansi may change in time. A more precise replacement
would be -std=c++98.
My understanding was that "-ansi" will always refer to the
current standard, which at this moment is the one enforced by
-std=c++98... having said which, I've just re-read the
relevant section in the latest docs and it doesn't actually
say that. In fact it doesn't say anything beyond "-ansi is
equivalent to -std=c++98". Hmm...
But of course, that documentation is only valid for 4.3.x. I
would expect that when g++ gets around to supporting C++03,
-ansi would be the equivalent of -std=c++03. If the code you're
compiling was written for C++98, that wouldn't be what you want.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 17 '08 #18
In article <67**********************************@l76g2000hse. googlegroups.com>,
Prasad <pr**********@gmail.comwrote:
>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.
Don't!

Use either:

1- std::vector< >
(std::vector< std::vector< std::vector<int is perfectly legal)
2- new

There's very very little reasons to use malloc. Only in very specific
cases when you know that neither std::vector (your first port of call)
nor new will work.

Yannick
Oct 20 '08 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
10
by: Arthur J. O'Dwyer | last post by:
I'm seeing a bug at the moment that I can't track down. It's part of a moderately large program, but here is a small program that exhibits the bug on gcc. (The program code follows at the bottom...
4
by: Jim Strathmeyer | last post by:
Under what circumstances would closing a istream object (such as 'in.close()') SEGFAULT?
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
14
by: Donn Ingle | last post by:
Yo, An app of mine relies on PIL. When PIL hits a certain problem font (for unknown reasons as of now) it tends to segfault and no amount of try/except will keep my wxPython app alive. My first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.