0

I'm working through K&R exercise 1-13, and I forgot to set the elements in my array to 0. To my surprise, the last value that I got when printing the array was 32767; subsequent tests have different element values for the array, some different, and some not.

I'd like to know why this is happening. If it's highly complex, then what's going on in simple terms?

#include <stdio.h>

#define IN 1  /* inside a word */
#define OUT 0 /* outside a word */

/* print the length of words as input to a histogram with horizontal bars */
int main() {
  int c, i;
  int state = OUT;
  int accum = 0;
  int nchar[10];

  while ((c = getchar()) != EOF) {
    if (c != ' ' && c != '\n' && c != '\t') {
      state = IN;
      ++accum;
    }
    else {
      state = OUT;
      ++nchar[accum];
      accum = 0;
    }
  }
  for (i = 0; i < 10; ++i)
    printf("%d\n", nchar[i]);
  return 0;
}

Input & Corresponding Output:

hello codes

4195584
0
0
0
4196032
2
4195584
0
-1608045280
32767
2
  • 3
    It is called "undefined behaviour". Nobody has set any array values, so you can never say what will happen, although there may be times when it seems to be predictable. Commented Sep 6, 2015 at 20:02
  • 1
    I'm more worried about your use of accum. You never check when it reaches the array limit. Values 0 ..9 are ok, after that in extreme cases you may need to scrap the computer, since indexing nchar by 10 is very bad. while ((c = getchar()) != EOF && accum < 10) { Commented Sep 6, 2015 at 20:07

3 Answers 3

1

When the array is created, the compiler claims memory on the stack. Data is written to that memory location, if you are initializing the array or (in general) assigning values to it.

If you do not initialize anything, just memory is claimed, which was already used before for something else. The stack is not zeroed after data gets removed, because it would waste too much processor time and the RAM is getting filled with data again anyway.

Sign up to request clarification or add additional context in comments.

Comments

1

That's simply what happens when you don't initialize your memory. You get whatever was there before your program claimed it...

3 Comments

How is it determined which memory will be used?
That mostly depends the OS, but generally, you can't assume anything about what you're going to get.
Because of the way the stack works, you'll often end up with old values for variables that no longer exist because they've gone out of scope (like local variables for a finished function call). If you're unlucky, you may be "lucky" and "inherit" an initial value that suits you -- until a change in some other unrelated part of the code breaks everything. Bugs like that can be pretty hard to find (but the 'valgrind' tool can help you a lot).
1

Whatever the program that previously ran in your address space put there. So if a program put, say, 77, at address 0xabcd5657, and then you read that address, you'd get 77. This is because C does not zero initialize memory for you, although you can yourself with memset:

memset(nchar, 0, 10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.