sizeof([ALLOCATED MEMORY])
- From: "ballpointpenthief" <Matt.Smiglarski@xxxxxxxxx>
- Date: 2 May 2006 17:15:36 -0700
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
.
- Follow-Ups:
- Re: sizeof([ALLOCATED MEMORY])
- From: Chris McDonald
- Re: sizeof([ALLOCATED MEMORY])
- From: Richard Heathfield
- Re: sizeof([ALLOCATED MEMORY])
- Prev by Date: Re: (corr) counting with char
- Next by Date: Re: counting with char
- Previous by thread: counting with char
- Next by thread: Re: sizeof([ALLOCATED MEMORY])
- Index(es):
Relevant Pages
|