Re: Question about the *= (and similar) operator



CBFalconer wrote:
Keith Thompson wrote:
"James Dow Allen" <jdallen2000@xxxxxxxxx> writes:

... snip ...
Yes, but I still think a discussion of gcc's peculiar optimization
re malloc() would be *more* interesting.
You made three points in your original article (subject "gcc knows
about malloc()"):

1. gcc assumes that malloc()'s return value is not an alias for
another pointer.

Compilers are allowed to use knowledge of functions in the standard
library. It's a reasonably cool optimization, but there's not all
that much more to say about it.

Of course it can make that assumption. malloc, if successful,
always return a pointer to fresh storage. realloc may not, but any
copies of the original pointer have to be deemed invalid after
calling realloc.

Our realloc can be a misbehaving child and needs attention. Consider..

int *ptr;
ptr = malloc(100 * sizeof *prt);
ptr = realloc(ptr, 200 * sizeof *ptr);

Assume malloc works and then realloc fails. We'll have ptr set to NULL and no reference to the memory malloc gave us. A leak!

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.



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: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I believe I actually do call malloc() in some xxx.cpp files... ... What you quoted above is not *the* documentation for qsort. ... If block is a NULL pointer, realloc works just like malloc. ...
    (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: realloc() implicit free() ?
    ... >> another] what happens to the input block when realloc() finds it ... > you must instead use the output pointer value. ... The realloc function changes the size of the object pointed to by ... new, deallocate the old, and return a pointer to the new -- but I see ...
    (comp.lang.c)