[ODE] Re: ALLOCA

Steve Baker sjbaker1 at airmail.net
Fri Mar 5 15:58:49 MST 2004


Olivier Michel wrote:
> jon klein wrote:
> 
>> I use realloc under Windows without any problems (using MinGW) -- 
>> maybe the implementation of realloc you're using is faulty.
> 
> 
> No, this is the normal behavior of realloc. The returned pointer may be 
> different from the one passed as the first argument.

Right.  The idea is that you call realloc if you need more space.  If it
can do it, it'll extend the existing memory block because that's faster
than copying a potentially large amount of data off into a new, larger,
memory buffer.

However, in the general chaos of a dynamically allocated memory system,
there is no way to be sure that there is enough space to extend the existing
structure - so it may *have* to be copied.   If your program relies on the
address not changing, it *WILL* crash at some time in the future.

So - you should ALWAYS assume that the address you get back from realloc
will be different from the one you passed into it.   The rest is just a
matter of performance.

However - ODE is written in C++ - and it's EXTREMELY poor programming
practice to use malloc/calloc/realloc/strdup/free in C++.  You should
ALWAYS use the C++ new/delete operators instead.

I'm fairly horrified that we're even having this discussion!

---------------------------- Steve Baker -------------------------
HomeEmail: <sjbaker1 at airmail.net>    WorkEmail: <sjbaker at link.com>
HomePage : http://www.sjbaker.org
Projects : http://plib.sf.net    http://tuxaqfh.sf.net
            http://tuxkart.sf.net http://prettypoly.sf.net
-----BEGIN GEEK CODE BLOCK-----
GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M-
V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++
-----END GEEK CODE BLOCK-----



More information about the ODE mailing list