Thread: Malloc and calloc problem!!

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Everyone ought to put that link in their signature. That and the one about gets().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    Like i said doesent matter. With a good compilier like gcc its really a mute point. But i do understand why its not needed.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In addiiton to the FAQ, you never need to typecast a void pointer when assigning one. Ever. There's no reason to do it, because the assignment is automatically promoted to whatever type you're assigning them to. Anything else is just a waste of keystrokes.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sizeof(char) is one, so there is no point in multiplying by it.
    If you write
    p = malloc ( n * sizeof *p );
    then this always works and you never have to worry about 'remembering' to multiply by your object type.

    The problem with optional things (like braces in single line if statements for example) is that sooner or later, it's not what you want and you don't even realise it until much later when you get that big "DOH" moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Of course, the OP seems to have gone away and taken their mysterious program with them, so I guess we'll never know.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  3. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  4. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM