Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Thu, 10 Aug 2006 21:57:59 -0700
On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"
<hormelfree@xxxxxxxxxxxxxxxx> wrote:
Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I read some
of the items from the bottom up of the buffer, and some from the
top down, moving the bottom items back to the new re-allocated
bottom on every file read.
I don't quite follow this description.
Then when I've read all four files, I sort the top and bottom items
separately using qsort(), which takes a pointer to a list of items, and
write the two sorted lists to two new files.
Problem is, I worry that if I just supply a pointer to the first item
in the bottom list to qsort(), it might point out to bozo-land during
the sort because I thought that dynamically re-allocated memory
is not necessarily contiguous. So I've done a little two step where
The block of memory whose non-NULL address is returned from
malloc/realloc/calloc is guaranteed to be contiguous. You memory is
allocated from address to address+size-1. Furthermore, calculating
the value address+size is always allowed but you may not dereference
this address.
I write the bottom list to another buffer to do the sorting and writing,
and everything works great, but I'm wondering if I'm wasting time
and worrying about nothing...after all, if I can't trust a pointer to an
arbitrary point in the list, how can I trust a pointer to the start of
the list?
Any light you can shed on how pointers are handled in dynamically
allocated memory would be interesting and helpful...thanks.
A pointer value between the limits mentioned above is within range of
the allocated memory. You have to insure alignment but if the pointer
has the correct type the compiler will do this for you.
Remove del for email
.
- Follow-Ups:
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- References:
- Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Prev by Date: Re: Need Help Declaring a Pointer to an Array of Structures
- Next by Date: Re: Newbie question - displaying trivia questions at random
- Previous by thread: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Next by thread: Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Index(es):
Relevant Pages
|