Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?




Barry Schwarz <schwarzb@xxxxxxxxx> wrote in message
news:k93od21vgd6n6fhrg6tooem3r5j06ejrq4@xxxxxxxxxx
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.

Yeah, it's a little confusing, and not that relevant to what I'm
asking...the
bottom line is I want to separately sort two parts of a list...

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.

OK, that's the answer, I was just plain wrong that the memory
might not be contiguous...I've probably only read that guarantee
about 100000000000 times but just forgot it.

I think I got that confused with the idea that the re-allocated
block may have a different location than the original malloc, which
would mean...

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.

....you wouldn't want to dereference an address, right.

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.

OK, so this should be completely legal and flawless:

/* sort the symbol list alphabetically */
qsort((void *)curr_instrs,num_symbols,128,sort_alpha_list);

then...

/* sort the no-symbol list alphabetically */
qsort((void *)curr_instrs+num_symbols,num_no_symbols,128,sort_alpha_list);

First qsort() sorts down to the end of the symbols part of the list,
the second sorts down from the start of the no-symbols part of the
list to the end of the list. I guess it was the (void *) cast that scared
me...thanks.

---
William Ernest Reid



.



Relevant Pages

  • Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... of the items from the bottom up of the buffer, ... Then when I've read all four files, I sort the top and bottom items ... write the two sorted lists to two new files. ... I worry that if I just supply a pointer to the first item ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... of the items from the bottom up of the buffer, ... write the two sorted lists to two new files. ... I worry that if I just supply a pointer to the first item ... The block of memory whose non-NULL address is returned from ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... a buffer which I re-allocate the size as I read each file. ... of the items from the bottom up of the buffer, ... I worry that if I just supply a pointer to the first item ... The block of memory whose non-NULL address is returned from ...
    (comp.lang.c)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • PROBLEM SOLVED ! :D
    ... INITIALIZE AND FINALIZE THE COMPLETE STRUCTURE ?! ... because it uses raw pointers for the data pointer ... ManuallyInitializeData and ManuallyFinalizeData which represent external ... // of first generation and second generation lists:D ...
    (alt.comp.lang.borland-delphi)