Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault

Reply
Thread Tools

segmentation fault

 
 
Piotr S.
Guest
Posts: n/a
 
      06-18-2006
What's wrong in following program:
#include <stdio.h>
#include <stdlib.h>
char s[80];
int main()
{
FILE *fp, *fopen();
if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
printf("%s\n",s);
}
}
I thought that buffor s is not properly declared, I had tried to declare
this variable
inside main(), and as char * s; Everytime runnig the program resulted in
segmentation fault.




 
Reply With Quote
 
 
 
 
rrs.matrix@gmail.com
Guest
Posts: n/a
 
      06-18-2006

Piotr S. wrote:
> What's wrong in following program:
> #include <stdio.h>
> #include <stdlib.h>
> char s[80];
> int main()
> {
> FILE *fp, *fopen();
> if(fp = fopen("plik1","r")== NULL)
> {
> printf("blad otwarcia"); exit(1);
> }
> else
> {
> printf("\notwarty\n");
> while(fgets(s,30,fp)== NULL)
> printf("%s\n",s);
> }
> }
> I thought that buffor s is not properly declared, I had tried to declare
> this variable
> inside main(), and as char * s; Everytime runnig the program resulted in
> segmentation fault.



if(fp = fopen("plik1","r")== NULL)
this statement is wrong. it should be
if((fp = fopen("plik1","r"))== NULL)

while(fgets(s,30,fp)== NULL)
this statement also is wrong.
it should be
while(fgets(s,30,fp)!= NULL)

the changed program would be.
#include <stdio.h>
#include <stdlib.h>
char s[80];
int main()
{
FILE *fp, *fopen();
if((fp = fopen("plik1","r"))== NULL)
{
printf("blad otwarcia"); exit(1);
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)!= NULL)
printf("%s\n",s);
}

}

 
Reply With Quote
 
 
 
 
Piotr S.
Guest
Posts: n/a
 
      06-18-2006

Uzytkownik <(E-Mail Removed)> napisal w wiadomosci
news:(E-Mail Removed) oups.com...


> if(fp = fopen("plik1","r")== NULL)
> this statement is wrong. it should be
> if((fp = fopen("plik1","r"))== NULL)
>
> while(fgets(s,30,fp)== NULL)
> this statement also is wrong.
> it should be
> while(fgets(s,30,fp)!= NULL)

Of course the last statement was logically wrong - would produce a not
expected output. With the first statement I was warned by the compiler:
"assignment to pointer variable without a cast". Thanks, it's working now!




 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Piotr S. wrote:
>
>> What's wrong in following program:
>> #include <stdio.h>
>> #include <stdlib.h>
>> char s[80];
>> int main()
>> {
>> FILE *fp, *fopen();
>> if(fp = fopen("plik1","r")== NULL)
>> {
>> printf("blad otwarcia"); exit(1);
>> }
>> else
>> {
>> printf("\notwarty\n");
>> while(fgets(s,30,fp)== NULL)
>> printf("%s\n",s);
>> }
>> }
>> I thought that buffor s is not properly declared, I had tried to
>> declare this variable inside main(), and as char * s; Everytime
>> runnig the program resulted in segmentation fault.

>
> if(fp = fopen("plik1","r")== NULL)
> this statement is wrong. it should be
> if((fp = fopen("plik1","r"))== NULL)
>
> while(fgets(s,30,fp)== NULL)
> this statement also is wrong.
> it should be
> while(fgets(s,30,fp)!= NULL)


I actually stared at the original without spotting that fault, only
the silly redeclaration of *fopen(). Once more the advantage of
writing comparisons to constants as:

if (NULL == (fp = fopen(...))) {

appears. With that construct it is hard to omit the parens.

However the compiler should have complained about the assignment of
an integer to a pointer anyhow.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      06-18-2006
On 2006-06-18, Piotr S. <(E-Mail Removed)> wrote:
> What's wrong in following program:

Lots.

> #include <stdio.h>
> #include <stdlib.h>
> char s[80];

Why are you using a global?

> int main()

int main (void) is better.

> {
> FILE *fp, *fopen();

*fopen()? Does that really compile unhindered? It doesn't make any sense,
as fopen() is already defined in stdio.h.

> if(fp = fopen("plik1","r")== NULL)
> {
> printf("blad otwarcia"); exit(1);

What's wrong with return 1; or better, return EXIT_FAILURE;.

> }
> else
> {
> printf("\notwarty\n");
> while(fgets(s,30,fp)== NULL)

While fgets returns a null string, print the null string? That can't be
what you want.

> printf("%s\n",s);
> }
> }


> I thought that buffor s is not properly declared, I had tried to declare
> this variable
> inside main(), and as char * s; Everytime runnig the program resulted in
> segmentation fault.
>


I can imagine. You shouldn't try to printf a NULL pointer.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      06-18-2006
Andrew Poelstra wrote:
> On 2006-06-18, Piotr S. <(E-Mail Removed)> wrote:
>> What's wrong in following program:

> Lots.
>
>> #include <stdio.h>
>> #include <stdlib.h>
>> char s[80];

> Why are you using a global?
>
>> int main()

> int main (void) is better.
>
>> {
>> FILE *fp, *fopen();

> *fopen()? Does that really compile unhindered? It doesn't make any
> sense, as fopen() is already defined in stdio.h.
>
>> if(fp = fopen("plik1","r")== NULL)
>> {
>> printf("blad otwarcia"); exit(1);

> What's wrong with return 1; or better, return EXIT_FAILURE;.
>
>> }
>> else
>> {
>> printf("\notwarty\n");
>> while(fgets(s,30,fp)== NULL)

> While fgets returns a null string, print the null string? That can't
> be what you want.
>
>> printf("%s\n",s);
>> }
>> }

>
>> I thought that buffor s is not properly declared, I had tried to
>> declare this variable
>> inside main(), and as char * s; Everytime runnig the program
>> resulted in segmentation fault.
>>

>
> I can imagine. You shouldn't try to printf a NULL pointer.


As he doesn't mention static, and shows no initialiser, I suspect it was a
random pointer he was writing to.

'notwarty' - love it - what language is this?


--
==============
Not a pedant
==============


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-19-2006
On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
<(E-Mail Removed)> wrote:

>On 2006-06-18, Piotr S. <(E-Mail Removed)> wrote:
>> What's wrong in following program:

>Lots.
>
>> #include <stdio.h>
>> #include <stdlib.h>
>> char s[80];

>Why are you using a global?
>
>> int main()

>int main (void) is better.
>
>> {
>> FILE *fp, *fopen();

>*fopen()? Does that really compile unhindered? It doesn't make any sense,
>as fopen() is already defined in stdio.h.


It is only declared in stdio.h. Since this declaration is at
function/block scope, it serves to temporarily replace the declaration
in stdio.h only for code in this function/block.

>
>> if(fp = fopen("plik1","r")== NULL)
>> {
>> printf("blad otwarcia"); exit(1);

>What's wrong with return 1; or better, return EXIT_FAILURE;.
>
>> }
>> else
>> {
>> printf("\notwarty\n");
>> while(fgets(s,30,fp)== NULL)

>While fgets returns a null string, print the null string? That can't be
>what you want.


It's even worse. While fgets returns a NULL pointer, attempting to
print the data pointed to invokes undefined behavior.

>
>> printf("%s\n",s);
>> }
>> }

>
>> I thought that buffor s is not properly declared, I had tried to declare
>> this variable
>> inside main(), and as char * s; Everytime runnig the program resulted in
>> segmentation fault.
>>

>
>I can imagine. You shouldn't try to printf a NULL pointer.



Remove del for email
 
Reply With Quote
 
jussij@zeusedit.com
Guest
Posts: n/a
 
      06-19-2006
Piotr S. wrote:

> With the first statement I was warned by the compiler:
> "assignment to pointer variable without a cast".


Which is exactly why it is never a smart idea to ignore
compiler warnings

Jussi Jumppanen
Author: Zeus for Windows IDE
http://www.zeusedit.com

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      06-26-2006
On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
<(E-Mail Removed)> wrote:

> On 2006-06-18, Piotr S. <(E-Mail Removed)> wrote:


> > int main()

> int main (void) is better.
>
> > {
> > FILE *fp, *fopen();

> *fopen()? Does that really compile unhindered? It doesn't make any sense,
> as fopen() is already defined in stdio.h.
>

Yes. It is a redeclaration of fopen with a type (function of
unspecified arguments returning pointer to FILE) that is compatible
with, though less specific than, the type specified by stdio.h
(function of two [in C99 restricted] pointers to const char returning
as above); redeclaring functions and for that matter nonauto objects
in this fashion is legal and no diagnostic is required, although
redeclaring standard functions anywhere is generally silly.

Nits: stdio.h declares but not defines it. And since standard headers
need not be actual files, I prefer to say 'by' rather than 'in'.

> > printf("%s\n",s);
> > }
> > }

>
> > I thought that buffor s is not properly declared, I had tried to declare
> > this variable
> > inside main(), and as char * s; Everytime runnig the program resulted in
> > segmentation fault.
> >

>
> I can imagine. You shouldn't try to printf a NULL pointer.


Not *printf as a string %s as here; nor an invalid (indeterminate)
pointer which it will be if declared 'inside main()' hence at block
scope and automatic, and without an initializer. Nor use it as a
pointer in any other way, like in the preceding (snipped) fgets().

You _can_ *printf the pointer itself if valid including null with %p.

- David.Thompson1 at worldnet.att.net
 
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
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments