|
[Sponsors] |
January 13, 2004, 14:32 |
calloc( ) or malloc( )
|
#1 |
Guest
Posts: n/a
|
Hello All,
I have written a 2-d finite element code in C. My OS is RedHat 9.0 and compiler is GNU C compiler. Currently I am using calloc( ) to allocate memory for Node and Element data-structures and for the global stiffness matrix. Everything is working fine. However I have some questions and observations that I would like you all to comment/answer. My knowledge of pointers is very limited. 1. What should I use to assign memory for data structures and global stiffness matrix, calloc( ) or malloc( ) ? any explanations ?? 2. I saw website on FEM programming (don't remember which one) that recommended to use malloc( ), the reason was that calloc( ) may return a NON NULL pointer even when memory may not be actually available, while malloc( ) is guranteed to return NULL Pointer if memory is not available. 3. I observed atleast on two occassions (when my matrix size was huge) where calloc( )did return a NON NULL Pointer and further execution of program resulted in a system crash. I experimented with malloc and it it returned a NULL pointer in same situation. Any comments ?? Explanations ?? Thanks, Abhijit Tilak. |
|
January 14, 2004, 05:42 |
Re: calloc( ) or malloc( )
|
#2 |
Guest
Posts: n/a
|
I would always use malloc, because it doesn't do unnecessary work. w.r.t your calloc() failure, the ANSI/ISO 9899-1990 standard stipulates that BOTH malloc() and calloc() should return:
"either a null pointer or pointer to the allocated space." If your compiler doesn't follow the standard, complain to the compiler provider. |
|
January 14, 2004, 06:56 |
Re: calloc( ) or malloc( )
|
#3 |
Guest
Posts: n/a
|
The <tt>malloc</tt> manpage says:
Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer. |
|
January 14, 2004, 07:34 |
Re: calloc( ) or malloc( )
|
#4 |
Guest
Posts: n/a
|
Wow! A(nother) good reason to not use gcc!
The one that really bugs me is gcc's really stupid warning: "the use of `tmpnam' is dangerous, better use `mkstemp'" I can't find mkstemp in my ANSI C standard anywhere so I'm hardly likely to deliberately make my code non-portable because of gcc's advice. |
|
January 15, 2004, 11:37 |
Re: calloc( ) or malloc( )
|
#5 |
Guest
Posts: n/a
|
Hello guys,
thanks a lot for your answers and comments. Best Regards, Abhijit Tilak. |
|
January 19, 2004, 21:53 |
Re: calloc( ) or malloc( )
|
#6 |
Guest
Posts: n/a
|
You might consider using Intel's C compiler - there's a free (unsupported) version that you can download for Linux. Check the Intel homepage for it - it's a little difficult to find, but it's there.
We use the Intel 7.1 C and Fortran compilers on several systems in my lab running Redhat 9. It typically generates the fastest executables for modern PCs (e.g., Pentium 4, AMD Athlon XP/64) versus other compilers (like MS Visual C++ 6/.net on Windows...). |
|
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
fluent | nithin20k | Main CFD Forum | 1 | January 12, 2010 20:37 |
[Commercial meshers] ST_Malloc: out of memory.malloc_storage: unable to malloc Velocity SA, | cfdproject | OpenFOAM Meshing & Mesh Conversion | 0 | April 14, 2009 16:45 |
No. of Elements issue | kartik | FLUENT | 1 | February 5, 2009 14:25 |