Thread: malloc()

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    malloc()

    Referring to - http://www.mkssoftware.com/docs/man3/malloc.3.asp

    I would be obliged to see explanation for this

    "Unlike many UNIX platforms, the heap that is used by malloc(), calloc(), and realloc() is not guaranteed to be one contiguous piece of memory. ok!

    Thus it is invalid to assume that all memory between two pointers returned by these functions is accessible, -- didnt get it

    and it is invalid to compare pointers returned by these functions to determine the total size of the heap." -- how can i compare pointers to determine size of heap?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I don't get the last either, but the second one refers to a common trick in C. If the memory is contiguous, you can do pointer arithmetic and simply increment the pointer, which will add 1 * sizeof(whatever type) to the pointer address. However, if memory is not contiguous, you cannot assume that pointer + 1 * sizeof(whatever type) will get you to the next element of your pointer.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by Desolation View Post
    I don't get the last either, but the second one refers to a common trick in C. If the memory is contiguous, you can do pointer arithmetic and simply increment the pointer, which will add 1 * sizeof(whatever type) to the pointer address. However, if memory is not contiguous, you cannot assume that pointer + 1 * sizeof(whatever type) will get you to the next element of your pointer.

    ah, ok got the second one now, how abt third ?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What it says is that if you do this
    Code:
    a = malloc(size1);
    b = malloc(size2);
    there's no guarantee that the memory blocks a and b point to are right next to each other. If malloc didn't fail, you can be quite sure that a points to size1 of contiguous memory and you can use pointer arithmetics on this range as much as you like.

    Concerning 3, why would anyone think that this can be used to determine the size of the heap? If I'm not mistaken comparing pointers is fishy unless they point to the same object (e.g pointers to elements in the same array).

    There are probably some OS specific functions to find about memory sizes etc. After all memory is rather virtual and the OS just takes care that your addresses are right.

    (Just tried one thing: a program allocates an int and prints its address. Multiple instances of the program display the same value at the same time!)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by anon View Post
    What it says is that if you do this
    [code]
    If malloc didn't fail, you can be quite sure that a points to size1 of contiguous memory and you can use pointer arithmetics on this range as much as you like.
    malloc does not return pointer to contiguous allocated memory!

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by vb.bajpai View Post
    malloc does not return pointer to contiguous allocated memory!
    Yes, it does. A single malloc() call will give you a contiguous chunk of memory. You're misreading what the documentation says. It's saying the entire heap isn't guaranteed to be a contiguous chunk of memory. That doesn't mean that individual calls to malloc() won't give you a contiguous chunk of memory.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vb.bajpai View Post
    malloc does not return pointer to contiguous allocated memory!
    I think you're misunderstanding. If you ask malloc() for X bytes, you get a pointer to a CONTIGUOUS region of memory of at least X bytes in length. Obviously the chunks you ask for will be allocated contiguously (otherwise how would you know which regions you could access?)

    The problem is when you do something like this:

    Code:
    int *a = malloc(100 * sizeof(int));
    int *b = malloc(100 * sizeof(int));
    And then assume that ALL memory between a and b can be accessed. If a and b are directly adjacent to each other (in other words, if a + 100 * sizeof(int) == b, or vice versa), then there is no question as there is no "dead space." But if that were not the case, you still could not assume that the intervening memory is legally addressable. THAT's what they mean by "the heap is not guaranteed to be contiguous." Make sense?

    This isn't even generally true on UNIX systems either. Yes, normally malloc() carves chunks out of the data segment using sbrk(), but it is not required to -- it could get the memory some other way, like creating an anonymous memory map (in fact, the GNU C library does this for very large blocks).

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    ok!, got the 2nd point, plz clarify on the 3rd point please

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you cannot do
    a-b if these two pointers point to the reageons allocated by different mallocs
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM