.
class pixel { public: pixel( ) : _i(0) {} void print() { cout << _i << endl ; } int _i ; }; //main program to call the array for 4 ints and return average int main() { static const int MAX_PIXELS = 10 ; int* buffer = new int[MAX_PIXELS] ; vector<int*> v ; for( int i = 0; i < MAX_PIXELS; i++ ) { int* p = new (buffer) int() ; *p = i ; v.push_back( p ) ; cout << "p = " << p << ", *p = " << *p << endl ; } cout << endl << "-------printing-------" << endl << endl ; for( i = 0; i < v.size(); i++ ) { cout << "v[i] = " << v[i] << ", *v[i] = " << *v[i] << endl ; } return 0 ; }
inline void* operator new( size_t sz, void* here ) { return here ; } inline void* operator new[]( size_t sz, void* here ) { return here ; }
for( int i = 0; i < MAX_PIXELS; i++ ) { int* p = new (buffer+i) int(i) ; // specify where you want the object v.push_back( p ) ; cout << "p = " << p << ", *p = " << *p << endl ; }
My requirement is: I need to create lots and lots of pixel objects (e.g. about 100 matrixs of 800x800 pixels each)
class pixel { public: pixel( ) : _x(0), _y(0) {} void print() { cout << this << " = " << _x << ',' << _y << endl ; } int _x, _y ; }; int main() { static const int MAX_PIXELS = 10 ; pixel* buffer = new pixel[MAX_PIXELS] ; vector<pixel*> v ; for( int i = 0; i < MAX_PIXELS; i++ ) { pixel* p = new (buffer) pixel() ; p->_x = i ; p->_y = MAX_PIXELS - i ; p->print() ; v.push_back( p ) ; } cout << endl << "-------printing-------" << endl << endl ; for( int j = 0; j < v.size(); j++ ) { cout << "v[" << j << "] = " ; v[j]->print() ; } return 0 ; }
.
#include <iostream> #include <vector> #include <new> using namespace std ; class pixel { public: pixel( ) : _x(0), _y(0) {} void print() { cout << this << " = " << _x << ',' << _y << endl ; } int _x, _y ; }; int main() { static const int MAX_PIXELS = 10 ; pixel* buffer = new pixel[MAX_PIXELS] ; vector<pixel*> v ; for( int i = 0; i < MAX_PIXELS; i++ ) { pixel* p = new (buffer+i) pixel() ; // buffer => buffer+i p->_x = i ; p->_y = MAX_PIXELS - i ; p->print() ; v.push_back( p ) ; } cout << endl << "-------printing-------" << endl << endl ; for( vector<pixel*>::size_type j = 0; j < v.size(); j++ ) { cout << "v[" << j << "] = " ; v[j]->print() ; } return 0 ; }
#include <iostream> #include <vector> #include <new> #include <memory> using namespace std ; class pixel { public: pixel( int xx=0, int yy=0 ) : _x(xx), _y(yy) {} void print() { cout << this << " = " << _x << ',' << _y << endl ; } int _x, _y ; }; struct pixel_allocator : public std::allocator<pixel> { enum { NUM_PIXELS = 1024*1024*32 }; // number of pixels pointer allocate( size_type N, const void* = 0 ) { void* pv = cnt<NUM_PIXELS ? buffer + cnt*sizeof(pixel) : 0 ; cnt += N ; return pointer(pv) ; } void deallocate( pointer ptr, size_type count ) {} // do nothing! void construct( pointer ptr, const pixel& val ) { new(ptr) pixel(val) ; } void destroy( pointer ptr ) { ptr->pixel::~pixel() ; } size_type max_size() const throw() { return NUM_PIXELS ; } static size_type cnt ; // number of pixels so far allocated static char* buffer ; // pointer to pre-allocated memory }; pixel_allocator::size_type pixel_allocator::cnt = 0 ; char* pixel_allocator::buffer = new char[ pixel_allocator::NUM_PIXELS * sizeof(pixel) ] ; int main() { static const int MAX_PIXELS = 10 ; vector<pixel,pixel_allocator> v ; for( int i = 0; i < MAX_PIXELS; i++ ) v.push_back( pixel( i, MAX_PIXELS - i ) ) ; for( vector<pixel>::size_type j = 0; j < v.size(); j++ ) { cout << "v[" << j << "] = " ; v[j].print() ; } return 0 ; }

pixel** ppp = new pixel* [ NUM_TRANSMITTERS ] ;
ppp[transmitter_number] = new pixel[npixels_in_this_transmitter] ;
| DaniWeb Message | |
| Cancel Changes | |