LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2009, 11:46 AM   #1
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
Problem with free


I am trying to learn c at the moment and I am having a problem freeing a block after I have allocated and used it. I have checked the block is valid using gdb before it hits the crash with segv using gdb and it looks to be intact so I can't understand why I can't free the block.

The code which is causing me trouble is

Code:
char **rules = splitString(&rule_size,  rule_string, ",");
if (!rules){ return NULL; }
for (forloop=0; forloop < rule_size; forloop++){
             rule = strip(rules[forloop]);
            //Do some rule checking.
             free(rule);        
         }  
 free(rules);
Can anyone tell me what is going wrong?
 
Old 06-02-2009, 12:38 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Looks OK to me.

What is the problem actually? Does it give you an error?

Or is it that you don't see memroy usage dropping in tools like 'top'? In the latter case there probably isn't any problem. The kernel will have registered that you free'd the block of memory, and may decide to keep the memory block as a part of your process to re-use it later when your program asks for another block.
 
Old 06-02-2009, 12:49 PM   #3
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 34
Hello.

Perhaps providing the definitions of functions such as splitString and strip might help elucidate things.
 
Old 06-03-2009, 05:29 AM   #4
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Original Poster
Rep: Reputation: 32
The string functions I am using are as follows:

Code:
//Strip all leading and trailing whitespace from a string.

char* strip(char string[]){
      int start = 0;
      int charsize = strlen(string);
      int loop;
      int stop = charsize;
      
      for (loop = 0; loop < charsize; loop++){
          if (string[loop] != ' ' && string[loop] != '\t'){
              break;
              }
           start++;
      }
      for (loop = charsize-1; loop > 0; loop--){
          if (string[loop] != ' ' && string[loop] != '\t'){
             break;
           }
           stop--;
      }
      
      string = slice(string, start, stop, 1);
      return string;
}
       
//Get a slice of a string 

char* slice(char string[], int start, int stop, int step){
    
      //Initalise the basic paramaters.
      
       int size = strlen(string); 
       int chptr = 0;
       int loop, charsize;
       char *rtn = NULL;
              
       //normalise the paramaters.
       
       if ( start < 0 ){ start = (size + 1) + start; }
       if ( stop < 0 ) { stop = (size + 1) + stop; }
       if ( start > stop){
          int temp = start;
          start = stop;
          stop = temp;
       }
       step = abs(step);
       
       //Prepare the copy.
       
       charsize = (stop - start)/ step;
       
       rtn = (char *)malloc(charsize+2 * sizeof(char));
       if (rtn == NULL){
           return NULL;
       }    
       
       //copy each required character.
       
       for (loop = start;loop < stop; loop = loop + step){
          rtn[chptr] = string[loop];
          chptr++;
          }
      rtn[charsize] =  '\0';    
      return rtn;
}

// return an array of string split by the given delimiter

char** splitString(int *size, char data[], char delimiter[]){
       mlog("INFO", "splitString called.");
       char **results;
       char *token = NULL;
       
       *size = 0;
       results = (char **) malloc(sizeof(data) + 3);
       if (results == NULL){
           mlog("FATAL", "malloc failed in splitString.");
           return NULL;
       }
       
       token = strtok(data, delimiter);
       while (token != NULL){
             mlog("INFO", "Got a token in splitString.");
             results[(int) *size] = token;
             *size =  *size + 1;
             token = strtok(NULL, delimiter);
             }  
       return results;
}
The program crashes with free(): invalid next size (fast)
 
Old 06-03-2009, 06:09 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
There is an obvious error that fits the symptoms. But I didn't check carefully enough to see if it is the only such error nor even if it is the error that would cause the symptoms first.

Quote:
Originally Posted by pgpython View Post
results = (char **) malloc(sizeof(data) + 3);
sizeof(data) does not do what you expect. Passing char data[] does not pass the size of the array, just its address.
Even if you had the size of the char array, I don't think the allocation would be correct. You would then be depending on the average split length being greater than the number of bytes in a pointer.

The actual error generally means you used more of a memory allocation than you allocated, overwriting the information right after that allocation. Clearly you do so with this results allocation. I don't know if you also do so with other allocations.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding this line "Free taken as in free speech, not free beer" SHENGTON Linux - Newbie 11 01-12-2009 07:40 AM
Free NX Server Problem swamprat Linux - Software 1 11-29-2008 09:02 PM
LXer: Free Me: a DVD about free culture and free software LXer Syndicated Linux News 0 03-12-2007 12:01 PM
Free, free,FREE=? no money, = Freedom? murshed Linux - Newbie 8 01-20-2003 07:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration