Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 11 Aug 2006 06:41:22 GMT
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:
Barry Schwarz <schwarzb@xxxxxxxxx> wrote in message
news:k93od21vgd6n6fhrg6tooem3r5j06ejrq4@xxxxxxxxxx
On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"Yeah, it's a little confusing, and not that relevant to what I'm
<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.
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...
One thing that I found a little confusing in your original message is
that you talked about "re-allocated" memory, but you didn't mention
the "realloc" function. The more specific your description, the more
likely it is that we can help.
[...]
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);
Um, no.
Don't be afraid of whitespace. I put blanks around most operator
symbols, and after every comma. If I have to split something across
lines, that's ok. So I'd write your qsort call as:
qsort((void *)curr_instrs + num_symbols,
num_no_symbols,
128,
sort_alpha_list);
The third argument, 128, is a "magic number". It's very difficult to
tell what it means or whether it's even correct. Define a constant:
#define WHATEVER 128
so you only need to change it in one place (but pick a better name, of
course).
The first argument to qsort is:
(void *)curr_instrs + num_symbols
You can't do pointer arithmetic on a void* value. (Some compilers may
allow it; if you're using gcc, try "-ansi -pedantic -Wall -W", or
replace "-ansi" with "-std=c99").
If you're trying to get the address pointed to by curr_instrs plus an
offset of num_symbols bytes, you'll need to to the arithmetic using
char*:
qsort((char*)curr_instrs + num_symbols,
/* other args */);
assuming that curr_instrs isn't already a char*. Note that I didn't
cast the expression to void*; any pointer-to-object type can be
converted to void*, or vice versa.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- 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
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Barry Schwarz
- Re: 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: i=infinity;0= i*sin k*pi, 1=cos k*pi, k=m/n, n=4,m=0-00; c*G=20=const, 1/sgrt2>G>0.5, 6<N = NA ^2surf/NAvol<7 ; h/N =11=const, e+i*pi; D universe =f(h)*1/ (a))^4, T=f( m, S, D)
- Next by Date: Re: code portability
- Previous by thread: Re: 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
|