| | |
regarding dynamic memory allocation
![]() |
•
•
Join Date: Dec 2005
Posts: 8
Reputation:
Solved Threads: 0
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
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.
[edit] Just found this one too that doesn't throw an exception
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.
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
array = new (std::nothrow) int[0x3fffffff];
Link with -lstdc++ (or -lstdcxx under DOS), or use g++ (or gpp under DOS):
C Syntax (Toggle Plain Text)
$ 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
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
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
Declare an array that will be dynamically allocated as
C Syntax (Toggle Plain Text)
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
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
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
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
•
•
Join Date: Dec 2005
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
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
![]() |
Similar Threads
- dynamic memory allocation (C++)
- Problem with memory allocation =( (C)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- memory allocation ptr to array? how? (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C Forum
- Previous Thread: Need help Enlarging a Matrix
- Next Thread: Graph theory, do you have the project?
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






