Changeset 12447


Ignore:
Timestamp:
06/18/12 13:30:04 (13 years ago)
Author:
utke
Message:

should have read the manual page more carefully the first time around ... need the old size but also handling of the other corner cases.

Location:
issm/trunk-jpl/src/c/shared
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified issm/trunk-jpl/src/c/shared/Alloc/xNewDelete.h

    r12446 r12447  
    5959
    6060template <class T>
    61 T* xReNew(T* old, unsigned int size) {
     61T* xReNew(T* old, unsigned int old_size, unsigned int size) {
    6262#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
    63   assert(old);
    64   T* aT_p=xNew<T>(size);
    65   for (unsigned int i=0; i<size;++i)
    66     aT_p[i]=old[i];
    67   xDelete<T>(old);
     63  T* aT_p=NULL;
     64  if (!old) { // no old memory
     65    if (size) 
     66      aT_p=xNew<T>(size);
     67  }
     68  else { // have old memory
     69    if (!size)  // but 0 size
     70      xDelete<T>(old);
     71    else { // non-zero size
     72      if (size>old_size) { // do something only if it is bigger
     73        aT_p=xNew<T>(size);
     74        for (unsigned int i=0; i<old_size;++i)
     75          aT_p[i]=old[i]; // copy the items
     76        xDelete<T>(old);
     77      }
     78      else // size is equal or less than old size
     79        aT_p=old; // do nothing
     80    }
     81  }
    6882  return aT_p;
    6983#else
  • TabularUnified issm/trunk-jpl/src/c/shared/TriMesh/TriMeshUtils.cpp

    r12446 r12447  
    6666                                else{
    6767                                        /*Reallocate another max_number_elements slots in the GridElements: */
    68                                         GridElementsRealloc=xReNew<int>(GridElements,(current_size+max_number_elements));
     68                                        GridElementsRealloc=xReNew<int>(GridElements,current_size,(current_size+max_number_elements));
    6969                                        if (!GridElementsRealloc){
    7070                                                noerr=0;
Note: See TracChangeset for help on using the changeset viewer.