There were a lot of problems with this source that needing ironing out,
and I needed to start to somehwhere.
An *excellent* place to start is with a description of what
you want the program to do. As it is, you've just thrown a bunch
of code at us and expected us to divine your intentions. If the
code were not so clearly broken, divination might stand a chance;
even then it would be better to state your purpose. With broken
code, such a statement is even more important.
When I've got a program that
compiles, I like to have the magic numbers #defined.
Your choice, but you're making extra work for yourself. When
you're replacing numeric constants with macro names and you come
across a 3, should you change it to SHIFTS_PER_DAY or to VERSION?
Essentially, you've created a reverse-engineering problem for yourself,
giving yourself an extra opportunity to make misteaks.
I haven't really
done much C on a 64-bit processor, so my ballparking seems to be wanting.
Nothing in your code stands out as bitness-related.
#include <stdio.h>
#include <limits.h>
#define SIZE 20000000
#define SIZE2 (UCHAR_MAX+1)
int main(void)
{
int c;
long counter = 0;
long counter2 = 0;
long a[SIZE2];
long i, j;
FILE *fp;
fp = fopen("shoulder.wmv", "rb+");
Why the "+", since you never write to the file? And why
is there still no check for failure, after others have already
pointed out the lack?
printf("UCHAR_MAX + 1 is %d\n", SIZE2);
for (i = 0; i < (SIZE2); ++i) {
The parentheses around SIZE2 are harmless, but not helpful:
They're just unnecessary clutter that gives the reader more to
parse. Perhaps you think they're necessary because SIZE2 expands
to an expression rather than to a single token? But the expression
is already parenthesized (as good macros should be), so tacking on
an extra layer isn't needed. What you've written is equivalent to
for (i = 0; i < ((UCHAR_MAX+1)); ++i)
a = 0;
}
for (j = 0; j < SIZE; ++j) {
c = fgetc(fp);
counter++;
if (c != EOF) {
a[c] = ++a[c];
Undefined behavior: Two modifications of the same object
with no intervening sequence point.
} else
break;
}
for (i = 0; i < (SIZE2); ++i) {
printf("a[%ld] is %ld\n", i, a);
counter2 = counter2 + a;
}
printf("Counter reached %ld\n", counter);
printf("Counter2 reached %ld\n", counter2);
fclose(fp);
return 0;
}
// cc -Wall -Wextra -g hist3.c -o hist
$
I needed two different SIZES, one that would be larger than the byte
count of the file to be read and another that was one greater than
UCHAR_MAX.
SIZE2 (or equivalent) is necessary, yes. But SIZE seems to
serve no useful purpose -- unless, as mentioned above, you have
purposes that aren't stated.
"Things are seldom what they seem.
Skim milk masquerades as cream ..."