Thread: Pointers and malloc

  1. #1
    Unregistered
    Guest

    Pointers and malloc

    Hi, I just have one quick question.
    I am doing something like this:

    char *buf = malloc(10);
    int file_descriptor = ... // just some file descriptor to read from

    read(file_descriptor, buf, 9);

    buf += 5;

    ...

    free(buf);

    Now the question is, when I call free(buf), does it free up all 10 bytes of memory that were allocated? Or only the 5 bytes following the current address that buf is pointing to?

    Thanks.

  2. #2
    Unregistered
    Guest
    I'm pretty sure that it will free 10 bytes but will free them starting at the point 5 from where you allocated them which could be very bad. But someone should probly confirm that

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    It will free any memory allocated with it, so all 10 bytes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers + Malloc = Painloc
    By Chalks in forum C Programming
    Replies: 9
    Last Post: 10-19-2008, 01:10 PM
  2. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  3. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  4. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 04:20 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM