-3

I have seen several posts on the dangers of WRITING outside of array boundaries. I was wondering though, is there any problem with READING outside of them? My reason for this is as follows:

I have commands and data in a randomly generated array, but sometimes the commands require uncertain amounts of data. Do I need to put checks in each command's subroutine so that data is not read from outside the string, or can I temporarily read from outside the array, and realloc later?

Dominic Mason
  • 307
  • 3
  • 8
  • See [Is accessing a global array outside its bound undefined behavior?](http://stackoverflow.com/q/26426910/1708801) ... although the question is specific to global arrays the bulk of the answers apply to all cases. – Shafik Yaghmour Oct 30 '15 at 17:09
  • I'm not sure, but i think you can use that in comparison, maybe I'm wrong. Take a look [HERE](http://stackoverflow.com/questions/33231470/how-to-find-actual-end-of-char-array-in-c-containing-zeros-in-the-middle/33232025#33232025). – Michi Oct 30 '15 at 17:26
  • Do the junk you read cause your programs to still take the proper actions ? e.g. you're not conditionally launching rockets based on what might be in that junk ? Since your program could just crash when you try to access indexes past an array, is that going to be a problem for your program ? – nos Oct 30 '15 at 17:59
  • @ alk. and Shafik: I saw that question and it did not say whether reading alone was dangerous. It said writing was dangerous. That is not the issue. – Dominic Mason Oct 31 '15 at 02:12

2 Answers2

2

According to the (draft) C standard (ISO 9899:201x) Annex J, undefined behaviour includes :

— Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary * operator that is evaluated (6.5.6).

— An array subscript is out of range, even if an object is apparently accessible with the given subscript

In C this expression a[2] is equivalent to *(a+2)

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
2

sometimes the commands require uncertain amounts of data

Initially, an amount of memory needs to be allocated based on the best guess (using malloc or calloc).

can I temporarily read junk from outside the array, and realloc later

As the program proceeds, the previous uncertainty about the amount of data presumably gives way to certainty as to how much new memory is needed to store new data.

Then, realloc needs to be called with the newly available information. But the program always needs to check the return values of alloc routines and keep proper accounting of the valid range(s) of memory blocks that it was given and should keep within those bounds.

Otherwise it would lead to undefined behavior

Better yet, you can use c++ standard library to do the memory management for you. The facilities provided by standard library allocate and expand memory as needed so you don't have to do it.

ramana_k
  • 1,933
  • 2
  • 10
  • 14