Pointer Problems 
Author Message
 Pointer Problems

I'm writing a simple prog that re-uses a string array and am running
into errors when I try to free the memory allocated.

Here is some of the initialization code. This allocates an array of 500+

strings and assigns the proper value.

  field_count = LCTextListGetCount(&(Context->field_list));
  /* Cache names of fields for future compares */
  if(field_count)
  {
     state->resultnames =(void **) malloc
((field_count+1)*sizeof(char*));
     if(state->resultnames==NULL)
     {
       log("Insufficient memory. Call failed\n");
       return(LCFAIL_EXTERNAL);
     }
     for(count=0;count<field_count;count++)
     {
        status = LCTextListFetch(&(Context->field_list), count+1,
                                LCSTREAMFMT_NATIVE, CON_STREAM,
                               (void **)&name, &length);
        if(length>0)
       {
         state->resultnames[count] = strdup(name);
         if(state->resultnames[count]==NULL)
         {
               log("Insufficient memory. Call failed\n");
               return(LCFAIL_EXTERNAL);
         }
         keys++;
       }
    }
    state->resultnamecount = keys;
  }

The problem I'm having is that the program crashes hard when I try to
free the array:

    for(i=0; i < state->resultnamecount; i++)
    {
      if(state->resultnames[i])
      {
        free(state->resultnames[i]);
        state->resultnames[i]=NULL;
      }
    }
    free(state->resultnames);
    state->resultnames=NULL;
    state->resultnamecount = 0;

This will loop anywhere between 200 - 300 times freeing the strings and
then will crash, no explanation given. If I check then resultnamecount
variable at the beginning of the loop it will be set to its proper
value(509).
What I would like is a way of preventing the crash from happening and/or

a possible cause for the free failure. I though that testing if the
string was null was a good way of checking if the pointer was freeable
or not. Am I wrong?

Sorry for the newbie question and TIA.
--



Sat, 10 Nov 2001 03:00:00 GMT  
 Pointer Problems
Hi,

I think the problem may be shown below, but I could be wrong.  I'd be
grateful if someone would point out where I am wrong, if I am :)


Quote:
> I'm writing a simple prog that re-uses a string array and am running
> into errors when I try to free the memory allocated.

> Here is some of the initialization code. This allocates an array of 500+

> strings and assigns the proper value.

>   field_count = LCTextListGetCount(&(Context->field_list));
>   /* Cache names of fields for future compares */
>   if(field_count)
>   {
>      state->resultnames =(void **) malloc
> ((field_count+1)*sizeof(char*));
>      if(state->resultnames==NULL)
>      {
>        log("Insufficient memory. Call failed\n");
>        return(LCFAIL_EXTERNAL);
>      }

*** In this for loop, you increment count each time ***

Quote:
>      for(count=0;count<field_count;count++)
>      {
>         status = LCTextListFetch(&(Context->field_list), count+1,
>                                 LCSTREAMFMT_NATIVE, CON_STREAM,
>                                (void **)&name, &length);
>         if(length>0)
>        {

*** here, if length > 0, you set state->resultnames[count] ***

Quote:
>          state->resultnames[count] = strdup(name);
>          if(state->resultnames[count]==NULL)
>          {
>                log("Insufficient memory. Call failed\n");
>                return(LCFAIL_EXTERNAL);
>          }
>          keys++;
>        }

*** if length <= 0, you do nothing with state->resultnames[count]
    and so it is left pointing somewhere ... who Knows...  Anyway,
    when you come to free your structure, you try to free every
    one of state->resultname[i].  In order to correct the problem
    I would suggest adding

        else
            state->resultname[count] = NULL;

- Show quoted text -

Quote:
>     }
>     state->resultnamecount = keys;
>   }

> The problem I'm having is that the program crashes hard when I try to
> free the array:

>     for(i=0; i < state->resultnamecount; i++)
>     {
>       if(state->resultnames[i])
>       {
>         free(state->resultnames[i]);
>         state->resultnames[i]=NULL;
>       }
>     }
>     free(state->resultnames);
>     state->resultnames=NULL;
>     state->resultnamecount = 0;

> This will loop anywhere between 200 - 300 times freeing the strings and
> then will crash, no explanation given. If I check then resultnamecount
> variable at the beginning of the loop it will be set to its proper
> value(509).
> What I would like is a way of preventing the crash from happening and/or

> a possible cause for the free failure. I though that testing if the
> string was null was a good way of checking if the pointer was freeable
> or not. Am I wrong?

You are only not wrong (i.e. only right :) if you can guarantee that invalid
pointers that must not be freed will be set to NULL (or 0, or some other
special value that you choose).

Hope that helps.

Quote:

> Sorry for the newbie question and TIA.
> --


--



Sun, 11 Nov 2001 03:00:00 GMT  
 Pointer Problems

Quote:

> I'm writing a simple prog that re-uses a string array and am running
> into errors when I try to free the memory allocated.

> Here is some of the initialization code. This allocates an array of 500+

> strings and assigns the proper value.

Debugging problems with memory access can be difficult. The main reason
is that
the error often occurs somewhere after the faulty code. If you set the
variable

MALLOC_CHECK_=1; export MALLOC_CHECK_; program

This will cause a debuggiug version of malloc to be used. (This assumes
you are using
gcc with the current version of the C library). Error messages will
printed to stderr.

MALLOC_CHECK_=2; export MALLOC_CHECK_; program

This will cause the program to abort() as soon as a problem is
encountered. Hopefully
this will give an indication as to where the problem is. See 'man 3
free' for more info.

Norman Tackett
http://home.att.net/~sltackett
--



Fri, 23 Nov 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. pointer to pointer problem, please advice

2. Interesting Pointer-to-pointer problem

3. Pointer to pointer problem.

4. pointer->pointer problem

5. pointer to pointer problem

6. dyn mem alloc - pointer to pointer problem

7. C++ function pointers versus C function pointer problem

8. pointer to pointer problem

9. MicroSoft C _based pointers problem (chicken and egg problem)

10. pointer problem

11. char ** pointer problem

12. Plez help with this pointer problem...

 

 
Powered by phpBB® Forum Software