• Advertisement
Sign in to follow this  

Simple C program seg faults

This topic is 4945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a problem with a simple program that should print a histogram displaying the frequency of different characters in its input. I would try using gdb, but it is not installed on my system at the moment, and it would take forever to reinstall the compiler. Here is the code:
#include <stdio.h>

#define MAX_CHARS	256	/* maximum number of chars to count */

/* 1-14; print a histogram showing the frequency of
	characters in input */
main()
{
	/* declare vars */
	int c, i, j, charsmax;
	int chars[MAX_CHARS];
	
	/* initialize vars */
	charsmax = 0;
	
	for (i = 0; i < MAX_CHARS; ++i)
		chars = 0;
	
	/* get chars */
	while ((c = getchar()) != EOF)
		++chars[c];
	
	/* which character appears most often? */
	for (i = 0; i < MAX_CHARS; ++i)
		if (chars > charsmax)
			charsmax = chars;
	
	/* now for the histogram */
	for (i = charsmax; i > 0; --i) {
		printf("%3d |", i);
		for (j = 0; i < MAX_CHARS; ++j) {
			if (chars[j] >= i)
				printf("   *");
			else printf("    ");
		}
		putchar('\n');
	}
	printf("     ");
	for (i = 0; i < MAX_CHARS; ++i)
		printf("----");
	putchar('\n');
	printf("     ");
	for (i = 0; i < MAX_CHARS; ++i)
		printf("%4d", i + 1);
	putchar('\n');
}
And the output when I run it on the source file (
./a.out < charfreq.c
):
[mark@hendrix ~/code/kandr]$ ./a.out <charfreq.c
129 |                                                                                                                                   *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               *   *   *       *   *       *                       *   *   *       *       *   *           *   *   *                                                                                                                                                                   *       *               *                       *               *       *       *                                       *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   Segmentation fault
[mark@hendrix ~/code/kandr]$
I'd appreciate any help. As I said I don't have access to gdb at the moment and my efforts of reviewing the code have not been productive. Thanks, Mark

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
> while ((c = getchar()) != EOF)
> ++chars[c];

Hmmmm... What happens if there are more than MAX_CHARS in the file?

Share this post


Link to post
Share on other sites
AP, you misread the code; MAX_CHARS is not the number of characters in the file (in fact there isn't even a file), it is the number of possible character values. The array chars[] is the count of all the occurences of a particular character, where the value of the character is the index into the array.

I suspect the problem is in your histogram output loop. I really can't tell what you're trying to accomplish here. I'd suggest something like this:


for(i = 0; i < MAX_CHARS; i++)
{
if(chars > 0)
printf("%c | %3d\n", i, chars);
}


Of course you can also do some sorting to get the list in highest-to-lowest ranking, etc.

Share this post


Link to post
Share on other sites
I think the loop for(j = 0; i < MAX_CHARS; ++j) is causing the error, because you're checking i<MAX_CHARS, not j<MAX_CHARS, but you're accessing chars[j].

Share this post


Link to post
Share on other sites
Quote:

Hmmmm... What happens if there are more than MAX_CHARS in the file?

Quote:

MAX_CHARS is not the number of characters in the file (in fact there isn't even a file), it is the number of possible character values. The array chars[] is the count of all the occurences of a particular character, where the value of the character is the index into the array.

ApochPiQ is correct here.
Quote:

I suspect the problem is in your histogram output loop. I really can't tell what you're trying to accomplish here.

The first for loop prints each row, and the second for loop checks whether or not char[j] (where j is an ascii value) appears at least i times in input. If so, I print an asterisk. If not, I print some blanks to keep the output lined up correctly.
Quote:

I think the loop for(j = 0; i < MAX_CHARS; ++j) is causing the error, because you're checking i<MAX_CHARS, not j<MAX_CHARS, but you're accessing chars[j].

That's exactly the problem. Thanks :) I don't know why I couldn't catch that... I appreciate all the help. Thanks guys. Thanks,
Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement