Changeset 12448


Ignore:
Timestamp:
06/18/12 14:08:17 (13 years ago)
Author:
utke
Message:

replace NULL, put in some extra comments relating to the manual page; do the new/delete/assignments in all cases of nonzero sizes because the C++ ctor/dtor/assignment operators can have side-effects that likely should reflect the operations and not bypass them.

File:
1 edited

Legend:

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

    r12447 r12448  
    6161T* xReNew(T* old, unsigned int old_size, unsigned int size) {
    6262#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
    63   T* aT_p=NULL;
     63  T* aT_p=0;
    6464  if (!old) { // no old memory
    6565    if (size) 
    66       aT_p=xNew<T>(size);
     66      aT_p=xNew<T>(size); // according to realloc behavior in manual page
    6767  }
    6868  else { // have old memory
    6969    if (!size)  // but 0 size
     70      xDelete<T>(old); // according to realloc behavior in manual page
     71    else { // non-zero size
     72      assert(old_size); // have old memory - need to have old_size set or this call is bad
     73      // allocate new, delete old; ; even for the case when size is
     74      // less than old_size we can't just keep the memory unchanged
     75      // because otherwise classes that have ctors/dtors with side-effects
     76      // may misbehave, for example classes with static instance/operations counters.
     77      aT_p=xNew<T>(size);
     78      unsigned int iMax=(old_size<size)?old_size:size;
     79      for (unsigned int i=0; i<iMax;++i) {
     80        // we need to copy the items by explicit assignments
     81        aT_p[i]=old[i];
     82      }
    7083      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
    8084    }
    8185  }
    8286  return aT_p;
    83 #else
    84   T* aT_p=(T*)realloc((void*)old,size*sizeof(T));
    85   assert(aT_p);
     87#else
     88  T* aT_p=0;
     89  aT_p=(T*)realloc((void*)old,size*sizeof(T));
     90  if (size)
     91    assert(aT_p); // according to realloc behavior in manual page
    8692  return aT_p;
    8793#endif
Note: See TracChangeset for help on using the changeset viewer.