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

  • SEARCH
  • Home
  • 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 7437919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:28:26+00:00 2026-05-29T10:28:26+00:00

I’m C# man, new in C language working with points first time. I have

  • 0

I’m C# man, new in C language working with points first time.

I have this function that works with malloc(), realloc() and free() at future:

char ** split(char * delimiter, char * input) {

        int i = 0;
        int size = sizeof(char *);
        char ** tokens;
        char * token;
        char * state; 
        tokens = (char **) malloc(size);

        if(tokens == NULL) {
            printf("Allocation failed.");
            return;
        }

        for(token = strtok_r(input, delimiter, &state); 
             token != NULL;
             token =  strtok_r(NULL, delimiter, &state),
             i++, size *= i) {

             tokens = (char **) realloc(tokens, size);

             if(tokens == NULL) {
                printf("Realloc failed.");
                return;
             }

             tokens[i] = state;
        }

        return tokens;
}

when I call:

char * IPNumber = "127.0.01";
char * delimiter = "."; 
char ** parts = split(delimiter, IPNumber);

it gives segmentation fault.

I’m looking for an explanation how to get(calculate) the size value to be used in the second argument of realloc() function. Thanks in advance.

  • 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-29T10:28:27+00:00Added an answer on May 29, 2026 at 10:28 am

    Complete rewrite. There are some issues with the original code as posted.

    • The reallocation size computation is incorrect.
    • The passing of a string constant to strtok_r is not valid. It modifies the first argument, so that could result in an access violation when passed the string literal.
    • The assignment of the token into the result array starts at position 1 instead of 0.
    • The assignment uses the state variable instead of the token (probably not at all the desired result and probably undefined behavior).
    • There is no way for the caller to know how many tokens are in the returned array.
    • A failed call to realloc does not free the original pointer, so it would leak.

    So rather than attempt to describe the changes, I’ll follow the same pattern as others and show what might be a cleaner implementation with a single allocation based on the max possible number of tokens.

    char ** split(char * delimiter, char * input) {
        int size;
        int maxsize;
        char ** tokens;
        char * token;
        char * state;
    
        // compute max possible tokens, which is half the input length.
        // Add 1 for the case of odd strlen result and another +1 for
        // a NULL entry on the end
        maxsize = strlen( input ) / 2 + 2;
        tokens = (char**)malloc( maxsize * sizeof( char*) );
        if(tokens == NULL) {
            printf("Allocation failed.");
            return NULL;
        }
    
        size = 0;
        for(token = strtok_r(input, delimiter, &state);
             token != NULL;
             token =  strtok_r(NULL, delimiter, &state) ) {
    
             tokens[size++] = token;
        }
    
        assert( size < maxsize );
        // Put a NULL in the last entry so the caller knows how many entries
        // otherwise some integer value would need to be returned as an output
        // parameter.
        tokens[size] = NULL;
    
        // NOTE: could use realloc from maxsize down to size if desired
    
        return tokens;
    }
    

    Usage might look like the following. Note the use of strdup to avoid passing the string constant to the function:

    char * IPNumber = strdup( "127.0.01" );
    char * delimiter = ".";
    char ** parts = split(delimiter, IPNumber);
    int i;
    
    if ( parts ) {
       for ( i = 0; parts[i] != NULL; i++ )
          printf( "%s\n", parts[i] );
    
       free( parts );
       }
    
    free( IPNumber );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.