regarding dynamic memory allocation

Reply

Join Date: Dec 2005
Posts: 8
Reputation: srishekh is an unknown quantity at this point 
Solved Threads: 0
srishekh srishekh is offline Offline
Newbie Poster

regarding dynamic memory allocation

 
0
  #1
Mar 25th, 2006
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
Attached Files
File Type: cpp f1.cpp (866 Bytes, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: regarding dynamic memory allocation

 
0
  #2
Mar 25th, 2006
>>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.
  1. int *array;
  2. try
  3. {
  4. array = new int[100];
  5. }
  6. catch(...)
  7. {
  8. cout << "out of memory" << endl;
  9. return 1;
  10. }


[edit] Just found this one too that doesn't throw an exception
  1. array = new (std::nothrow) int[0x3fffffff];
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: regarding dynamic memory allocation

 
0
  #3
Mar 25th, 2006
> 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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 8
Reputation: srishekh is an unknown quantity at this point 
Solved Threads: 0
srishekh srishekh is offline Offline
Newbie Poster

Re: regarding dynamic memory allocation

 
0
  #4
Mar 25th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: regarding dynamic memory allocation

 
0
  #5
Mar 25th, 2006
Link with -lstdc++ (or -lstdcxx under DOS), or use g++ (or gpp under DOS):
  1. $ g++ template.cpp -o template
  2.  
  3. $ gcc template.cpp -o template -lstdc++
  4.  
  5. C>gpp template.cpp -o template.exe
  6.  
  7. C>gcc template.cpp -o template.exe -lstdcxx
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: regarding dynamic memory allocation

 
0
  #6
Mar 25th, 2006
Declare an array that will be dynamically allocated as
  1. double **2darray, ***3darray;
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: regarding dynamic memory allocation

 
0
  #7
Mar 25th, 2006
If you're using scanf() and printf(), you can use the C functions malloc() and free() instead of C++'s new and delete.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: regarding dynamic memory allocation

 
0
  #8
Mar 25th, 2006
Originally Posted by srishekh
my gcc v3.3 doesnt support exception.
Then it isn't a c++ compiler so it will not support new and delete either. :eek:
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 8
Reputation: srishekh is an unknown quantity at this point 
Solved Threads: 0
srishekh srishekh is offline Offline
Newbie Poster

Re: regarding dynamic memory allocation

 
0
  #9
Mar 26th, 2006
Originally Posted by srishekh
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: regarding dynamic memory allocation

 
0
  #10
Mar 26th, 2006
did you attempt to use try/catch blocks as I posted earlier? compilers are not required to return NULL.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC