sizeof([ALLOCATED MEMORY])



If I have malloc()'ed a pointer and want to read from it as if it were
an array, I need to know that I won't be reading past the last index.

If this is a pointer to a pointer, a common technique seems to be
setting a NULL pointer to the end of the list, and here we know that
the allocated memory has been exhausted. All good.

When this is a pointer to another type, say int, I could have a
variable that records how much memory is being allocated and use that
to track the size of the 'array'.
Alternatively, we could set the end of the 'array' to some kind of
error-code, such as 99 or MAX_INT.
I don't like either of these techniques.

So, what is a good way to stop a loop reading or writing past the
memory allocated to a pointer?
Or if possible, what is a good way of determining the size of memory
allocated to a pointer?

Cheers,
Matt

.



Relevant Pages

  • Re: HardBound and SoftBound (was "The State of Software")
    ... completely re-widen, to all of memory, and hence lose all protection. ... e.g. unless you know that malloc'ing is being done out of a common array ... I agree with Nick - SoftBound will fail to detect many common ... interpreted as a pointer across different architectures and memory models... ...
    (comp.arch)
  • Re: Problem with large arrays
    ... >am trying to using an array of signals that is just slightly larger) for the ... location of this very large memory. ... so that each pointer points to one row. ... row data structure and make the pointer point to it. ...
    (comp.lang.vhdl)
  • Re: gdb not catching out-of-bounds pointer
    ... that, for example, accesses one array from a pointer to another is ... provided the library writer knows what the compiler writer guarantees ... The portability of an allocator depends on the source of raw memory. ...
    (comp.unix.programmer)
  • Re: Out-of-bounds nonsense
    ... 6.5.6p8 of the C standard says about C pointer arithmetic. ... moving throught that array. ... The wording used in both standards makes sense only if the relevent ... pointer arithmetic within 'malloc'ed memory blocks (which naturally have no ...
    (comp.std.c)
  • Re: Problem with large arrays
    ... >>am trying to using an array of signals that is just slightly larger) for the ... > location of this very large memory. ... instead of signals I used variables and the snapshot was not any ... > so that each pointer points to one row. ...
    (comp.lang.vhdl)