1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

malloc allocation

Discussion in 'C' started by answerme, Sep 20, 2011.

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    hi
    when I do

    ptr=(int*)malloc(10);
    Does the memory allocate of 10 bytes are in continuous memory area .
    Does malloc allocates memory conitinous
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes to both, although this is not good use of malloc. It allocates 10 bytes, but you should allocate N*sizeof(int) instead, where N is how many ints you want. If ints are 4 bytes on your system, this will allocate two and a half of them, of which only the first two are useful.
     
  3. raixyz

    raixyz Banned

    Joined:
    Oct 18, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    That is to say it returns a pointer of type void * that is the start in memory of the reserved portion of size number_of_bytes. If memory cannot be allocated a NULL pointer is returned.
     

Share This Page