Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A seg fault

Reply
Thread Tools

A seg fault

 
 
Albert
Guest
Posts: n/a
 
      01-17-2010
Why does this seg fault?

#include <stdio.h>

#define MAX 1000

int positions[MAX];
int nleaflets;

void storeinput () {
FILE *fin = fopen ("dropin.txt", "r");
int i;
fscanf (fin, "%d", &nleaflets);
for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);
}

int main () {
FILE *fout = fopen ("dropout.txt", "w");
int i, j, d, l, a, b, val, best, done[MAX][MAX];

return 0;
}
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-17-2010
Albert <(E-Mail Removed)> writes:

> Why does this seg fault?
>
> #include <stdio.h>
>
> #define MAX 1000
>
> int positions[MAX];
> int nleaflets;
>
> void storeinput () {
> FILE *fin = fopen ("dropin.txt", "r");
> int i;
> fscanf (fin, "%d", &nleaflets);
> for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);
> }
>
> int main () {
> FILE *fout = fopen ("dropout.txt", "w");
> int i, j, d, l, a, b, val, best, done[MAX][MAX];
>
> return 0;
> }


There's not much to go on but my first guess would be that you'd don't
have room to allocate 1,000,000 ints of automatic storage. It's odd
that the small array is static and the bug one is automatic.

--
Ben.
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      01-17-2010
On 17 Jan, 11:12, Ben Bacarisse <(E-Mail Removed)> wrote:
> There's not much to go on but my first guess would be that you'd don't
> have room to allocate 1,000,000 ints of automatic storage. *It's odd
> that the small array is static and the bug one is automatic.


Does that get some sort of award for "most appropriate typo"?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-17-2010
Paul N <(E-Mail Removed)> writes:

> On 17 Jan, 11:12, Ben Bacarisse <(E-Mail Removed)> wrote:
>> There's not much to go on but my first guess would be that you'd don't
>> have room to allocate 1,000,000 ints of automatic storage. *It's odd
>> that the small array is static and the bug one is automatic.

>
> Does that get some sort of award for "most appropriate typo"?


I think I've done better, you know. Since I have at least one per
line on average, the odds are better than you'd think!

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-17-2010
Albert <(E-Mail Removed)> writes:
> Why does this seg fault?
>
> #include <stdio.h>
>
> #define MAX 1000
>
> int positions[MAX];
> int nleaflets;
>
> void storeinput () {
> FILE *fin = fopen ("dropin.txt", "r");
> int i;
> fscanf (fin, "%d", &nleaflets);
> for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);
> }
>
> int main () {
> FILE *fout = fopen ("dropout.txt", "w");
> int i, j, d, l, a, b, val, best, done[MAX][MAX];
>
> return 0;
> }


It doesn't on my system.

Ben Bacarisse already pointed out that the "done" array may be too
big. Depending on your implementation, declaring it static might
help.

I also note that you never call storeinput(). You probably removed
the call while trying to reduce the program to a minimal example that
exhibits the problem. But if the "done" array is really the problem,
you could probably have removed the storeinput() function and most of
the rest of the code, eventualy giving you just:

#define MAX 1000

int main() {
int done[MAX][MAX];
return 0;
}

I think you just gave up too soon.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      01-17-2010

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Albert <(E-Mail Removed)> writes:
>> Why does this seg fault?
>>
>> #include <stdio.h>
>>
>> #define MAX 1000
>>
>> int positions[MAX];
>> int nleaflets;
>>
>> void storeinput () {
>> FILE *fin = fopen ("dropin.txt", "r");
>> int i;
>> fscanf (fin, "%d", &nleaflets);
>> for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);
>> }
>>
>> int main () {
>> FILE *fout = fopen ("dropout.txt", "w");
>> int i, j, d, l, a, b, val, best, done[MAX][MAX];
>>
>> return 0;
>> }

>
> It doesn't on my system.
>
> Ben Bacarisse already pointed out that the "done" array may be too
> big. Depending on your implementation, declaring it static might
> help.
>


<snip>

yep.

actually, this example is more likely to crash on Windows, and not crash on
Linux, since Linux tends to default to a bigger stack than Windows.

however, the mystery is:
this still should not crash right-off on Windows, as there should still be
around 194kB of stack left over, which does (presumably, it would crash
after a few more functions were called...).

then again, considering Windows memory default layout:
0x00000000-0x0000FFFF //unused, NULL page (Windows likes 64kB "pages").
0x00010000-0x0001FFFF //unused, trap page
0x00020000-0x003FFFFF //main stack

this leaves 63232 bytes free (61.75kB).

if I remember correctly, the TEB and TLS go on the stack in Windows 4kB or
8kB for TLS (Win32 or Win64), + ~4kB for the TEB (rough estimate).

this leaves ~49.75 kB.
- whatever is used in crtMainEntry (or whatever this is called, I forget).
- whatever is needed by 'fopen'.
....

