0

Here is the code.

int main(int argc, char *argv[])
{
    char name[5];

    printf("%s\n",name);

    system("PAUSE");
    return 1;
}

The printf function outputs ©. Why does it use that character? In difference instances other characters are used. I'm trying to understand more about how memory works in C.

Cheers :)

EDIT - Thanks everyone. All your answers were very useful to me.

user84628
  • 37
  • 1
  • 1
  • 5
  • 2
    It's just whatever garbage was left in that memory by a previous program. – luser droog Apr 26 '13 at 12:00
  • 1
    an uninitialized object with automatic storage duration has indeterminate value. meaning it can have any value if it is not explicitly initialized – Koushik Shetty Apr 26 '13 at 12:01
  • Please read K&R for understanding C. It's not related to memory works, it's more related to how C works. After that you have a problem, we can help you. – abasu Apr 26 '13 at 12:03
  • @luserdroog: no sane "normal" operating system recycles the same stack for different programs, the garbage is likely to be left there by previous *function calls*. – Matteo Italia Apr 26 '13 at 12:48
  • @MatteoItalia I agree in general. But this program appears to make no previous function calls. – luser droog Apr 26 '13 at 13:21
  • 1
    @luserdroog: the `main` normally is not the real entrypoint of your executable; instead, your stack has already been used by libc/CRT initialization routines, hence why you often have an already "dirty" stack. Again, "normal" operating systems don't give to a process a page already used by another process for security reasons (and there's usually a low priority task in the kernel whose job is to wipe used memory pages). – Matteo Italia Apr 26 '13 at 13:29

4 Answers4

2

tl;dr: the value is undetermined.

Depending on the compiler, the value of the array is undetermined (most cases) or zero (some specific compilers that clean the stack).

Most often, the array is simply some memory space that is taken from the stack, so depending on what this area of memory was before you used it, you can have many different values in there.

Gui13
  • 12,993
  • 17
  • 57
  • 104
1

Since the array is declared in a function the initial value is undefined. If the array was declared global then it would have been initialized to all zeros. From the draft standard, section 6.7.9.10

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

Per the standard, it's undefined, which means it could be anything. If you want to make sure it's one specific value, you have to set it yourself (e.g. using memset()).

This doesn't mean some compilers may set it to a specific value.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

Usage of unitialized array is undefined behaviour, so pritntf call can lead to any results, including app crash. THru in a real life it will print some garbage from stack.

alexrider
  • 4,449
  • 1
  • 17
  • 27