What causes these default array values?

Tom Clifford

I've been working the the Kernighan/Ritchie C Programming Language book (2nd edition) and in the example in chapter 1.6, page 22 I've encountered some strange (to me at least) behaviour based on a mistake I made.

The mistake was that I declared int ndigit[10] but missed the for loop that set all the integers in the array to zero.

The strange thing is the first 8 values in the array seem to work correctly, defaulting to 0, but the last two default to -373429304 and 32766. This resulted in the digit counting function in the program to work as intended apart from the counts of 8s and 9s.

I worked out my mistake and fixed it, but I'm still curious as to why the first 8 values set to 0 but the last two were wildly different?

nio

I will add to the Martin Véronneau's comment with a nice answer,

What happens to a declared, uninitialized variable in C? Does it have a value?)

Assumed that you're on x86/x64 PC, if you declare int ndigit[10] inside of a function, and that it is stored on a stack. Stack is a part of a memory, that stores all function calls your program made before, return addresses, parameters to functions and local variables (not dynamically allocated).

When the function returns, the space is freed by adding a value to a stack pointer. Data in RAM stays there. There were some zero bytes from code execution before not related to your part of code you debugged and something wasn't zeroed which you had observed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What are the default values of the char array in Java?

From Dev

Create ZonedDateTime values with default values causes DateTimeParseException

From Dev

what are default integer values?

From Dev

What are the default values for StreamReader?

From Dev

What are the Default $PATH Values?

From Dev

What are the Default $PATH Values?

From Dev

Default values in array mongoose

From Dev

What is default timeout values for THTTPRIO

From Dev

What is the default values for Django fields?

From Dev

What are the default values for synclient parameters?

From Dev

Assigning values to an array after malloc causes crash

From Dev

Values in an array change due to unknown causes

From Dev

Adding values to array of structs causes Seg Fault

From Dev

PHP Function Array Default Values?

From Dev

What happens when extract() is used in an array with default key values (0,1,2,...)

From Dev

Is swift by default autorelease? What causes memory growth when the app is idle?

From Dev

How to generate an array using a default values array and a new values array?

From Dev

What are the default values for @QueryParam when @DefaultValue is not specified?

From Dev

What does "default values" do in sqlserver?

From Dev

What is the default values of KMP_AFFINITY variable?

From Dev

what are the possible options and default values for CJuiAutoComplete?

From Dev

What are nmap's default timing values?

From Dev

What causes this strange result in Python when assigning to values in a dictionary?

From Dev

What causes this strange result in Python when assigning to values in a dictionary?

From Dev

What is causing these array values to not match?

From Dev

Dictionary with String Array as Values Causes Duplicate Keys Error

From Dev

Dictionary with String Array as Values Causes Duplicate Keys Error

From Dev

How to initialize default float values in to array of structs?

From Dev

SwiftUI - Array default values when opening the view

Related Related

  1. 1

    What are the default values of the char array in Java?

  2. 2

    Create ZonedDateTime values with default values causes DateTimeParseException

  3. 3

    what are default integer values?

  4. 4

    What are the default values for StreamReader?

  5. 5

    What are the Default $PATH Values?

  6. 6

    What are the Default $PATH Values?

  7. 7

    Default values in array mongoose

  8. 8

    What is default timeout values for THTTPRIO

  9. 9

    What is the default values for Django fields?

  10. 10

    What are the default values for synclient parameters?

  11. 11

    Assigning values to an array after malloc causes crash

  12. 12

    Values in an array change due to unknown causes

  13. 13

    Adding values to array of structs causes Seg Fault

  14. 14

    PHP Function Array Default Values?

  15. 15

    What happens when extract() is used in an array with default key values (0,1,2,...)

  16. 16

    Is swift by default autorelease? What causes memory growth when the app is idle?

  17. 17

    How to generate an array using a default values array and a new values array?

  18. 18

    What are the default values for @QueryParam when @DefaultValue is not specified?

  19. 19

    What does "default values" do in sqlserver?

  20. 20

    What is the default values of KMP_AFFINITY variable?

  21. 21

    what are the possible options and default values for CJuiAutoComplete?

  22. 22

    What are nmap's default timing values?

  23. 23

    What causes this strange result in Python when assigning to values in a dictionary?

  24. 24

    What causes this strange result in Python when assigning to values in a dictionary?

  25. 25

    What is causing these array values to not match?

  26. 26

    Dictionary with String Array as Values Causes Duplicate Keys Error

  27. 27

    Dictionary with String Array as Values Causes Duplicate Keys Error

  28. 28

    How to initialize default float values in to array of structs?

  29. 29

    SwiftUI - Array default values when opening the view

HotTag

Archive