Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3361930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:12:43+00:00 2026-05-18T03:12:43+00:00

Im trying to split a string according to the following rules: words without around

  • 0

Im trying to split a string according to the following rules:

  1. words without “” around them should be treated as seperate strings
  2. anything wiht “” around it should be treated as one string

However when i run it in valgrind i get invalid frees and invalid read size errors, but if i remove the two frees i get a memory leak. If anyone could point me in the right direction i would appreciate it

The code that calls split_string

    char *param[5];
    for(i = 0;i < 5;i++) {
            param[i] = NULL;
    }
    char* user = getenv("LOGNAME");
    char tid[9];
    char* instring = (char*) malloc(201);

    /
    while((printf("%s %s >",user,gettime(tid)))&&(instring
            =fgets(instring,201,stdin)) != NULL) {
            int paramsize = split_string(param, instring);

The code that tries to free param

                for(i = 0;i < 5;i++) {
                if(param[i] != NULL) {
                    free(param[i]);
                    fprintf(stderr,"%d",i);
                }
            }

    int split_string(char** param, char* string) {
    int paramplace = 0; //hvor vi er i param
    int tempplace = 0; //hvor i temp vi er

    char* temp = malloc(201); 
    int command = 0; 
    int message = 0; 
    for(; (*string != '\0') && (*string != 10) && paramplace < 4; string++) {
            if((*string == ' ') && (message == 0)) {
                    if(command == 1) {
                        temp[tempplace] = '\0'; 
                        param[paramplace++] = temp; 
                        tempplace = 0;
                        command = 0;
                    }
            }
            else {
                    if(*string =='"') {
                            if(message == 0) message = 1;
                            else message = 0;
                    }
                    if(command == 0) {
                            free(temp);
                            temp = malloc(201);
                    }
                    command = 1;
                    if(*string != '"') {
                            temp[tempplace++] = *string;
                    }
            }
    }
    if(command == 1) {
            temp[tempplace] = '\0';
            param[paramplace++] = temp;
    }
    param[paramplace] = NULL;
    free(temp);
    return paramplace;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:12:44+00:00Added an answer on May 18, 2026 at 3:12 am

    As far as I can see, you want to put the split strings into param as an array of pointers (presumably making the caller responsible for freeing them). In the first branch of the if statement in your loop, you do so by assigning the current temp buffer to that place. However, once you start a new string (when comnmand == 0, you free that space, rendering the previous param entry pointer invalid.

    Only free each pointer once. I wouldn’t rule out other leaks in this code: I think you can simplify your state machine (and probably find other bugs as a result).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to split a string up into words and punctuation, adding the punctuation
i am trying to split a string up into separate lines with the following
Using Ruby I'm trying to split the following text with a Regex ~foo\~\=bar =cheese~monkey
I'm trying to split an HTML string by a token in order to create
string temp_constraint = row[Constraint_Name].ToString(); string split_string = FK_+tableName+_; string[] words = Regex.Split(temp_constraint, split_string); I
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I am trying to split a string from a recordset that is in the
I'm trying to split a string on its punctuation, but the string may contain
I am trying to find a way to disable Design / Split views from
Trying to make a MySQL-based application support MS SQL, I ran into the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.