CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

calloc( ) or malloc( )

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   January 13, 2004, 14:32
Default calloc( ) or malloc( )
  #1
Abhijit Tilak
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.
  Reply With Quote

Old   January 14, 2004, 05:42
Default Re: calloc( ) or malloc( )
  #2
Steve
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.
  Reply With Quote

Old   January 14, 2004, 06:56
Default Re: calloc( ) or malloc( )
  #3
Praveen
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.
  Reply With Quote

Old   January 14, 2004, 07:34
Default Re: calloc( ) or malloc( )
  #4
Steve
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.
  Reply With Quote

Old   January 15, 2004, 11:37
Default Re: calloc( ) or malloc( )
  #5
Abhijit Tilak
Guest
 
Posts: n/a
Hello guys,

thanks a lot for your answers and comments.

Best Regards,

Abhijit Tilak.
  Reply With Quote

Old   January 19, 2004, 21:53
Default Re: calloc( ) or malloc( )
  #6
Marcus Lobbia
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...).
  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
fluent nithin20k Main CFD Forum 1 January 12, 2010 20:37
Need help with the error cfdproject OpenFOAM Other Meshers: ICEM, Star, Ansys, Pointwise, GridPro, Ansa, ... 0 April 14, 2009 15:45
No. of Elements issue kartik FLUENT 1 February 5, 2009 14:25


All times are GMT -4. The time now is 17:29.