Error in user guide

For general discussion related FlowStone
Post Reply
francoisreme
Posts: 29
Joined: Wed Feb 15, 2017 4:01 pm

Error in user guide

Post by francoisreme »

Hi folks.

I'm kind of surprised about these lines in flowstone user guide, and also with provided helper functions.
if we use a new [] we need to use delete[]. Could be serious memory issue, if you leak an array of size N at each function call...

Code: Select all

// Delete previous array and reset pOut entry
if( pOut[x] )
{
delete *((int**)&pOut[x]);
pOut[x] = 0;
}
// Create new array (assume we have declared 'length' – add 1 for size at front)
int* array = new int[length+1];
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Error in user guide

Post by trogluddite »

Unfortunately, the code examples in the FlowStone documentation and default toolbox items are often of a pretty poor standard. You're quite right of course, the C++ standard says that new[] -> delete is "undefined behaviour". In most compilers, it does work without leaking memory so long as the array contains only POD data types, as they have no destructors to call - so it's fortunate that the DLL API types are all POD (string arrays contains C-style strings which have to be freed manually)!

I've seen other coders do this for POD arrays, especially if they're regular C coders who haven't used C++ much. The compiler will optimise away destructor calls if they're not required, so there's no clever "hacky" reason to do it - it's really easy to get caught out by it if you change the array to contain objects, and it can get beginners into bad habits!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
francoisreme
Posts: 29
Joined: Wed Feb 15, 2017 4:01 pm

Re: Error in user guide

Post by francoisreme »

Didn't event know that (POD types) !
Post Reply