incremented pointer and free

This is a discussion on incremented pointer and free within the C Programming forums, part of the General Programming Boards category; Hi All, I've got a problem with some code. It does something like this: - create a pointer p (char ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    incremented pointer and free

    Hi All,

    I've got a problem with some code. It does something like this:

    - create a pointer p (char *)

    - allocate a buffer pointed to by p

    - fill buffer with data

    - assign another pointer q (char *) to the buffer

    - set p to NULL

    - use pointer q, increment it to the end of the buffer

    - free q


    My question is, what happens to the buffer, and more importantly the program? The pointer has been moved to the end of the buffer before it has been free'd. This program runs on several different unix platforms and crashes on one of them - at the free.


    cheers to anyone who can help.
    Pete

    (sorry I can't list any code as it's split all over the place).

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, it's obviously wrong. You should only call free if it points at the beginning of the buffer. Why not keep p and call free on p, p still points at the beginning of the buffer.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    I figured it was wrong. This isn't my code, it's complicated stuff and does some things I don't get - with some fairly low-level system calls to fill the buffer.

    Having run it through a memory checker (valgrind) it seems to be ok (no memory leaks!). I'm trying to make sure the free is incorrect before I have to wade in to the spaghetti to fix it (it isn't a simple job to keep the pointer p).

    I was wondering if the free of such a pointer, q, was undefined behaviour (as it works on some platforms and not others and the memory checker shows no leaks - it doesn't crash on the first free btw).

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well any code which makes such a mess of it's only pointer to allocated memory is really in deep doo doo IMO

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing on free() with an invalid pointer message
    By skreaminskull in forum C Programming
    Replies: 6
    Last Post: 01-23-2009, 04:10 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21