random size objects c++ -
i've been trying complete programming exercise involves allocating 10,000 objects of random sizes in [1000:0)-byte range using new, purpose time how long takes new allocate objects , how long takes delete deallocate them. wondering what's best way objects of random sizes? thought of 1 method i'm not sure if correct:
// object of varying size class object { public: object(int num_bytes) { vc.resize(num_bytes); } vector<char> vc; // char 1 byte }; int n = 10000; // number of objects allocate vector<object*> pointers; (int = 0; < n; ++i) { int size = randint(1, 1000); // random number between 1 , 1000 object* p = new object(size); pointers.push_back(p); // keep pointers objects in vector // can later deallocate }
for (int = 0; < n; ++i) { int size = randint(1, 1000); // random number between 1 , 1000 char* p = new char[size]; pointers.push_back(p); }
i think code need.
Comments
Post a Comment