Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Memory Allocation Problem
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

Report this thread to moderator Post Follow-up to this message
Old Post
nvjoglekar
03-31-04 10:38 PM


Re: Memory Allocation Problem
nvjoglekar 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Kevin Goodsell
03-31-04 10:38 PM


Re: Memory Allocation Problem
* 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?

Report this thread to moderator Post Follow-up to this message
Old Post
Alf P. Steinbach
03-31-04 11:41 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:27 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.