473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

segmentation fault with gets()

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
Nov 13 '05 #1
10 7207
# /* 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.
Nov 13 '05 #2
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" <de******@HotPOP.com> wrote in message
news:vp************@corp.supernews.com...
# /* 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.

Nov 13 '05 #3
"Peter Dragun" <pd*****@cogeco.ca_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
(ir*******@freenet.de)
Nov 13 '05 #4
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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #5
Irrwahn Grausewitz wrote:
...
#define BUFLEN 900
...
char stream[BUFLEN];
...
while( fgets( stream, BUFLEN, stdin ) {


Better than BUFLEN: sizeof stream

--
Richard Heathfield : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #6
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Irrwahn Grausewitz wrote:
...
#define BUFLEN 900
...
char stream[BUFLEN];
...
while( fgets( stream, BUFLEN, stdin ) {


Better than BUFLEN: sizeof stream


Indeed.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7
"Peter Dragun" <pd*****@cogeco.ca_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
(ir*******@freenet.de)
Nov 13 '05 #8
Irrwahn Grausewitz wrote:

Richard Heathfield <do******@address.co.uk.invalid> 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 ---
Nov 13 '05 #9
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #10
In article <J%********************@news04.bloor.is.net.cable. rogers.com>
Peter Dragun <pd*****@cogeco.ca_NOSPAM> writes:
... 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" ...
[in short, using a form of Run-Length Encoding; the output is
(N,value) pairs representing N instances of the given value].

Without answering questions about gets() (others have done that
already), I will note that the first thing I thought about when
I read the above is:

What if the input has the same integer value split across
several lines?

For instance, if the input is:

4
4
4 5
5 101 7

should the output be:

1 4
1 4
1 4
1 5
1 101
1 7

or should it be:

3 4
2 5
1 101
1 7

?

Later, you note:
the instructions told us we could only use scanf ...


which suggests (but does not say for certain) that the answer is
the latter. In particular, if you use scanf's "%d" format, the
scanf engine -- the code shared by scanf, fscanf, sscanf, and in
C99 the various vscanf routines -- will skip "white space", and
newlines are considered white space. Hence a loop of the form:

while (scanf("%d", &var) == 1)

will eat right through those newlines, producing the second kind
of output. (Note that scanf() will "jam" if it hits a non-numeric,
non-white-space character, so this kind of loop is certainly not
"robust" against malformed input. As a result you might actually
do better, in the end, using fgets(), unless of course you are Dan
Pop. :-) )

(The reason I say "does not say for certain" is that if you use
scanf with "%c" formats, and "manual" conversion of integer values
one digit at a time, you will be able to "see" the newlines in the
input stream. This again gives you the choice of whether to consider
newlines significant. Note, however, that the first form of output
does *not* allow you to reconstruct the original set of newlines.
If the RLE-output says "N1 X" followed by "N2 X" -- i.e., if the
value being run-length encoded repeats -- then you know for certain
there was a newline between these, but if the value after N2 is
not X, you do not know whether there was a newline there. That
makes it hard for me to imagine why anyone would *want* the first
form of output.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: bumps | last post by:
Guys, I am getting segmentation fault while I am trying to open a file in a small function. int PingSamplesList::saveListElements (char *outputFileString) { PingSampleElement *tmpPtr; int...
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
10
by: Linny | last post by:
Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This...
8
by: priyanka | last post by:
Hi, I am getting segmentation fault in a very simple thing. But I am not able to figure out waht is wrong. I should be missing something. char *kernel_rget_pt; char kernel_rec_buf = {'t',...
7
by: macaco | last post by:
Hi, I'm getting a segmentation fault on this code when it gets to fopen: tac.h: typedef struct { char* ventanaInspeccion; char* ventanaMemoria; FILE* archivo; } TAC;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.