Personal tools
You are here: Home Forum Install & build Dynamic Memory Allocation

Dynamic Memory Allocation

Up to Install & build
  • Dynamic Memory Allocation

    Posted by Peter Dolbey at April 19. 2006

    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.

    /*
      Class       : AspectRatio
      Description : Functor for calculating aspect ratio
    */double AspectRatio::GetValue( const TSequenceOfXYZ& P )
    {
      int nbNodes = P.size();

      if ( nbNodes != 3 && nbNodes != 4 )
        return 0;

      // Compute lengths of the sides

      double aLen[ nbNodes ];
      for ( int i = 0; i < nbNodes - 1; i++ )
        aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
      aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );

    ...

    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.

    Pete

    • Re: Dynamic Memory Allocation

      Posted by Peter Dolbey at May 15. 2006

      Looks to me like the project team has gone to sleep again...

      Pete

      • Re: Dynamic Memory Allocation

        Posted by Sergey Mozokhin at May 16. 2006
        One way is using of stl vectors.
        This example of method static void sweepElement in file SMESH_MeshEditor.cxx illustrate this way:
        (Coment is old code)

        //list::const_iterator itNN[ nbNodes ];
        vector::const_iterator> itNN( nbNodes );

        //const SMDS_MeshNode* prevNod[ nbNodes ], *nextNod[ nbNodes ];
        vector prevNod( nbNodes );
        vector nextNod( nbNodes );

        Using of stl vectors is look like using of ordinary arrays. So we needs for change only initialization of arrays to vectors. That's all

        But in some case it's necessary use only dynamic array with new and delete operations.
      • Re: Dynamic Memory Allocation

        Posted by Sergey Mozokhin at May 16. 2006
        One way is using of stl vectors.
        This example of method static void sweepElement in file SMESH_MeshEditor.cxx illustrate this way:
        (Coment is old code)

        //list::const_iterator itNN[ nbNodes ];
        vector::const_iterator> itNN( nbNodes );

        //const SMDS_MeshNode* prevNod[ nbNodes ], *nextNod[ nbNodes ];
        vector prevNod( nbNodes );
        vector nextNod( nbNodes );

        Using of stl vectors is look like using of ordinary arrays. So we needs for change only initialization of arrays to vectors. That's all

        But in some case it's necessary use only dynamic array with new and delete operations.
        • Re: Dynamic Memory Allocation

          Posted by Peter Dolbey at May 16. 2006

          Thanks for joining Sergey.

          In the example I found, the resolution appears even simpler e.g.

          double aLen[4];

          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

            Posted by Alexander A. Borodin at May 16. 2006
            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

              Posted by Peter Dolbey at May 16. 2006

              Thanks Alexander,

              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?

              Pete

Powered by Ploneboard
Document Actions