|
Archives
D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger
C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows
digitalmars.empire
digitalmars.DMDScript
|
c++.windows.32-bits - How to delete a dynamically allocated multi-dimensional array?
↑ ↓ ← → Jens Friese <j.friese highfisch.de> writes:
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Nic Tiger" <nictiger progtech.ru> writes:
Just use reversed order (compared to what you have when allocated it)
for (i=0;i<4;i++) {
for (j=0;j<hsize[i];j++) {
delete xbuffer[i][j];
delete ybuffer[i][j];
delete zbuffer[i][j];
}
delete xbuffer[i];
delete ybuffer[i];
delete zbuffer[i];
}
delete xbuffer;
delete ybuffer;
delete zbuffer;
As these arrays are simple type, you can simply use free() function instead
of delete operator.
Nic Tiger.
"Jens Friese" <j.friese highfisch.de> ???????/???????? ? ???????? ?????????:
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → Jens Friese <j.friese highfisch.de> writes:
Thank you so much for helping me, Nic!
Jens
Nic Tiger wrote:
Just use reversed order (compared to what you have when allocated it)
for (i=0;i<4;i++) {
for (j=0;j<hsize[i];j++) {
delete xbuffer[i][j];
delete ybuffer[i][j];
delete zbuffer[i][j];
}
delete xbuffer[i];
delete ybuffer[i];
delete zbuffer[i];
}
delete xbuffer;
delete ybuffer;
delete zbuffer;
As these arrays are simple type, you can simply use free() function instead
of delete operator.
Nic Tiger.
"Jens Friese" <j.friese highfisch.de> ???????/???????? ? ???????? ?????????:
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Matthew Wilson" <dmd synesis.com.au> writes:
I'm sorry, but you cannot possibly justify allocating a resource with one
API and deallocating it with another, particularly when the language
standard proscribes such action.
It's bad practise. It's not maintainable. It's a bad example. It contravenes
the standard. It has no benefit whatsoever.
This is wrong, wrong, wrong!
Soap-box Simon
"Nic Tiger" <nictiger progtech.ru> wrote in message
news:b5v9ml$1u8g$1 digitaldaemon.com...
Just use reversed order (compared to what you have when allocated it)
for (i=0;i<4;i++) {
for (j=0;j<hsize[i];j++) {
delete xbuffer[i][j];
delete ybuffer[i][j];
delete zbuffer[i][j];
}
delete xbuffer[i];
delete ybuffer[i];
delete zbuffer[i];
}
delete xbuffer;
delete ybuffer;
delete zbuffer;
As these arrays are simple type, you can simply use free() function
of delete operator.
Nic Tiger.
"Jens Friese" <j.friese highfisch.de> ???????/???????? ? ????????
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Nic Tiger" <nictiger progtech.ru> writes:
Ok, than I suggest to allocate arrays also via malloc() rather than new
operator, as long as the only difference between them is that new calls
constructor afterwards.
Well, in general I admit that using different APIs is not good. Thanks for
correction.
Nic Tiger.
"Matthew Wilson" <dmd synesis.com.au> сообщил/сообщила в новостях следующее:
news:b5vpl0$2cqn$1 digitaldaemon.com...
I'm sorry, but you cannot possibly justify allocating a resource with one
API and deallocating it with another, particularly when the language
standard proscribes such action.
It's bad practise. It's not maintainable. It's a bad example. It
the standard. It has no benefit whatsoever.
This is wrong, wrong, wrong!
Soap-box Simon
"Nic Tiger" <nictiger progtech.ru> wrote in message
news:b5v9ml$1u8g$1 digitaldaemon.com...
Just use reversed order (compared to what you have when allocated it)
for (i=0;i<4;i++) {
for (j=0;j<hsize[i];j++) {
delete xbuffer[i][j];
delete ybuffer[i][j];
delete zbuffer[i][j];
}
delete xbuffer[i];
delete ybuffer[i];
delete zbuffer[i];
}
delete xbuffer;
delete ybuffer;
delete zbuffer;
As these arrays are simple type, you can simply use free() function
of delete operator.
Nic Tiger.
"Jens Friese" <j.friese highfisch.de> ???????/???????? ? ????????
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Matthew Wilson" <dmd synesis.com.au> writes:
Don't have a problem with that, other than that it is more work and more
scope for error. I am sceptical that there is much, if any, performance gain
to be had from going straight to malloc()/free() rather than new/delete.
Nonetheless, I am not a religious adherent to the notion that every mem
alloc in C++ should be through new. My only objection was your mixing APIs,
which is guaranteed to fail on many compilers, even if it works with DMC++
:)
Matthew
"Nic Tiger" <nictiger progtech.ru> wrote in message
news:b60kl4$31il$1 digitaldaemon.com...
Ok, than I suggest to allocate arrays also via malloc() rather than new
operator, as long as the only difference between them is that new calls
constructor afterwards.
Well, in general I admit that using different APIs is not good. Thanks for
correction.
Nic Tiger.
"Matthew Wilson" <dmd synesis.com.au> сообщил/сообщила в новостях
news:b5vpl0$2cqn$1 digitaldaemon.com...
I'm sorry, but you cannot possibly justify allocating a resource with
API and deallocating it with another, particularly when the language
standard proscribes such action.
It's bad practise. It's not maintainable. It's a bad example. It
the standard. It has no benefit whatsoever.
This is wrong, wrong, wrong!
Soap-box Simon
"Nic Tiger" <nictiger progtech.ru> wrote in message
news:b5v9ml$1u8g$1 digitaldaemon.com...
Just use reversed order (compared to what you have when allocated it)
for (i=0;i<4;i++) {
for (j=0;j<hsize[i];j++) {
delete xbuffer[i][j];
delete ybuffer[i][j];
delete zbuffer[i][j];
}
delete xbuffer[i];
delete ybuffer[i];
delete zbuffer[i];
}
delete xbuffer;
delete ybuffer;
delete zbuffer;
As these arrays are simple type, you can simply use free() function
of delete operator.
Nic Tiger.
"Jens Friese" <j.friese highfisch.de> ???????/???????? ? ????????
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Rajiv Bhagwat" <dataflow vsnl.com> writes:
Just curious: where are you using a structure which requires a pointer to a
pointer to a pointer?
Would suggest to wrap up allocations and releases in a class constructor &
destructor (as advocated by www.relisoft.com) , so that compiler worries
about freeing in the right order and right amount, not you.
- Rajiv
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → Jens Friese <j.friese highfisch.de> writes:
Hi everybody,
I hardly dare to say that I have no idea
what pointers are. I just have to create
this three-dimensional array during runtime
because it uses up to 800 mb (!) of memory.
Don't tell me that's too much - I do really need it ;)
Someone told me, to allocate memory this way -
and it works fine for me...
Can you tell me an equally easy method to do so
in a better way? I would be happy if you could
tell me about this destructor thing too.
Jens
Rajiv Bhagwat wrote:
Just curious: where are you using a structure which requires a pointer to a
pointer to a pointer?
Would suggest to wrap up allocations and releases in a class constructor &
destructor (as advocated by www.relisoft.com) , so that compiler worries
about freeing in the right order and right amount, not you.
- Rajiv
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Rajiv Bhagwat" <dataflow vsnl.com> writes:
Well, any book on 'c' will tell you about arrays and pointers. Any book on
'c++' will tell you about classes, their constructors and destructors. Lot
of sites about learning these languages will also explain these to you.
(Search using Google.com).
Best of luck,
- Rajiv
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8404A3.2000108 highfisch.de...
Hi everybody,
I hardly dare to say that I have no idea
what pointers are. I just have to create
this three-dimensional array during runtime
because it uses up to 800 mb (!) of memory.
Don't tell me that's too much - I do really need it ;)
Someone told me, to allocate memory this way -
and it works fine for me...
Can you tell me an equally easy method to do so
in a better way? I would be happy if you could
tell me about this destructor thing too.
Jens
Rajiv Bhagwat wrote:
Just curious: where are you using a structure which requires a pointer
pointer to a pointer?
Would suggest to wrap up allocations and releases in a class constructor
destructor (as advocated by www.relisoft.com) , so that compiler worries
about freeing in the right order and right amount, not you.
- Rajiv
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "Matthew Wilson" <dmd synesis.com.au> writes:
Please don't apologise for lack of understanding. This community is very
friendly, so please feel at ease. (Plus, you should see the number of stupid
questions I pose on the D newsgroup. Ouch!)
In answer to your question, the STLSoft library (http://stlsoft.org/) has a
dynamically-sized (as opposed to sized at compile time) but not resizable
(once created, it cannot be resized) 3 dimensional array called
fixed_array_3d. (There are fixed_array_1d, _2d and _4d also.)
It supports multi-dimensional array notation, which is unchecked (though
asserts are used in debug mode), as well as checked access via the at(d0,
d1, d2) method
You would use it like
#include <stlsoft.h>
#include <stlsoft_fixed_array.h>
int main(int /* argc */, char ** /*argv*/)
{
typedef stlsoft::fixed_array_3d<float> float_array_t;
float_array_t array(4, 10, 20);
array[0][1][2] = 12345.6789;
for(int i = 0; i < dimension0(); ++i)
{
for(int j = 0; j < dimension1(); ++j)
{
for(int k = 0; k < dimension2(); ++k)
{
assert(array.at(i, j, k) == array[i][j][k]);
}
}
}
}
Only one allocation is performed for the entire array, which means your use
of memory would be more efficient both from a performance and size
perspective. However, since the [] operators involve the creation of
temporaries of fixed_array_2d and fixed_array_1d, there is a small
performance penalty in their use. at() is more efficient, but still involves
checks for the validity of the given indexers. I've plans to make an
unchecked_at(d0, d1, d2) which would be extremely efficient, but I've not
yet got round to it. Maybe if a user makes a request ... ;)
Hope that helps
Matthew
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8404A3.2000108 highfisch.de...
Hi everybody,
I hardly dare to say that I have no idea
what pointers are. I just have to create
this three-dimensional array during runtime
because it uses up to 800 mb (!) of memory.
Don't tell me that's too much - I do really need it ;)
Someone told me, to allocate memory this way -
and it works fine for me...
Can you tell me an equally easy method to do so
in a better way? I would be happy if you could
tell me about this destructor thing too.
Jens
Rajiv Bhagwat wrote:
Just curious: where are you using a structure which requires a pointer
pointer to a pointer?
Would suggest to wrap up allocations and releases in a class constructor
destructor (as advocated by www.relisoft.com) , so that compiler worries
about freeing in the right order and right amount, not you.
- Rajiv
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8318B4.20307 highfisch.de...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
...
void main(int argc, char* argv[]){
...
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
...
Any suggestions? Thanks a lot! :)
Best wishes
JENS
↑ ↓ ← → "KarL" <someone somewhere.org> writes:
This sounds like a more like a DBMS or multi processing issue
than mere 3D array.
Any possibility that your 3D array could be sparse array?
i.e., not all of the elements are used?
Are you able to break the algorithm into multiple threads working on
2D to work out?
Do you have a computer that really have > 1GB of physical memory? Wow...
otherwise the swap disk takes over and you might as well implement
the algorithm different way.
"Jens Friese" <j.friese highfisch.de> wrote in message
news:3E8404A3.2000108 highfisch.de...
Hi everybody,
I hardly dare to say that I have no idea
what pointers are. I just have to create
this three-dimensional array during runtime
because it uses up to 800 mb (!) of memory.
Don't tell me that's too much - I do really need it ;)
Someone told me, to allocate memory this way -
and it works fine for me...
Can you tell me an equally easy method to do so
in a better way? I would be happy if you could
tell me about this destructor thing too.
↑ ↓ ← → Richard Grant <fractal clark.net> writes:
In article <3E8318B4.20307 highfisch.de>, Jens Friese says...
Hi,
I cannot figure out how to delete this array:
float ***xbuffer,***ybuffer,***zbuffer;
This probably won't help, but I had a lot of fun doing it so.. and sorry about
the long post. A good portion of it is conditionally compiled.
Richard
#if 0
xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
xbuffer[i] = new float*[hsize[i]];
ybuffer[i] = new float*[hsize[i]];
zbuffer[i] = new float*[hsize[i]];
for (j=0;j<hsize[i];j++) {
xbuffer[i][j] = new float[vsize[i]];
ybuffer[i][j] = new float[vsize[i]];
zbuffer[i][j] = new float[vsize[i]];
}
}
#endif
//#define OUTPUTTEST
// above macro enables an output test so that this lib can be verified.
// use carefully with CAPACITYTEST or much results displayed..
//#define TESTING
// above macro enables a non template version of the lib to be tested
#define CAPACITYTEST
// above macro allows speed and exception testing
// use carefully with OUTPUTTEST or much results displayed..
#include <deque>
#include <algorithm>
#if defined (OUTPUTTEST) || defined (CAPACITYTEST)
# include <iostream>
#endif
namespace detail {
class Buffer {
public:
Buffer(std::size_t size) throw (std::bad_alloc) :
bound(size), array(0)
{
// gen bad_alloc on fail
array = new unsigned char[bound];
if (array == 0)
throw std::bad_alloc();
}
~Buffer() {
delete [] array;
}
std::size_t size() const { return bound; }
unsigned char* data() { return array; }
private:
Buffer(const Buffer& b);
Buffer& operator=(const Buffer& b);
unsigned char* array;
std::size_t bound;
};
struct freebuffer : std::unary_function<void,Buffer*> {
void operator()(Buffer* b) {
delete b;
}
};
struct findbuffer : std::unary_function<bool,Buffer*> {
findbuffer(unsigned char* tofind) : val(tofind) { }
bool operator()(Buffer* b) {
return (b->data() == val) ? true : false;
}
private:
unsigned char* val;
findbuffer();
};
struct capacitycounter : std::unary_function<void,Buffer*> {
capacitycounter(std::size_t* p) : val(p) { *val = 0; }
void operator()(Buffer* b) { *val += b->size(); }
private:
std::size_t* val;
};
class BufferFactory {
private:
BufferFactory(const BufferFactory& b);
BufferFactory& operator=(const BufferFactory& b);
std::deque<Buffer*> known;
public:
BufferFactory() {}
unsigned char* make(std::size_t cap) throw (std::bad_alloc) {
Buffer* b = new Buffer(cap);
known.push_front(b);
return b->data();
}
void free(unsigned char* p) {
std::deque<Buffer*>::iterator it =
std::find_if(known.begin(), known.end(), findbuffer(p));
if (it != known.end()) {
Buffer* b = *it;
delete b;
known.erase(it);
}
return;
}
std::size_t size() const {
std::size_t s;
std::for_each(known.begin(), known.end(), capacitycounter(&s));
return s;
}
~BufferFactory() {
std::for_each(known.begin(), known.end(), freebuffer());
}
};
}//detail
#if !defined (TESTING)
template <int N>
#endif
class CubixFactory
{
public:
#if defined (TESTING)
static const int N = 4;
#endif
typedef float** Cubix[N];
typedef std::size_t Plane[N];
private:
CubixFactory(const CubixFactory& c);
CubixFactory& operator=(const CubixFactory& c);
detail::BufferFactory bfact;
public:
CubixFactory() { }
Cubix* make(const Plane hplane, const Plane vplane) {
// do one honkin alloc
unsigned char* data;
std::size_t data_size = sizeof(float****) +
N * sizeof(float***);
for (int i=0; (i < N); i++)
data_size += hplane[i] * sizeof(float*) *
vplane[i] * sizeof(float);
try {
data = bfact.make(data_size);
} catch (...) {
#if defined (OUTPUTTEST)
std::cerr << data_size << " failed alloc" << std::endl;
#endif
throw;
}
// layer view onto data
Cubix* vals;
int offset = 0;
vals = (Cubix*)data;
offset += 1 * sizeof(Cubix*);
// overlay concept - since no cast to array
typedef float*** cons;
cons* cube = (cons*)data;
*cube = (cons)(data + offset); // use cons, no cast to Cubix
offset += sizeof(cons) * N;
// end of overlay concept
#if defined (OUTPUTTEST)
static float test_cnt = 0;
#endif
for (int h=0; (h < N); h++) {
(*vals)[h] = (float**)(data + offset);
offset += hplane[h] * sizeof(float*);
for (int i=0; (i < hplane[h]); i++) {
(*vals)[h][i] = (float*)(data + offset);
offset += vplane[h] * sizeof(float);
#if defined (OUTPUTTEST)
for (int j=0; (j < vplane[h]); j++) {
(*vals)[h][i][j] = test_cnt++;
}
#endif
}
}
return vals;
}
void dump(Cubix* vals) {
unsigned char* data = (unsigned char*)vals;
bfact.free(data);
}
std::size_t allocated() const { return bfact.size(); }
~CubixFactory() { }
};
#if defined(OUTPUTTEST) || defined (CAPACITYTEST)
const int Bound = 4;
#if defined (TESTING)
typedef CubixFactory Factory;
#else
typedef CubixFactory<Bound> Factory;
#endif
typedef Factory::Plane Plane;
typedef Factory::Cubix Cubix;
void output(const Plane hplane, const Plane vplane,
Cubix* c, const char* s)
{
#if defined (OUTPUTTEST)
std::cout << '\n' << s << '\n';
for (int h=0; (h < Bound); h++) {
for (int i=0; (i < hplane[h]); i++) {
for (int j=0; (j < vplane[h]); j++) {
std::cout << (*c)[h][i][j] << '\t';
}
std::cout << '\n';
}
}
std::cout << '\n';
#endif
}
int main() {
#if defined (CAPACITYTEST)
Plane h = { 1024, 16, 16, 16 };
Plane v = { 768, 16, 16, 16 };
Plane hh = { 10240, 160, 160, 160 };
Plane vv = { 7680, 160, 160, 160 };
try {
Factory f;
Cubix* xbuffer = f.make(hh, vv);
} catch (std::bad_alloc& e) {
std::cerr << "\npassed bad alloc test\n" << std::endl;
}
#else
Plane h = { 2, 3, 8, 4 };
Plane v = { 6, 2, 3, 2 };
#endif
try {
Factory f;
Cubix* xbuffer = f.make(h, v);
Cubix* ybuffer = f.make(h, v);
output(h, v, ybuffer, "ybuffer");
Cubix* zbuffer = f.make(h, v);
std::cout << "factory memory use: " << f.allocated() <<
std::endl;
f.dump(ybuffer);
std::cout << "factory memory use after dump ybuffer: " <<
f.allocated() << std::endl;
output(h, v, zbuffer, "zbuffer");
f.dump(zbuffer);
std::cout << "factory memory use after dump zbuffer: " <<
f.allocated() << std::endl;
output(h, v, xbuffer, "xbuffer");
f.dump(xbuffer);
std::cout << "factory memory use after dump xbuffer: " <<
f.allocated() << std::endl;
xbuffer = f.make(h, v);
ybuffer = f.make(h, v);
zbuffer = f.make(h, v);
std::cout << "factory memory use after realloc: " << f.allocated() <<
std::endl;
output(h, v, xbuffer, "xbuffer");
output(h, v, ybuffer, "ybuffer");
output(h, v, zbuffer, "zbuffer");
} catch (...) {
std::cerr << "in main and got unexpected exception" << std::endl;
throw;
}
// destruction of factory object deletes all "made" objects
std::cout << "\npassed tests" << std::endl;
}
#endif
|
|