1

I'm trying to learn C and have been trying a few exercises in a book I have found on the internet.

This question will probably be easy for you experts, What are the values displayed in the watch window when I watch some of the variables. I am referring to its state BEFORE a value is assigned to the variable. They are usually long numbers with about 10 digits and sometimes negative.

I know this may seem a silly question, but I am curious as to what they are. At first I thought they were addresses.

alk
  • 69,737
  • 10
  • 105
  • 255

2 Answers2

2

Since memory is not reset to a particular value when it is deallocated or reallocated, you will see whichever value was left there by the last code that was using that memory.

This can sometimes be a security issue. For example, if if that memory contained a password or other sensitive information. Hence for certain applications, it is important to zero-out the memory before freeing it.

Kariem
  • 750
  • 5
  • 13
2

Before you assign a value to a variable, it has whatever happened to be in that memory previously. It's very likely that the memory for a variable in one function was used for a variable of a different type in some other function. If the previous use was for a float or pointer, and now you're using it for an int, the value will look very random.

Barmar
  • 741,623
  • 53
  • 500
  • 612