Value of memory changed without permission

前端 未结 3 1172
旧时难觅i
旧时难觅i 2021-01-29 07:01

I have an array with 2 dimensions and when I print the data of the array the first time, the date is printed correctly, but the other times the data of array[last][i] from i = 0

相关标签:
3条回答
  • 2021-01-29 07:30

    An array of length numero has numero elements. Going from index 0 to numero-1. You are treating them like they have an index numero. Switch out i <= numero for i < numero. Do the same for all for loops and with j.

    0 讨论(0)
  • 2021-01-29 07:44

    The problem as I see it is, you're looping over with a condition check like

     for (unsigned int i = 0; i <= numero; i++) 
    

    for an array defined as

    unsigned int p_a[numero];
    

    and you're going off-by-one. This is essentially invalid memory access which invokes undefined behavior.

    C arrays have 0-based indexing, so the valid limit would be

    for (unsigned int i = 0; i < numero; i++)
    
    0 讨论(0)
  • 2021-01-29 07:57

    If you have an array declared as having numero elements then the valid range of indices is [0, numero-1].

    Thus such loops like this

    for (unsigned int i = 0; i <= numero; i++) {
    

    used to access elements of an array with numero elements results in undefined behavior.

    0 讨论(0)
提交回复
热议问题