Sign in to follow this  
Minion

Simple C program seg faults

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[i] = 0;
	
	/* get chars */
	while ((c = getchar()) != EOF)
		++chars[c];
	
	/* which character appears most often? */
	for (i = 0; i < MAX_CHARS; ++i)
		if (chars[i] > charsmax)
			charsmax = chars[i];
	
	/* 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 ([pre]./a.out < charfreq.c[/pre]):
[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
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[i] > 0)
printf("%c | %3d\n", i, chars[i]);
}


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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this