[vox-tech] cygwin - segfault on array allocation

Micah J. Cowan micah at cowan.name
Wed Feb 8 12:15:52 PST 2006


On Wed, Feb 08, 2006 at 02:47:04PM -0500, Peter Jay Salzman wrote:
> On Wed 08 Feb 06, 11:29 AM, Micah J. Cowan <micah at cowan.name> said:
> > On Wed, Feb 08, 2006 at 12:12:19PM -0500, Peter Jay Salzman wrote:
> > > i'm finding that cygwin segfaults on a very simple program:
> > > 
> > >    #include <iostream>
> > >    using std::cout;
> > >    using std::endl;
> > >    // 50760 is the last
> > > 
> > >    int main( int argc, char *argv[] )
> > >    {
> > >       const long int N = strtol(argv[1], (char **)0, 0);
> > >       double a[N], b[N], c[N], d[N], ans[N];
> > > 
> > >       return 0;
> > >    }
> > 
> > First: note that the above is not portable code. C++ does not support
> > variable-length arrays (that is, the bracketed expression must be a
> > constant). As long as you're constraining yourself to g++, though, or
> > other ones that support this extension, you're fine. (VLAs /are/
> > supported in the current version of the ISO C Standard, BTW).
>  
> How does one gain this kind of knowledge at fingertips?

Er, one buys a copy of the appropriate standards, and spends some
quality time on comp.lang.c and comp.lang.c++ (not that I do that any
more). :-)  ...it also helps to be somewhat obsessive-compulsive about
standard-conformance (which you can acquire by spending time at the
above USENET groups :) ).

Electronic (PDF) copies of these standards are available for $18 a pop
from webstore.ansi.org. Seriously cool. I'd wait though: a new C++
standard should be out /relatively/ soon (months, I think). I don't
actually know, though: it may be out already (I haven't been keeping
tabs anymore). Scheduled features include automatic type deduction, so
you can stop saying:

  vector< double, My_alloc<double> >::const_iterator p = v.begin();

and use:

  auto p = v.begin();

instead!

Also, they've fixed the syntax so that, even if you wanted to write the
first one above, you can now use:

  vector<double, My_alloc<double>>::const_iterator p = v.begin();
                                ^^

which formerly would've been illegal because the two >s would have been
recognized as the right-shift operator >> instead of two closing
brackets for template parameter lists.

> > What line number? It didn't break at 7, it broke after you "stepped"
> > after 7, which means you don't necessarily know where it broke. I
> > usually let segfaulting programs dump core, and then use gdb to examine
> > where it was when it got the signal. I don't think you'll get very
> > useful information that way for this example, however.
> 
> Yeah, did that.  Didn't give any line number associated with the signal.  It
> actually said there were corrupted frames on the stack frame.

Oh. Yeah. Yeah, that makes sense.

-- 
Micah J. Cowan
micah at cowan.name


More information about the vox-tech mailing list