Realloc()ing data of a node from a linked list 
Author Message
 Realloc()ing data of a node from a linked list

I'm writing a function that inserts a character in the data of a node like:
struct node {
     struct node *next, *previous;
     unsigned char * data;
Quote:
}

The function I wrote is (dl_strlen is just strlen(pointers.current->data):
void dl_insert_char ( unsigned char newdata, int n, BOOLEAN insert ) {
    int i, size, length = dl_strlen();
    if ( insert == TRUE ) {
      if ( n >= length )         size = n + 1;
      else                       size = length + 1;
      if ( realloc ( pointers.current->data, size ) == NULL ) {
        alert("Could not allocate memory at dl_insert_char");
        return;
      }
      else {
        if ( n < length ) {
          for ( i = length; i > n; i-- )
            pointers.current->data[i] = pointers.current->data[i-1];
        }
        else {
          for ( i = length; i < n; i++ )
            pointers.current->data[i] = 32;
        }
      }
    }
    else {
      if ( n >= length ) {
        if ( realloc ( pointers.current->data, n + 1 ) == NULL ) {
          alert("Could not allocate memory at dl_insert_char");
          return;
        }
        for ( i = length+1; i < n; i++ )
          pointers.current->data[i] = 32;
      }
    }
    pointers.current->data[n] = newdata;
Quote:
}

Where pointers.current is a pointer to the node I'm using. This function works
about 3/5 of the time, but sometimes it just goes mad after the last line, and if
string that was "Stri", the new character being 'n', the result is
"Stri\030t\120\093"; garbage. What's wrong?

"There's never enough time to do all the nothing you want" Bill Watterson

My other OS is Linux -=- http://www.*-*-*.com/ -=- Electric Eng, Poli



Sun, 21 Jan 2001 03:00:00 GMT  
 Realloc()ing data of a node from a linked list

Quote:

>      if ( realloc ( pointers.current->data, size ) == NULL ) {

realloc may *move* the data it reallocates to a new location if there isn't enough
space where it is. so you need something like

    unsigned char *newpointer = realloc ( pointers.current->data, size);
    if( ! newpointer)
    {
        alert(...);
    }
    pointers.current->data = newpointer;

In general, you shouldn't just use the simpler

    pointers.current->data = realloc ( pointers.current->data, size);

because if realloc returns NULL then you have effectively leaked the original
block of memory. If you're just going to exit() if you run out of memory then
this may not be a problem.

--
Edwin



Sun, 21 Jan 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. nodes linked listed and such

2. 2 nodes end up in a cycle in a single linked list

3. newbie question: removal of node in linked list

4. How to find a node in link list

5. why malloc for a new node in linked list

6. Destroying All Nodes in a Linked List

7. Destroying All Nodes is a Linked List

8. Removing node from linked list

9. Removing a NODE for good (double linked lists)

10. deleting a previous node in a singly linked list

11. Question about swapping nodes in a doubly linked list

12. Deleting a node in a linked list

 

 
Powered by phpBB® Forum Software