Respected Sir/madam,

Could you help me in understanding the dynamic memory allocation
for a 3D matrix.
I have attached the file.I am allocating memory dynamically for the
3D matrix,which i am using in my project.

As per the concept when new is unable to allocate the specified memory
it will return NULL nalue,which can be used for the normal termination of the program.But when I give 1000 for a,b,c, it simply hangs,instead of
displaying the message "unable to allocate" and exit.

I use gcc 3.3.0.

Could you please tell me how to do that.

Thanks and regards
srishekh

>>when new is unable to allocate the specified memory it will return NULL

No it doesn't. c++ standards say that new throws an exception when there isn't enough memory, although Microsoft compilers may still return NULL as well. There may be better ways to code this exception handling routine, but this is the basic concept.

int *array;
try
{
   array = new int[100];
}
catch(...)
{
   cout << "out of memory" << endl;
   return 1;
}

[edit] Just found this one too that doesn't throw an exception

array = new (std::nothrow) int[0x3fffffff];

> But when I give 1000 for a,b,c
Do the math - 1000 * 1000 * 1000 * sizeof(double)
Do you have 8GB of memory?

> if(dp==NULL)
Should be outside the first for loop, not inside.

Also, what's with all the printf/scanf inside a C++ program?

Thank you for your replies.

I tried by putting printf outside the loop,even then instead of displaying
message, it hangs.

my gcc v3.3 doesnt support exception.

Will you please give me the solution.

Link with -lstdc++ (or -lstdcxx under DOS), or use g++ (or gpp under DOS):

$ g++ template.cpp -o template

$ gcc template.cpp -o template -lstdc++

C>gpp template.cpp -o template.exe

C>gcc template.cpp -o template.exe -lstdcxx

Declare an array that will be dynamically allocated as

double **2darray, ***3darray;

If you're using scanf() and printf(), you can use the C functions malloc() and free() instead of C++'s new and delete.

my gcc v3.3 doesnt support exception.

Then it isn't a c++ compiler so it will not support new and delete either. :eek:

Thank you for your replies.

I tried by putting printf outside the loop,even then instead of displaying
message, it hangs.

my gcc v3.3 doesnt support exception.

Will you please give me the solution.

Sorry, I did mention gcc v3.3.
It is a typing mistake.
The compiler I use is : g++ 3.3

There is no problem in printf and scanf.
Problem is, why it doesnot display message when it can not allocate memory.

Any other alternative solution ,pls tell me.
srishekh

did you attempt to use try/catch blocks as I posted earlier? compilers are not required to return NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.