Malloc and calloc problem!!

This is a discussion on Malloc and calloc problem!! within the C Programming forums, part of the General Programming Boards category; Everyone ought to put that link in their signature. That and the one about gets()....

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21