Simple C program seg faults

Started by
3 comments, last by Minion 19 years, 9 months ago
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
Advertisement
> while ((c = getchar()) != EOF)
> ++chars[c];

Hmmmm... What happens if there are more than MAX_CHARS in the file?
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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].
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

This topic is closed to new replies.

Advertisement