I have a line of code that looks like this:

_data = new (nothrow) T[_size];

This code is inside a classe's constuctor
The problem is that it initializes each element to zero which I don't want it to do.
Is there to just have it reserve the memory but not set it to zero?

Recommended Answers

All 8 Replies

Why don't you want the memory set to zero?

Are you trying to extract some 'previous' value from the memory?

no, it's just that it takes up to much time, I'm writing a class that stores huge amounts of data and I don't need it set to zero because I set them up arbitrarily. Speed is very important here

If the data is large enough, I suppose it can take a while, but I've done some fairly large data sets and never really felt the need to 'speed up' allocation.

How much data are you allocating?

Have you measured how long it takes, if so how long does it take?

How often do you make this allocation?

(Speed is always important, but if you're not careful, you'll spend more man-hours on speed improvements than you would ever save.)

I'm testing this class by allocating all of it, heres the full class

template<class T>
class xarray {
  T *_data, *_next;
  unsigned int _size, _rsize;
  xarray<T> *next;
public:
  xarray(int x = 10) : _size(x), _rsize(x), _next(NULL)
  {
    _data = new (nothrow) T[_size];
    while (!_data)
    {
      _size /= 2;
      if (_size < 7) { cout << "ran out of all available memory" << endl; exit(1); }
      _data = new (nothrow) T[_size];
    }
    
    if (_rsize > _size)
    { 
      next = new xarray<T>(_rsize - _size, a);
    }
  }

I want to be able to allocate all available memory. So you see, I don't want it to be initialized to zero because that takes time

I don't see much point in allocating all available memory unless you're trying to stress-test another application, and if you're stress testing, I'm not sure what the hurry is.

I'm pretty sure you can override new to create custom allocators, but because this is a template, we don't know what we're allocating.

The language requires that the constructor be called for all of the array elements (I'm pretty sure this is why the new for the array is slower).

You might try using malloc to allocate the memory, you can allocate largish chunks and even if the memory is initialized to zero, it won't be calling multiple constructors.

I don't think your line 19 will compile, the xarray constructor I can see will accept either 0 or 1 argument. Your call is providing 2.

Add some debug:
* how long does it take to perform the first allocation?
* how big was the first allocation?
* how long does it take to perform the last allocation?
* how big was the last allocation?
* how long did it take overall for all of the allocations?

As I don't see that you're actually planning to use the data at all, why did you bother making a template with built-in recursive allocation, why not just a function to repeatedly allocate memory??

basically I want to be able to specify a xarray<int> of a humongous size dynamically.
If I were to try something like

int *pi = new int[45645*456456*45645*456456*456456];

I would get a runtime error, with this class I would be able to allocate all of that memory (well maybe not all that but you get the point), and since the class will take care of the indexes, it would act just like a regular array.
That is why I don't want it to initialize everything to zero, since I will use it like a regular array and since setting everything to zero takes away precious time, I'm looking for a way to get around it.

So your array class is intended to transparently represent the result of possibly many allocations as a single array. Due to the HUGE size of the array, it is absolutely CRITICAL that the allocation take as little time as possible.

Also, if your 'example' allocation which wants to allocate 198,145,029,864,623,361,045,926,400 array entries is even vaguely accurate, you have a much larger problem. Even if the allocation were only for 1 byte each it would take 198,145,029 terabytes (if I did the math right). I don't think you'll find that much memory (or even disk space) in any computer I've ever heard of.

If your example was 'over eager' and I were in your situation right now, I'd be looking at malloc (if it's available to you and the array will contain a 'standard' C type) but it would be a significant change from your current structure.

If your array won't fit in memory, you could end up virtualizing it to disk...but if you think calling constructors on memory was slow, disk access will not be a pleasant surprise.

I've asked questions several time attempting to get some real scope on the problem. I'm not putting any more of MY time into this unless you can show me hard data that its worth more than I've already invested. As I stated before, speed is always important, but if you're not careful, you'll spend more man-hours on speed improvements than you would ever save.

Questions I've asked that you've conveniently ignored:

How much data are you allocating?

How long does the entire allocation take?

If you haven't measured how long the entire allocation takes, measure at least a portion of the allocation required so we have some idea of the cost you are currently 'paying'.

How often do you make this allocation?

If the answer to the above is based on the number of times the program is run, how many times do you expect to run the program in the next 30 days? 90 days?

Best of luck, without hard data, I won't reply to this thread again.

This is just one of my projects that I'm focusing on right now, there isn't any timeframe on it. Ok, I'll show you what I have, here is my code in full:

#include <cstdlib>
#include <iostream>

#define _DEBUG 1
#define DEBUG if (_DEBUG)

using namespace std;

template<class T>
class xarray {
  T *_data;// *_next;
  unsigned int _size, _rsize;
  xarray<T> *_next;
public:
  xarray(int x = 10, int a) : _size(x), _rsize(x), _next(NULL)
  {
    cout << endl << "entering contructor" << endl;
    _data = new (nothrow) T[_size];
    while (!_data)
    {
      _size /= 2;
      if (_size < 7) { cout << "ran out of all available memory" << endl; exit(1); }
      _data = new (nothrow) T[_size];
    }
    DEBUG cout << "addr = " << _data <<  " size = " << _size << endl;
    if (_rsize > _size)
    { 
      DEBUG cout << endl << "starting again " << ++a; 
      _next = new xarray<T>(_rsize - _size, a);
    }
  }
  
  ~xarray() { cout << "hi" << endl; if (_next) _next->~xarray(); free(_data); }
  
  T &operator[](unsigned int a)
  {
    if (a > _rsize) throw -1;
    if (a > _size) { DEBUG cout << "next " << endl; return (*_next)[a]; }
    return _data[a];
  }
  T operator[](unsigned int a) const
  {
    if (a > _rsize) throw -1;
    if (a > _size) { DEBUG cout << "next " << endl; return (*_next)[a]; }
    return _data[a];
  }


};

int main(int argc, char *argv[])
{
  xarray<int> lr(429667295 * 3000, 1);
  
  cout << "before = " << lr[260000000] << endl; //255848100
//  lr[200000000] = 34;
  cout << "after =  " << lr[260000000] << endl;
//  linkray<int> l1(429667295 * 300, 0);
//  linkray<int> l2(429667295 * 30, 0);
//  linkray<int> l3(429667295 * 3, 0);
//  xarray<int> l2(10, 0);
  
//  cout << lr[429667295 * 3000] << endl;
/*  for (int i = 0; i <= 10000; i++){
    lr[i] = i * i;
    cout << lr[i] << endl;
  }
  lr[450000000] = 43;
  cout << lr [450000000] << endl;
  */
//  cout << endl << l2[4] << endl << sizeof(string) << endl;   
  
//  try {l2[2];}
//   catch(int){cout << "out of ranrage" << endl;}
   
  system("PAUSE");
  return EXIT_SUCCESS;
}

basically, it's like you said, I'm trying to create a transparent array-like class in where I would be able to access it like an array which wouldn't be possible to allocate in the classical sense of the word. This code initiates everything to zero which takes up time and isn't needed because I will treat this array like a regular array, namely, I'll set everything myself and won't mess with uninitialized indexes. BTW I appreciate the help, I know you're spending your own free time on this and appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.