Thread: Tidying up memory

  1. #1
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16

    Tidying up memory

    I have the following method that I use to replace one node of a binary tree with another:
    Code:
    int inttree_replace(TreeNode *destination, TreeNode *node)
    {
    	//Return false if either node is invalid
    	if (!inttree_isnode(destination) || !inttree_isnode(node)) return false;
     
    	//Copy the entire node as a block of memory into the new position
    	node = (TreeNode *) memmove(destination, node, sizeof(TreeNode));
     
    	//The destination pointer is no longer valid
    	destination = NULL;
     
    	//Return true for success
    	return true;
    }
    I'm just not sure if I'm clearing up all the memory allocated earlier. As I understand it, using "memmove" moves a block of memory from A to B, so presumably I don't need to free up the original block of memory at A? I've also avoided using "free(destination)" because, after the "memmove" statement, I'd imagine that it's pointing to the new block of memory - is this right?

    Basically, could someone tell me if I'm doing anything wrong by copying memory in this way? Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    memmove doesn't physically move memory it moves only the contents.
    And yes, you have to call free on the memory pointed to by destination before you set the pointer to 0.
    Kurt

  3. #3
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    OK, thanks, but if I change it to this:
    Code:
    //Copy the entire node as a block of memory into the new position
    node = (TreeNode *) memmove(destination, node, sizeof(TreeNode));
     
    //The destination pointer is no longer valid
    free(destination);
    destination = NULL;
    Then surely the "free" statement would free the block of memory pointed to by "destination", which is now the new block of memory? Could I instead use this?:
    Code:
    //Free the destination block of memory
    free(destination);
     
    //Copy the entire node as a block of memory into the new position
    node = (TreeNode *) memmove(destination, node, sizeof(TreeNode));
     
    //The destination pointer is no longer valid
    destination = NULL;
    This frees the destination block of memory beforehand, but would this then affect the subsequent "memmove" statement? Does calling "free(destination)" keep "destination" as a valid pointer that can be used in memmove?

    Sorry about all the questions, I'm new to this

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I didn't look at you code very well.
    You are using memmove in a wrong way
    it works in the other direction
    Code:
    void *memmove( void *to, const void *from, size_t count );
    so it has to be
    Code:
    node = (TreeNode *) memmove(node, destination, sizeof(TreeNode));
    and you have to free after moving.

    Actually I am not shure in which direction you want to move from node to destination or the other way round.

    Kurt

  5. #5
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    Quote Originally Posted by ZuK
    Actually I am not shure in which direction you want to move from node to destination or the other way round.
    I'm moving data from "node" to "destination", so my original code should be correct.

    Quote Originally Posted by ZuK
    and you have to free after moving.
    What do I have to free after moving? If I free "destination" after I overwrite it with "node" then surely freeing the memory will free the newly-copied data as well?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > node = (TreeNode *) memmove(destination, node, sizeof(TreeNode));
    Since C supports structure assignment, do
    *destination = *node;

    You probably want to free(node) since you just made a copy of it. Freeing destination doesn't seem right at all.
    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.

  7. #7
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    Yikes, I didn't realise it could be done that simply. Thank you both for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. available memory from task manager
    By George2 in forum Tech Board
    Replies: 10
    Last Post: 01-18-2008, 02:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM