943,945 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3373
  • C RSS
Mar 25th, 2006
0

regarding dynamic memory allocation

Expand Post »
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, 21 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srishekh is offline Offline
8 posts
since Dec 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

>>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];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

> 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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srishekh is offline Offline
8 posts
since Dec 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

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
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

Declare an array that will be dynamically allocated as
  1. double **2darray, ***3darray;
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

If you're using scanf() and printf(), you can use the C functions malloc() and free() instead of C++'s new and delete.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Mar 25th, 2006
0

Re: regarding dynamic memory allocation

Quote 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:
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 26th, 2006
0

Re: regarding dynamic memory allocation

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srishekh is offline Offline
8 posts
since Dec 2005
Mar 26th, 2006
0

Re: regarding dynamic memory allocation

did you attempt to use try/catch blocks as I posted earlier? compilers are not required to return NULL.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help Enlarging a Matrix
Next Thread in C Forum Timeline: Graph theory, do you have the project?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC