Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault with gets()

Reply
Thread Tools

segmentation fault with gets()

 
 
Peter Dragun
Guest
Posts: n/a
 
      10-19-2003
I am generally new to programming under Unix, but know how to code under
Windows. I have to create a simple program, that takes the following
information from a file using redirection:

4 4 4 4 4 4 5 5 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 10 10 10 1 1
3 3 3 3 3 3 3 3 3 2 2 2 12 12 12 1 7 7 7 7 4
12 13 13 5 5 5 3 3 9 7 7 7 5 4 4 4 3 3 3 3

read each line, and "compress the information" to the following out print to
be printed on screen:
6 4
6 5
8 3
3 2
3 10
2 1
9 3
3 2
3 12
4 7
1 4
1 12
2 13
3 5
2 3
1 9
3 7
1 5
3 4
4 3

So the first column counts the occurences, and the second column in the
integer occurence.
I keep on getting a segmentation fault when I run my program, here is the
source code:

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

/* NUMBER, and COUNT are used in numbers array, indicating what is stored
*where
*/
#define NUMBER 0
#define COUNT 1

/* boolean definitions*/
#define TRUE 1
#define FALSE 0

/*flag to make sure first entry is processes properly*/
int first_run = TRUE;

/* next=store the next value;
* prev= stores the previous value from the stream;
* count=keep track how many times the same number is in sequence;
* incr_x = keeps track where to store the numbers in the numbers array;
* incr = used to display the numbers array, when encoding is complete.
*/
int next, prev, count, incr_x, incr;

/* Stream string to store input to be encoded*/
char stream[900] = "4 4 4 4 4 2 2 6 6 6 6 6 6 6 6 6 4 4 4 4 4 10 10 10 7 7 7
7 7 4 4";

/* Special delimiter to differient between values, this array is used with
* strtok function, the delimiter is set to whitespaces, so when a
whitespace
* is found, it indicates that the next element is a number.
*/
char delimiters[2] = " ";

/* Token pointer stores the outcome created by strtok*/
char *token;

/* The numbers array stores the numbers and count of them
* A number is stored using numbers[incr][NUMBERS]=n, and the count of that
* number found in the sequence i stored by numbers[incr][COUNT]=c
*/
int numbers[100][2];

int main() {
/* Standard input, sending data to the steam array for processing.*/

// This is where i get the fault, the program works, if I take out
the while, and just use the gets, but it only gets one line from the
// file.

while( gets( stream ) != NULL ) {
/* Get the first element that is delimited by whitespace. */
token = strtok( stream, delimiters );

do {
next = atoi( token );

/* If it is a first run, their is not previous value, so we
* assign prev with next.
*/
if( first_run ) {
prev = next;
first_run = FALSE;
}

/* If next is equal to the previous value, we increment the
count.*/
if( next == prev ) {
count++;

/* If the next value does not equal the previous,
that indicates a new
* number was found in the stream, so we insert the previous
value
* into the numbers array, and store how many times it was found
* sequentially, and then we reset the count, and increment
the numbers
* array for the next value.
*/
} else {
numbers[incr_x][NUMBER] = prev;
numbers[incr_x][COUNT] = count;
count = 1;
incr_x++;
}
prev = next;
/* We stop when the stream is finished.*/
} while( ( token = strtok( NULL,delimiters ) ) != NULL );

/* Since the strtok ends the do.while loop, before we can
process the
* last number found, we do it here.
*/
numbers[incr_x][NUMBER] = next;
numbers[incr_x][COUNT] = count;
incr_x ++;
}
/* Print out our results. */
for( incr=0; incr < incr_x; incr++ ) {
printf( "%d %d\n", numbers[incr][COUNT],
numbers[incr][NUMBER] );
}
return 0;
}

I wrote a comment where, I think the fault is occurring. Essentially, the
instructions told us we could only use scanf and printf for our I/O, but I
couldn't see how to use scanf to get what I want, so I decided to side line
one spec, and use gets(). With that in mind, I am limited to the use of
functions, and do not want to stray further from using gets or scanf.

Thanks

--
Peter Dragun
Faculty of Computer Science 2nd year student
Western University, London, Ontario, Canada


 
Reply With Quote
 
 
 
 
Derk Gwen
Guest
Posts: n/a
 
      10-19-2003
# /* Standard input, sending data to the steam array for processing.*/
#
# // This is where i get the fault, the program works, if I take out
# the while, and just use the gets, but it only gets one line from the
# // file.
#
# while( gets( stream ) != NULL ) {

Have you verified the input doesn't overflow the buffer? There's a reason
most people used fgets instead.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
 
Reply With Quote
 
 
 
 
Peter Dragun
Guest
Posts: n/a
 
      10-19-2003
I have been reading upon, how dangerous gets() is, could you help me provide
an alternative using scanf or fgets(without reading a file, but still using
the stdin)?



"Derk Gwen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> # /* Standard input, sending data to the steam array for

processing.*/
> #
> # // This is where i get the fault, the program works, if I take

out
> # the while, and just use the gets, but it only gets one line from the
> # // file.
> #
> # while( gets( stream ) != NULL ) {
>
> Have you verified the input doesn't overflow the buffer? There's a reason
> most people used fgets instead.
>
> --
> Derk Gwen http://derkgwen.250free.com/html/index.html
> Mention something out of a Charleton Heston movie, and suddenly
> everybody's a theology scholar.



 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
"Peter Dragun" <(E-Mail Removed)_NOSPAM> wrote:

>I have been reading upon, how dangerous gets() is, could you help me provide
>an alternative using scanf or fgets(without reading a file, but still using
>the stdin)?


<snip>

....
#define BUFLEN 900
....
char stream[BUFLEN];
....
while( fgets( stream, BUFLEN, stdin ) {
....

HTH

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-19-2003
Peter Dragun wrote:

> I have been reading upon, how dangerous gets() is, could you help me
> provide an alternative using scanf or fgets(without reading a file, but
> still using the stdin)?


/* untested code - beware! */

#include <stdio.h>
#include <string.h>

int chop(char *s)
{
int chopped = 0;
char *p = strchr(s, '\n');
if(p != NULL)
{
*p = '\0';
chopped = 1;
}
return chopped;
}

#define SOME_SIZE_OR_OTHER 32

int main(void)
{
char buf[SOME_SIZE_OR_OTHER] = {0};

if(fgets(buf, sizeof buf, stdin) != NULL)
{
if(chop(buf))
{
printf("The string is [%s]\n", buf);
}
else
{
printf("The string was a tad long. Here's "
"some of it: [%s]\n", buf);
if(fgets(buf, sizeof buf, stdin) != NULL)
{
printf("Here's some more: %s\n", buf);
}
}
}
else
{
printf("EOF or error encountered.\n");
if(ferror(stdin))
{
printf("Error.\n");
}
else
{
printf("EOF.\n");
}
}
return 0;
}

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-19-2003
Irrwahn Grausewitz wrote:

> ...
> #define BUFLEN 900
> ...
> char stream[BUFLEN];
> ...
> while( fgets( stream, BUFLEN, stdin ) {


Better than BUFLEN: sizeof stream

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
Richard Heathfield <(E-Mail Removed)> wrote:

>Irrwahn Grausewitz wrote:
>
>> ...
>> #define BUFLEN 900
>> ...
>> char stream[BUFLEN];
>> ...
>> while( fgets( stream, BUFLEN, stdin ) {

>
>Better than BUFLEN: sizeof stream


Indeed.
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
"Peter Dragun" <(E-Mail Removed)_NOSPAM> wrote:

>I am generally new to programming under Unix, but know how to code under
>Windows. I have to create a simple program, that takes the following
>information from a file using redirection:
>

<snip>
>read each line, and "compress the information" to the following out print to
>be printed on screen:

<snip>

/* If this is homework, don't read any further!!!!
*
*
*
*
*
*
*
*
*
*
*
*
*
*/

#include <stdio.h>

int main( void )
{
int n = 0,
nn = 0,
cnt = 0,
run = 1;

while( run )
{
if ( scanf( "%d", &n ) != 1 )
run = 0;

if ( n != nn || !run )
{
if ( cnt )
printf( "%d %d\n", cnt, nn );
nn = n;
cnt = 1;
}
else
++cnt;
}
return 0;
}

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-19-2003
Irrwahn Grausewitz wrote:
>
> Richard Heathfield <(E-Mail Removed)> wrote:
>
> >Irrwahn Grausewitz wrote:
> >
> >> ...
> >> #define BUFLEN 900
> >> ...
> >> char stream[BUFLEN];
> >> ...
> >> while( fgets( stream, BUFLEN, stdin ) {

> >
> >Better than BUFLEN: sizeof stream

>

Also better would be ...
while( fgets( stream, sizeof stream, stdin )) {}
so that the right paren doesn't get lonely.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-20-2003
Peter Dragun wrote:
>
> I am generally new to programming under Unix, but know how to
> code under Windows. I have to create a simple program, that takes
> the following information from a file using redirection:
>
> 4 4 4 4 4 4 5 5 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 10 10 10 1 1
> 3 3 3 3 3 3 3 3 3 2 2 2 12 12 12 1 7 7 7 7 4
> 12 13 13 5 5 5 3 3 9 7 7 7 5 4 4 4 3 3 3 3
>
> read each line, and "compress the information" to the following
> out print to be printed on screen:
> 6 4
> 6 5
> 8 3
> 3 2
> 3 10
> 2 1
> 8 3
> .... snip ...
>
> So the first column counts the occurences, and the second column
> in the integer occurence.
> I keep on getting a segmentation fault when I run my program,
> here is the source code:
>

.... snip code using gets ...
>
> I wrote a comment where, I think the fault is occurring.
> Essentially, the instructions told us we could only use scanf
> and printf for our I/O, but I couldn't see how to use scanf to
> get what I want, so I decided to side line one spec, and use
> gets(). With that in mind, I am limited to the use of functions,
> and do not want to stray further from using gets or scanf.


Bad decision. Your instructor was leading you in the right
direction. Think about what scanf does ... it extracts a number
(or other things) from an incoming stream. It requires no extra
buffering of the incoming stream, and will skip all white space
while extracting numbers. It also tells you if it succeeded. So,
the phrase:

int anint;
....
if (1 == scanf(stdin, "%d", &anint)) {
/* whatever */
}

will extract a number and tell you if it succeeded. You can also
modify that into a while statement. Now think about how you can
tell if this is the same number as the previous one, and what to
do if it is, or if it isn't. Then think about how to get the
process started. You may well find you need a few extra variables
and initialization.

When you are finished you will be astounded at how simple it is.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
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