I love regex Find and Replace

Our code base is very old C in places, but compiled as C++. One of my bug bears has been the use of a malloc wrapper. The issue that push me over the edge was turning on heap debugging to find all memory been allocated by this function (well two: malloc and realloc).

So when I commented them out via some large Ctrl-K-C action and add some wrapper #define’s, the program broke, because the custom malloc’s zeroed the RAM, and the standard malloc set every thing to 0xCDCDCDCD.

new on the other hand does zero ram (via the constructor), and not trash vtables, etc, so with the help of global search and replace (Ctrl-Shift-H) and some regex \(:b*{:i}:b*\*:b*\):b*mem_alloc:b*\(:b*sizeof\(:b*:i:b*\):b*\) and replace with new \1() I have removed most of the evil, and can now hand check the rest.


    typeA *var = ( typeA *)mem_alloc( sizeof(typeA) ) ;


    typeA *var =  new typeA() ;

which is much better. Not having back tracking means you have to do each replace by hand as the following matches.

(typeA *)mem_alloc( sizeof( typeB ) ) ;

In this case (in my code) typeB is a local variable that is an array of typeA so this is fine, you just don’t want a new of typeB.

Now I’m getting bitten by a default constructor issue that I will also blog about.

p.s. fear not that I’m doing this in the main trunk. I am using my sandpit branch, and using the lovely merge functionality of subversion (via TortoiseSVN) to keep my trunk up to date.