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




Keith Thompson <kst-u@xxxxxxx> wrote in message
news:ln64gyvgtc.fsf@xxxxxxxxxxxxxxxxxx
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:
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
Microsoft
style 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.

Nope, I don't think I ever do that. However, I do occasionally
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 ones
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.

The only libraries I provide are for myself, and as you note it is
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 different
language, and comp.lang.c++ down the hall on the left, just past the
water cooler.

Well, I didn't bring it up, but my code base is about 50/50...

Yes, it works with a cast. It also works without a cast, and there's
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.

Again, might be the C++ thing, or an urban legend or something...

The Solaris man page has similar wording.

Well, the Solaris man page would be just the old ucb man page,
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...

Oh, 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.)

I would think it unlikely it is broken, the package is irritatingly bad
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...

Problem 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.

Maybe I'll try it again. I made the changes to my data downloading
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



.



Relevant Pages

  • Re: Extra bytes
    ... malloc, assigning a new block of size bytes and returning a pointer to ... "The realloc function changes the size of an allocated memory block. ... The memblock argument points to the beginning of the memory block. ...
    (comp.unix.programmer)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... Bill Reid wrote in message ... If block is a NULL pointer, realloc works just like malloc. ...
    (comp.lang.c)
  • Re: Question about the *= (and similar) operator
    ... always return a pointer to fresh storage. ... calling realloc. ... Assume malloc works and then realloc fails. ... We'll have ptr set to NULL and no reference to the memory malloc gave us. ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I believe I actually do call malloc() in some xxx.cpp files... ... If block is a NULL pointer, realloc works just like malloc. ... I can use realloc in a loop and the first pass ...
    (comp.lang.c)
  • Re: vasam
    ... Here is the sequel of my previous post "Variable array sizes as members" ... I was given some advice on how to use malloc and realloc in oder to achieve ...
    (microsoft.public.vc.language)