quick c question

Discussion in 'Mac Programming' started by andrewface, Apr 13, 2007.

  1. andrewface macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #1
    what does this statement do?

    A = malloc(10*sizeof(long));

    also another question,

    if malloc() fails to allocate memory then it returns NULL right?
     
  2. bbarnhart macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #2
    sizeof returns the size of the type. In the case of long, it is probably 4 for 4 bytes. So A would be 40.

    A failure would cause malloc to return a null pointer, ie 0.
     
  3. andrewface thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #3
    and A would be of size 40 in memory right?

    hmm
    ive got this question on an assignment....

    so..

    if malloc fails to allocate memory it returns

    a) -1
    b) NULL
    c) EOF
    d) '\o'

    i thought NULL...what would the answer be?
     
  4. bbarnhart macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #4
    I hate to sound nasty, but I think if you looked for the correct answer to your quiz it would be faster than asking people here. And, you might learn something.

    And, I already answered that question in the previous post.
     
  5. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #5
    If you have a Mac with the developer kit installed, just open a terminal session and do a "man malloc"

    It will give you the answer.
     
  6. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #6
    To answer this part... Your statement doesn't quite make sense. malloc returns a pointer, i.e., some 32-bit or 64-bit value that is the address of an area of memory. So the type of the variable named "A" is pointer to something. Therefore A itself happens to be 32 bits or 64 bits in size and its value is a 32-bit or 64-bit number that is the address of an area of memory bit enough to hold 10 values of type long.
     

Share This Page