Dynamic Memory Allocation

Dynamic Memory Allocation

Following on from an earlier thread I thought I'd have a go at compiling Salome under Windows using using VS2003. I was trying a test compilation whilst building a template project, similar to that provided with OCC. However I came across an issue that I believe affects the Linux distributions.

I tried to compile the file SMESH_Controls.cxx and hit an error C2057: expected constant expression. The relevant code segment is shown below.

The problem lies with the statement double aLen[ nbNodes ];. This is using a static, compile time array definition to allocate memory dynamically. Whilst some compilers may accept this statement, according to various sources around the web the actual amount of memory is indeterminant - some say its likely to be zero which has the potenetial for significant buffer overruns.

Surely the correct C++ way to allocate memory is via new and delete operators, so that the line should read someting like double* aLen = new double [nbNodes];,with a corresponding delete [] aLen; before the routine exits. This is not the only instance I have found, but I don't yet know the size of the problem.

Is this going to be an issue with both the current distributions and future platforms, or am I missing something here in that standard C++ memory allocation syntax has been changed/amended on non-Windows platforms.

Re: Dynamic Memory Allocation

as the max limit can be inferred from the statement before. However, my main query is whether this was a standard programming practice in Salome - its a typical type of latent error that can corrupt stacks and heaps and, in my case, a source of compiler portability problems. In porting code, I don't really want to get into having to analyse the functions I'm compiling.

Using new and delete operators, for this type of data model can become irksome as you have to run a delete [] on every possible return path in the function to avoid memory leaks. Perhaps a simpler way is to write a wrapper class for this type of temp storage which can tidy up its memory in its destructor. Internally stl vectors would make a viable implementation although performance might be better with a simple linear array.

Any thoughts?

Pete

Re: Dynamic Memory Allocation

Dear Peter,
Using of stl vector it's the most universal solution.
I think, in Your context using of vertor/arrays( with not always using array element) is not in principal.
My argument for vector solution is follow:
if we'll change code:
if ( nbNodes != 3 && nbNodes != 4 )
return 0;
That we'll need for change of code
double aLen[4];
But is not change in case:
vector aLen(nbNodes)
Best Wishes,
Alexander

Re: Dynamic Memory Allocation

I think I understand. I'll give your suggestion a try - I don't yet know how many functions are affected but I was after a consistent technique that I could apply without having to think too much (plus it will teach me about STL and templates which aren't my strongest area!). If I come up with anything, is anyone else interested in the code?