malloc ,calloc code problem..

This is a discussion on malloc ,calloc code problem.. within the C Programming forums, part of the General Programming Boards category; the malloc need to return an address to which every cell is empty but when we print malloc1[i] (which is ...

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the malloc need to return an address to which every cell is empty
    but when we print malloc1[i] (which is the content of cell "i")
    we get the value -431602080.00000 instead of empty or some thing like that

    why is that?

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Malloc doesn't return "empty" space in the sense that the content is "zero", but it is "empty" in the sense that the piece of memory that the address refers to is guaranteed to not be used by anyone else [as long as they follow the rules, at least].

    If you want it to be zero then use calloc()[1]

    [1] However, some will immediately point out that a bit-pattern of all zeros doesn't necessarily make a floating point zero, so if you are allocating memory for use as floating point values, you should, for complete portability, use a loop that sets each value as 0.0 (double) or 0.0f (float). That way you are sure the value is a zero-floating point value, rather than "all bits are zero".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #33
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    the malloc need to return an address to which every cell is empty
    but when we print malloc1[i] (which is the content of cell "i")
    we get the value -431602080.00000 instead of empty or some thing like that

    why is that?
    malloc() does not initialize the empty storage it acquires since that's the job of calloc() ie
    Code:
    char *ptr_to_float = (float*)calloc(3, sizeof(float));  /* each of the 3 float elements will be initialized to zero */

  4. #34
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > each of the 3 float elements will be initialized to zero
    Read matsp's comment in the previous post.
    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. #35
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    And why wouldn't casting the return value of calloc() to the type float take care of that issue.

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because the issue isn't the type of the return value of calloc; the issue is that no one promises that "0x00000000" represents zero in your particular floating-point representation. (I don't know of any commonly used floating-point representation where it isn't, although in floating slash it might represent +Inf.)

  7. #37
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    As far as I know there is only the IEEE standard for representing floating point numbers and if the FPCP functionality exists alongwith the FPR's I don't see why the micro won't be able to intrepret that number as a float zero.

  8. #38
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    http://www.quadibloc.com/comp/cp0201.htm
    Not all of these had all bits zero == 0.0

    The C99 standard only refers to IEEE by example, to specify the minimum requirements for floating point support. It doesn't delve into the details of how a float is represented by the machine (in the same way that it makes no distinction about say endianess or 1 or 2's complement storage of integers).
    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 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my morse code program
    By justin87 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:23 PM
  2. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM

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