Re: Question about the *= (and similar) operator
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Sat, 17 Jun 2006 09:12:40 -0400
CBFalconer wrote:
Keith Thompson wrote:Our realloc can be a misbehaving child and needs attention. Consider.."James Dow Allen" <jdallen2000@xxxxxxxxx> writes:... snip ...
Yes, but I still think a discussion of gcc's peculiar optimizationYou made three points in your original article (subject "gcc knows
re malloc() would be *more* interesting.
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.
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 ---
.
- Follow-Ups:
- Re: Question about the *= (and similar) operator
- From: CBFalconer
- Re: Question about the *= (and similar) operator
- References:
- Question about the *= (and similar) operator
- From: spibou
- Re: Question about the *= (and similar) operator
- From: James Dow Allen
- Re: Question about the *= (and similar) operator
- From: Keith Thompson
- Re: Question about the *= (and similar) operator
- From: James Dow Allen
- Re: Question about the *= (and similar) operator
- From: Keith Thompson
- Re: Question about the *= (and similar) operator
- From: CBFalconer
- Question about the *= (and similar) operator
- Prev by Date: Re: knowing exact string array length ?
- Next by Date: Re: Using static to init vars?
- Previous by thread: Re: Question about the *= (and similar) operator
- Next by thread: Re: Question about the *= (and similar) operator
- Index(es):
Relevant Pages
|