Code Comments
Programming Forum and web based access to our favorite programming groups.Hi All
I am creating a huge 3D array of size 700 by 200 by 200 having
instances of one of my class. I use the following code to create it.
voxelCube_ = new Voxel**[xBound_];
for( int m=0; m<xBound_; m++)
{
voxelCube_[m] = new Voxel*[yBound_];
for (int l=0; l<yBound_; l++)
{
voxelCube_[m][l]= new Voxel[zBound_];
}
}
However after certain limit the program assigns value NULL to the
pointer. I am not sure why this is happening. Please let me know if
anybody has any idea about this phenomenon.
Regards
Nikhil
Post Follow-up to this messagenvjoglekar wrote:
> Hi All
>
> I am creating a huge 3D array of size 700 by 200 by 200 having
> instances of one of my class. I use the following code to create it.
>
> voxelCube_ = new Voxel**[xBound_];
>
> for( int m=0; m<xBound_; m++)
> {
> voxelCube_[m] = new Voxel*[yBound_];
> for (int l=0; l<yBound_; l++)
> {
> voxelCube_[m][l]= new Voxel[zBound_];
> }
> }
>
> However after certain limit the program assigns value NULL to the
> pointer. I am not sure why this is happening. Please let me know if
> anybody has any idea about this phenomenon.
Sounds like your C++ implementation is broken. 'new' may not return a
null pointer. Some implementations incorrectly return a null pointer
when the request cannot be fulfilled.
Also, If you need a massive 3-dimensional array then your algorithm is
probably broken.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Post Follow-up to this message* nvjoglekar@rediffmail.com (nvjoglekar) schriebt:
>
> I am creating a huge 3D array of size 700 by 200 by 200 having
> instances of one of my class. I use the following code to create it.
>
> voxelCube_ = new Voxel**[xBound_];
>
> for( int m=0; m<xBound_; m++)
> {
> voxelCube_[m] = new Voxel*[yBound_];
> for (int l=0; l<yBound_; l++)
> {
> voxelCube_[m][l]= new Voxel[zBound_];
> }
> }
When you need such a "huge" thing (what is huge and what is small is
often very system-dependent) the size is often known in advance. So
you can then use a single stack-based variable or dynamic allocation
of _one_ large variable.
> However after certain limit the program assigns value NULL to the
> pointer. I am not sure why this is happening. Please let me know if
> anybody has any idea about this phenomenon.
Pre-standard behavior, e.g. in Visual C++ 6.0. Simply do
#include <stdexcept>
..
template<typename T>
inline T* notNull( T* p ){ if( !p ){ throw std::bad_alloc(); } ret
urn p; }
..
voxelCube_ = ::notNull( new Voxel**[xBound_] );
for( int m=0; m<xBound_; m++)
{
voxelCube_[m] = ::notNull( new Voxel*[yBound_] );
for (int l=0; l<yBound_; l++)
{
voxelCube_[m][l]= ::notNull( new Voxel[zBound_] );
}
}
And do _not_ listen to anyone who wants you to install a "new-handler"
which changes the behavior of operator new, because that might break a lot
of libraries that you're implicitly using, including the run-time lib.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.