Thread: malloc

  1. #1
    Registered User
    Join Date
    Jun 2006

    Smile malloc

    I know that malloc for pointers to get a place in mamory but how does it work?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    This probably would be a thread better placed on the C board. As to your question, do you wish to know how to use the malloc function, or do you wish to know how it is implememented on a given platform?

  3. #3
    Registered User
    Join Date
    Mar 2005
    int x;
    x = 5;

    the pointer equivelance:
    int *p = malloc(sizeof(int));
    *x = 5;

    if you did:
    x = 5;

    with the pointer, you would overwrite the memory returned from malloc and replace it with a 5. That is why you need to dereference it (*x) first, to assign to memory returned from malloc a value, and not replace the memory itself.

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() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM