#1
  1. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13

    another malloc question


    if I do this

    char * p = (char*) malloc(sizeof(char) * 30);

    p[20] = '\0';

    free(p);


    How much memory did I free? was it the whole 30 bytes that I putted asside with the first statement, or just 21bytes caused by putting a binary 0 at offset 20?

    and if it is only 21 bytes, did i just created a memory leak?
    Some day I'll create a smart quote to put here.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    You freed 30 bytes.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    21
    Rep Power
    0
    yes it frees 30 bytes of memory
    '\0' is not a specifier for end memory location
    it is only a value stored at memory location
    it is use a specifier in C for end of string like $ symbol use in Assembly
  6. #4
  7. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    thanks guys, specialy for the clarifing explanation.

IMN logo majestic logo threadwatch logo seochat tools logo