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

Attachments
#include <stdio.h>
#include <stdlib.h>
#include <iostream>



int main()
 {

  
//  double dp[a][b][c];
   int  i,j,k,a,b,c;
   double  ***dp;

printf("enter the values of a,b,c\n");

 scanf("%d%d%d",&a,&b,&c);
 dp = new double **[a]; 
 for (i = 0; i < a; i++)
   {   
    if(dp==NULL)
          {
           printf("unable to allocate\n");
           exit(1);
          }
   
    dp[i] = new double *[b];
        if(dp[i]==NULL)
          {
           printf("unable to allocate\n");
           exit(1);
          }
       for (j = 0; j < b; j++)
          { 
           dp[i][j] = new double[c];
          if(dp[i][j]==NULL)
          {
           printf("unable to allocate\n");
           exit(1);
          }
          } 
   }




for (i=0; i<a; ++i)
{
for ( j=0; j<b; ++j) 
    delete [] dp[i][j];

  delete [] dp[i];
}

delete [] dp;





  return 0;
 }

>>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.

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

This article has been dead for over six months. Start a new discussion instead.