The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
OTF values for a nearly-dynamic array....
MikeyPro


Joined: 14 Feb 2015
Posts: 15
Not SDL related, but programming related, does anybody find a major problem with this? Besides the obvious stack space use and possible invalid parameter types being cast to T type...

Code:

/*
   Creates an array of template type T of total size count, with count
   being no greater than MARR_BUFFER_SIZE-1, then fills the array
   values with the parameters given in the function parameter list.
*/

#define MARR_BUFFER_SIZE 2048
template <class T>
T* marr(int count, ...) {
   static T _buf[MARR_BUFFER_SIZE];
   memset(_buf, 0, sizeof(T)*MARR_BUFFER_SIZE);
   va_list options;
   va_start(options, count);
   for(int i = 0; i < count; ++i)
      _buf[i] = va_arg(options,T);
   va_end(options);
   return _buf;
}
MikeyPro


Joined: 14 Feb 2015
Posts: 15
I forgot to add at the beginning of the function to cap the count to the define(d) size:

Code:
 count = count >= MARR_BUFFER_SIZE ? MARR_BUFFER_SIZE : count;