so, yeah, it is very close to the edge here...

something likely happens somewhere to push it over the limit...


well, that, or Windows is just like OMG WTF?... when a function just goes
and tries to grab such a huge chunk of stack space all at once...



 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      01-18-2010
Keith Thompson wrote:
> Albert <(E-Mail Removed)> writes:
>> Why does this seg fault?
>>
>> #include <stdio.h>
>>
>> #define MAX 1000
>>
>> int positions[MAX];
>> int nleaflets;
>>
>> void storeinput () {
>> FILE *fin = fopen ("dropin.txt", "r");
>> int i;
>> fscanf (fin, "%d", &nleaflets);
>> for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);
>> }
>>
>> int main () {
>> FILE *fout = fopen ("dropout.txt", "w");
>> int i, j, d, l, a, b, val, best, done[MAX][MAX];
>>
>> return 0;
>> }

> <snip>
> ...[I]f the "done" array is really the problem,
> you could probably have removed the storeinput() function and most of
> the rest of the code, eventualy giving you just:
>
> #define MAX 1000
>
> int main() {
> int done[MAX][MAX];
> return 0;
> }
>
> I think you just gave up too soon.
>


The "done" array was really the problem.

I've been told to assume 8MB static memory for coding contests.
 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      01-18-2010

"Albert" <(E-Mail Removed)> wrote in message
news:KnO4n.1913$(E-Mail Removed)...
> Keith Thompson wrote:
>> Albert <(E-Mail Removed)> writes:
>>> Why does this seg fault?
>>>
>>> #include <stdio.h>
>>>
>>> #define MAX 1000
>>>
>>> int positions[MAX];
>>> int nleaflets;
>>>
>>> void storeinput () {
>>> FILE *fin = fopen ("dropin.txt", "r");
>>> int i;
>>> fscanf (fin, "%d", &nleaflets);
>>> for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);
>>> }
>>>
>>> int main () {
>>> FILE *fout = fopen ("dropout.txt", "w");
>>> int i, j, d, l, a, b, val, best, done[MAX][MAX];
>>>
>>> return 0;
>>> }

>> <snip>
>> ...[I]f the "done" array is really the problem,
>> you could probably have removed the storeinput() function and most of
>> the rest of the code, eventualy giving you just:
>>
>> #define MAX 1000
>>
>> int main() {
>> int done[MAX][MAX];
>> return 0;
>> }
>>
>> I think you just gave up too soon.
>>

>
> The "done" array was really the problem.
>
> I've been told to assume 8MB static memory for coding contests.


8MB stack is for Linux, but not for Windows, which uses 4MB...

it is also not uncommon to create additional threads with smaller stacks
(such as 256kB or 1MB), although this would normally be specified manually
by the app.


also, stack!=static...



 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      01-18-2010
On Jan 18, 2:34*am, Keith Thompson <(E-Mail Removed)> wrote:
> Albert <(E-Mail Removed)> writes:
> > Why does this seg fault?

> It doesn't on my system.
> [snip discussion of "optimizing" program to force seg fault]
> I think you just gave up too soon.


I've noticed a lot of callous, deliberate seg faulting
in this ng lately.

Perhaps some of you should read this:
http://xkcd.com/371/

James
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-18-2010
On 17 Jan, 10:09, Albert <(E-Mail Removed)> wrote:

> Why does this seg fault?


besides what everyone else has said there's a woeful lack of error
checking in this program

> #include <stdio.h>
>
> #define MAX 1000
>
> int positions[MAX];
> int nleaflets;
>
> void storeinput () {
> * * * * FILE *fin = fopen ("dropin.txt", "r");


check fopen()

> * * * * int i;
> * * * * fscanf (fin, "%d", &nleaflets);


check fscanf() (error recovery with fscanf() is a bit 'icky but that's
another issue)

> * * * * for (i = 0; i < nleaflets; i++) fscanf (fin, "%d", &positions[i]);


check fscanf(). Also check nleaflets is smaller than MAX

> }
>
> int main () {
> * * * * FILE *fout = fopen ("dropout.txt", "w");


check fopen()

> * * * * int i, j, d, l, a, b, val, best, done[MAX][MAX];


that's a lot of stuff to declare in main(). I pretty much think all
main() should do is parse the command line arguments and call one
function.

> * * * * return 0;

 
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
seg-fault on vector-auto-reserve Matthias Kaeppler C++ 2 02-27-2005 10:01 PM
seg fault on different linux boxes with C++. goktan C++ 4 08-02-2004 03:25 PM
fstream::open & seg fault Manuel Maria Diaz Gomez C++ 3 07-20-2004 06:18 PM
seg fault because stdexcept.cc not found? Corrine C++ 1 11-27-2003 06:15 AM
Suprising seg fault!!! Vinod C++ 3 09-27-2003 02:43 PM



Advertisments