Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: "Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx>
- Date: Sat, 12 Aug 2006 23:55:06 GMT
Keith Thompson <kst-u@xxxxxxx> wrote in message
news:ln64gyvgtc.fsf@xxxxxxxxxxxxxxxxxx
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:Microsoft
Keith Thompson <kst-u@xxxxxxx> wrote in message[...]
news:lny7tuvrj4.fsf@xxxxxxxxxxxxxxxxxx
Mmmmm, well it's actually a C++ package (with a lot of "Object Pascal"
crap laying around apparently just in a vain attempt to create a
Nope, I don't think I ever do that. However, I do occasionallystyle monopoly--three guesses who made it), and I do call back and
forth between C and C++ and D----i, so maybe I DO want to keep
the "unneeded" casts...
If you have a genuine need to compile the same code as both C and C++,
that's a valid reason to cast the result of the *alloc() functions.
call malloc in a xxx.cpp file, which is compiled by C++...
Very very few people have such a genuine need.
Yes, hard to imagine what the point of that would be, 'cept maybe
even greater programming confusion than I have!
We can count the onesThe only libraries I provide are for myself, and as you note it is
we've seen here on the fingers of P.J. Plauger's right hand (and even
that's overkill).
C++ provides mechanisms for interfacing to C code. Unless you're
providing a library to be used with either C or C++ code, you're
probably better off picking a language for each piece of your program
and using the appropriate compiler for it.
generally fairly painless to call into C++ object files from C and vice
versa.
How about if I call it from C++ like you mentioned about malloc()?
I believe I actually do call malloc() in some xxx.cpp files...
Why? C++ has "new" and "delete".
Good question, maybe there was a good reason, maybe not, but
since I'm not looking at that particular code right now, it probably
had to do with keeping certain data structures as similar as possible
when used in C++ as they are when used in C, and something about
"new" just "scared" me...
But in any case, C++ is a differentWell, I didn't bring it up, but my code base is about 50/50...
language, and comp.lang.c++ down the hall on the left, just past the
water cooler.
Well, the Solaris man page would be just the old ucb man page,
Yes, it works with a cast. It also works without a cast, and there'sAgain, might be the C++ thing, or an urban legend or something...
just no reason to use one.
What you quoted above is not *the* documentation for qsort(). You'll
find that in the C standard, and it doesn't say anything about casting
arguments.
The Solaris man page has similar wording.
right? In any event, I am highly displeased with this particular
development
package, and high on my list of specific displeasures is the documentation.
It is in some cases wrong, many cases stupidly written, incomplete,
and just plain difficult to use. So I'm not at all surprised that they
included an unnecessary cast in the example, but at least the
example works, as I said...
I would think it unlikely it is broken, the package is irritatingly badOh, while I've got you here, here's another issue I noticed that I'm
not sure about concerning realloc(). Here's the NON-documentation:
Syntax
#include <stdlib.h>
void *realloc(void *block, size_t size);
...
If block is a NULL pointer, realloc works just like malloc.
...
I read this years ago, and thought "Great, I don't necessarily have to
malloc something first, I can use realloc in a loop and the first pass
through the loop it'll just be like malloc."
Yes. If it doesn't work that way, your implementation is broken.
(But that's an unlikely bug, since the behavior is clearly documented
in the standard.)
in many ways but seems to generally put out clean functioning programs
after fighting the "tools", but who knows. I may have just done something
stupid, wouldn't be the first time...
Maybe I'll try it again. I made the changes to my data downloadingProblem is, it didn't seem to work out that way, and I'm not sure
what I did wrong, but I think I tried a number of things, such as
explicitly initializing my memory pointer to NULL, and always got
an error...is it actually possible to use realloc() to act like malloc
with a NULL pointer?
Yes. I can't guess why you were unable to get it to work.
code yesterday, including deleting the "unnecessary casts", ran some tests,
everything worked fine, put it "into production", 6:15pm EST rolled
around and it did its thing apparently flawlessly, only about three
milliseconds quicker...
---
William Ernest Reid
.
- 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
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Prev by Date: Re: passing a union's field to a function
- Next by Date: Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- 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
|