Thread: malloc,free pointer question.

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    malloc,free pointer question.

    Hi, I have a thought that i'd like to clear out.

    I allocate memory say;

    char* p = malloc(20);

    p = p + 5;

    //do stuff

    free(p);

    Now I know this is wrong, but the question is, does the call free ignores the request? Or does it just free 20 bytes from where ever it is pointing? And one last thing, is it valid to move the allocated pointer?

    Thanks
    Indy

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes it is wrong to do that.

    > does the call free ignores the request?
    Most likely, it will crash.

    > is it valid to move the allocated pointer?
    Sure you can, but the thing you end up freeing must be the thing you got from malloc.
    Save the original pointer, and modify a copy of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
     
    char* p = malloc(20);
    
    char *q = p;
    
    p = p + 5;
    
    //do stuff
    
    free(q);
    The above changes would be sufficient - you could do it many different ways, but if you modify the return value from malloc, you need to save it away so that you can free the same thing. As Salem said, it will most likely crash - but worse possibility is that it doesn't crash when you free, but perhaps it crashes next time you malloc, or some 20 malloc's later when the corrupted data that got stored back into the heap is needed again - those bugs can be VERY hard to find. So being careful about freeing exactly the same thing that you malloc'd is a good idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And one last thing, is it valid to move the allocated pointer?
    As long as you remember the location of the beginning of the block that you allocated, and free this, you're okay. So you could even do this!
    Code:
    char *p = malloc(5);
    size_t len = 0;
    
    strcpy(p, "Name");
    
    while(*p) putchar(*p ++), len ++;
    
    free(p - len);
    In other words, there's nothing special about the pointer that you initially assign the return value of malloc() to.

    But, of course, if you write code like that you're going to hopelessly confuse yourself and everyone around you in no time at all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Thanks lads. That clears the mind :-)

    Indy

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by freeindy View Post
    Now I know this is wrong, but the question is, does the call free ignores the request? Or does it just free 20 bytes from where ever it is pointing?
    It does neither -- the behavior is undefined. Most likely it will crash your program (either immediately or at some future point). But in theory, anything could happen. You might create a black hole.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM