Segmentation fault with array

P

ptq2238

Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];

FILE * fp;

if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}

while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}

i=0;
printf("char 3 is %c\n",instring[i+3]);

return 0;
}

The testdata.txt contains
ab$
e.f
1we
@\/
"@'
:]_
..

When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring = $
count = 3, instring =

count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =

count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e
count = 11, instring =

count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =

count = 16, instring = "
count = 17, instring = @
count = 18, instring = '
count = 19, instring =

Segmentation Fault (core dumped)

My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work

Have I misunderstood arrays completely ?

Pat
 
I

Ian Collins

Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];

FILE * fp;

if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}

while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}

i=0;
printf("char 3 is %c\n",instring[i+3]);

return 0;
}


My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work

Have I misunderstood arrays completely ?
You have an array of single characters, which you then fill, and keep
writing to without checking if you have written past the end. It looks
like you intended to read lines, rather than single characters.
 
M

Michael

Ian answered your actual question, so let me point out another problem
in your code:
char c; ....
if ((c = getc(fp))==EOF)

The function getc returns an integer. I know, that's crazy, but
that's the way it is. (Not all standard library functions have good
interfaces.) The comparison to EOF can fail (I forget whether it
always does or only does on certain implementations) if c is a
character instead of an int.

Michael
 
C

Chris Dollin

Michael said:
Ian answered your actual question, so let me point out another problem
in your code:


The function getc returns an integer. I know, that's crazy, but
that's the way it is.

Suppose `getc` returned a `char`. What `char` value would you
pick to represent EOF and hence disallow from appearing in any
FILE* input?

Remember that FILE*s can be reading "binary" files.
 
B

Bart van Ingen Schenau

Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];

FILE * fp;

if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}

while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}

i=0;
printf("char 3 is %c\n",instring[i+3]);

return 0;
}

The testdata.txt contains
ab$
e.f
1we
@\/
"@'
:]_
.

When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring = $
count = 3, instring =

count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =

count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e
count = 11, instring =

count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =

count = 16, instring = "
count = 17, instring = @
count = 18, instring = '
count = 19, instring =

Segmentation Fault (core dumped)

My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work

Have I misunderstood arrays completely ?

What you seem to be missing is that in C it is *your* responsibility, as
a programmer, to ensure that you will not go outside the array bounds.
The compiler is not required (and sometimes even not able) to check that
you stay within array bounds.

In your code, you were happily clobbering over some random memory, until
the processor determined that something was seriously wrong. This is
perfectly acceptable behaviour if the programmer forgets his/her duty
to check array bounds. (By accessing memory outside the array bounds,
you invoke undefined behaviour. Anything the computer does then is
fully correct.)

Bart v Ingen Schenau
 
B

Barry Schwarz

Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char c;

The return value from getc needs to be an int, not a char.
int num=0;
int count=0;
int i;
char instring[10];

FILE * fp;

if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);

2 is not a portable return value from main. Use EXIT_FAILURE which is
a macro that each system defines consistent with your environment.
}

while(!feof(fp))

Since you test for EOF inside the loop, calling the function here is
superfluous.
{
num++;
if ((c = getc(fp))==EOF)

It is not guaranteed that the char c can hold the value EOF. It needs
to be an int.
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')

In a text file, it is unlikely that c will ever equal '\0''\.
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);

The fact that c was not equal to EOF or '\0' does not mean it is a
printable character. It could have been '\n' indicating end of line
(not end of file) or other common text markers such as '\t'.
count++;
}
}
}

The most likely cause of your segmentation faults is not insuring that
count never exceeds 9. Once it does and you attempt to store or print
instring[10], you have exceeded the array bounds and invoked undefined
behavior.
i=0;
printf("char 3 is %c\n",instring[i+3]);

return 0;
}

The testdata.txt contains
ab$
e.f
1we
@\/
"@'
:]_
.

When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring = $
count = 3, instring =

This is what happens when you attempt to printf a '\n' with %c.
count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =

count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e

Of course this element does not exist and everything from this point
on is undefined behavior.
count = 11, instring =

count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =

count = 16, instring = "
count = 17, instring = @
count = 18, instring = '
count = 19, instring =

Segmentation Fault (core dumped)

My question is if the instring has 10 elements then why is it printing
out to element 19 ?

Because the c language does not require bounds checking during
run-time. It was a design decision by the language developers to
leave this task to the programmer. Since you didn't, you experienced
to different possible manifestations of undefined behavior. The first
ten times it did something "almost reasonable". The next time it did
something so unreasonable your operating system had to intercede and
terminate your program.
If I put a large number, such as char instring[150], then it will work

It will work longer but if your file has more than 150 characters you
will end up with the same problem. What you really need to do is add
code to your while loop to check the value of count and do something
special (terminate with a warning message, reset count back to 0, etc)
when it exceeds the size of instring.
Have I misunderstood arrays completely ?

What did you expect to happen when you exceeded the array size?


Remove del for email
 
M

Michael

Suppose `getc` returned a `char`. What `char` value would you
pick to represent EOF and hence disallow from appearing in any
FILE* input?

Remember that FILE*s can be reading "binary" files.

--

Alternately, suppose you wrote a function where the description is
"read the next thing and return an int, not a char." Would you call
such a function getc?

Or suppose your description of your function was: sometimes return one
thing, sometimes return another (e.g., return a char except if you
don't, because you've reached end of file, or if an error occurs).
Would you think that, perhaps, a better interface was in order?

Or suppose you developed an interface that was commonly misused, and
in fact, was incredibly easy to misuse, because you had a combination
of: 1) outputs that mean different things, 2) a bad name, and 3) a
common misuse pattern that can not be caught by the compiler. If you
were the one to develop such an interface, I would blame you, not the
poor schmucks who were confused by your inanity. Although I suppose
that the typical thing is design such goofiness, document it, and them
blame the person who uses the interface for not reading the
documentation closely enough, regardless of how poorly the interface
is designed.

</rant>

Michael
 
P

ptq2238

Thank you for everyone's reply.
I've followed through with the suggestions and it's now working.
I think I would be better off by drawing up a diagram of my logic
first next time.
 
G

guru.jois

Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];
FILE * fp;
if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}
while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}
i=0;
printf("char 3 is %c\n",instring[i+3]);
return 0;
}
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?

You have an array of single characters, which you then fill, and keep
writing to without checking if you have written past the end. It looks
like you intended to read lines, rather than single characters.

HAI EVERYBODY. I AM NEW TO C. EVERYBODY ANSWERED TO QUESTION, BUT
NOBODY IS POINTING WHY HE/SHE HAS CHECKED FOR EOF TWICE IN THE
PROGRAM. IS IT NECESSARY????
 
A

Army1987

Michael said:
Alternately, suppose you wrote a function where the description is
"read the next thing and return an int, not a char." Would you call
such a function getc?

If you want to use fscanf(fp, "%c", &c) or fread(&c, 1, 1, fp) you
are free to use them. If you would like a function returning a
char, there would be no way to report EOF, unless it looked like
extern unsigned char get(FILE * restrict stream, int *flag), which
IMO is no improvement. So what's your point?
 
A

Army1987

Barry Schwarz said:
On 18 May 2007 22:28:50 -0700, (e-mail address removed) wrote:

This is what happens when you attempt to printf a '\n' with %c.

What do you mean? printf("%c", '\n') is perfectly valid and does
exactly what it is supposed to do, as shown above.
 
A

Army1987

Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];
FILE * fp;
if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}
while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}
i=0;
printf("char 3 is %c\n",instring[i+3]);
return 0;
}
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?

You have an array of single characters, which you then fill, and keep
writing to without checking if you have written past the end. It looks
like you intended to read lines, rather than single characters.

HAI EVERYBODY. I AM NEW TO C. EVERYBODY ANSWERED TO QUESTION, BUT
NOBODY IS POINTING WHY HE/SHE HAS CHECKED FOR EOF TWICE IN THE
PROGRAM. IS IT NECESSARY????
No, it isn't.
They did that because the OP's code did that.
 
C

Chris Dollin

Michael said:
Alternately, suppose you wrote a function where the description is
"read the next thing and return an int, not a char." Would you call
such a function getc?

(a) No. But `getc` doesn't return "an int". It returns the int value
of a char or EOF. Typically it returns a `char`. I'd call it
`getChar`, and consider giving it return type CharOrEOF, or
Optional(char), or char??, depending on the language I was
writing this function in, and its observed conventions.

(b) I note you didn't answer my question. Either reach-the-next
char returns a char, and so can't signal EOF in its return value,
or it returns a non-char, and so can't signal the type of
the interesting value it returns. Perfection is a vice.
Or suppose your description of your function was: sometimes return one
thing, sometimes return another (e.g., return a char except if you
don't, because you've reached end of file, or if an error occurs).
Would you think that, perhaps, a better interface was in order?

I'd think a better /description/ was in order. Or a better programming
language.
Or suppose you developed an interface that was commonly misused, and
in fact, was incredibly easy to misuse, because you had a combination
of: 1) outputs that mean different things, 2) a bad name, and 3) a
common misuse pattern that can not be caught by the compiler. If you
were the one to develop such an interface, I would blame you, not the
poor schmucks who were confused by your inanity. Although I suppose
that the typical thing is design such goofiness, document it, and them
blame the person who uses the interface for not reading the
documentation closely enough, regardless of how poorly the interface
is designed.

Were there more room for manoeuver in the existing code, yes. There
isn't. It's a shame, but there it is.

Oh no! The ENTIRE STACK of commentary is unwinding! Gibble ... umph ...

!gnidniwun si yratnemmoc fo KCATS ERITNE htT !on hO

(fx:snip)
 
B

Barry Schwarz

What do you mean? printf("%c", '\n') is perfectly valid and does
exactly what it is supposed to do, as shown above.
Of course it is.

Since the OP asked why his output was strange without specifying what
he found strange other than the segfault, I annotated everything I
thought a newcomer would be puzzled by.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top