By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,989 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,989 IT Pros & Developers. It's quick & easy.

Segmentation fault with array

P: n/a
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

May 19 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
pt*****@gmail.com wrote:
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.

--
Ian Collins.
May 19 '07 #2

P: n/a
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

May 19 '07 #3

P: n/a
Michael wrote:
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.
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.

--
Three-Way Secret Hedgehog
A rock is not a fact. A rock is a rock.

May 19 '07 #4

P: n/a
pt*****@gmail.com wrote:
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.)
>
Pat
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
May 19 '07 #5

P: n/a
On 18 May 2007 22:28:50 -0700, pt*****@gmail.com wrote:
>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
May 19 '07 #6

P: n/a
pt*****@gmail.com wrote:
>
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(void) { /* added void for clarity */
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(EXIT_FAILURE); /* note change here **/
}
while (!feof(fp) && count < 10 ) { /* note change here **/
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]); /* clarity
edit */
count++;
}
}
}
i = 0;
printf("char 3 is %c\n", instring[i + 3]);
return 0;
}
I have edited your code slightly to reduce the vertical space.
Note the added comments. Search for '**/'. It is up to you to
ensure you do not write on unowned memory.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 20 '07 #7

P: n/a
On May 19, 12:23 am, Chris Dollin <e...@electrichedgehog.netwrote:
Michael wrote:
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.

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
May 20 '07 #8

P: n/a
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.

May 20 '07 #9

P: n/a
On May 19, 10:43 am, Ian Collins <ian-n...@hotmail.comwrote:
ptq2...@gmail.com wrote:
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.

--
Ian Collins.
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????

May 20 '07 #10

P: n/a

"Michael" <mc******@aol.comha scritto nel messaggio
news:11**********************@w5g2000hsg.googlegro ups.com...
On May 19, 12:23 am, Chris Dollin <e...@electrichedgehog.netwrote:
>Michael wrote:
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.

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?
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?
May 20 '07 #11

P: n/a

"Barry Schwarz" <sc******@doezl.netha scritto nel messaggio
news:mc********************************@4ax.com...
On 18 May 2007 22:28:50 -0700, pt*****@gmail.com wrote:
>>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.
What do you mean? printf("%c", '\n') is perfectly valid and does
exactly what it is supposed to do, as shown above.
May 20 '07 #12

P: n/a

<gu*******@gmail.comha scritto nel messaggio
news:11**********************@u30g2000hsc.googlegr oups.com...
On May 19, 10:43 am, Ian Collins <ian-n...@hotmail.comwrote:
>ptq2...@gmail.com wrote:
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.

--
Ian Collins.

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.
May 20 '07 #13

P: n/a
Michael wrote:
On May 19, 12:23 am, Chris Dollin <e...@electrichedgehog.netwrote:
>Michael wrote:
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.

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?
(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.
</rant>
Oh no! The ENTIRE STACK of commentary is unwinding! Gibble ... umph ...

!gnidniwun si yratnemmoc fo KCATS ERITNE htT !on hO

(fx:snip)

--
Far-Fetched Hedgehog
The shortcuts are all full of people using them.

May 20 '07 #14

P: n/a
On Sun, 20 May 2007 18:45:18 +0200, "Army1987" <pl********@for.it>
wrote:
>
"Barry Schwarz" <sc******@doezl.netha scritto nel messaggio
news:mc********************************@4ax.com.. .
>On 18 May 2007 22:28:50 -0700, pt*****@gmail.com wrote:
>>>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.

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
May 20 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.