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 7442421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:10:21+00:00 2026-05-29T11:10:21+00:00

I have extracted the meaning part of my code (and also replace some line

  • 0

I have extracted the “meaning” part of my code (and also replace some line to simplify it).

I have 2 dynamic pointers, one for the current line (extracted from a file) and a second for the current token.
Following this question, Free/delete strtok_r pointer before processing complete string?
I wrote this :

int main(void) {
    int n = 455;  
    char *tok2, *freetok2;
    char *line, *freeline;

    line = freeline = malloc(n*sizeof(*line));
    tok2 = freetok2 = malloc(n*sizeof(*tok2));

    /* content of the file) */
    const char* file_reading =  "coucou/gniagnia/puet/";

    /* reading from the file */
    strcpy(line, file_reading);

    strtok(line, "/");
    /* get the second token of the line */
    tok2 = strtok(NULL, "/");

    fprintf(stdout, "%s \n", tok2); // print gniagnia
    fprintf(stdout, "%s \n", line); // print coucou

    /* free error */
    //free(tok2);

    /* worked, but maybe don't free "everything ?" */
    //free(line);

    free(freetok2);
    free(freeline);

    return 0;
}

But at the end, I’m not sure of what is correct or not, and I find this solution not so elegant (because of using 2 “save variables”.

Is that correct ? Is there some ways to improve it ?
Thanks

Edit: changed my code for this, (and it will handle all the lines of the file)

include <unistd.h>
include <stdlib.h>

int main(void) {
    char *tok2; 
    char *line; 

    /* content of the file) */
    const char* file_reading =  "coucou/gniagnia/puet/";
    const char* file_reading2 =  "blabla/dadada/";

    /* reading from the file */
    line = strdup(file_reading);

    strtok(line, "/");
    /* get the second token of the line */
    tok2 = strtok(NULL, "/");

    printf("%s \n", tok2);
    printf("%s \n", line);

    /* reading from the file */
    line = strdup(file_reading2);

    strtok(line, "/");
    /* get the second token of the line */
    tok2 = strtok(NULL, "/");

    printf("%s \n", tok2);
    printf("%s \n", line);

    free(line);

    return 0;
}
  • 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-29T11:10:22+00:00Added an answer on May 29, 2026 at 11:10 am

    You’re not actually using the memory pointed by freetok2, you don’t need to malloc anything, thus you don’t need the freetok2 variable.

    Saying free(line) or free(freeline) is the same in your code so you don’t need the freeline at all.

    Another problem is this: malloc(n*sizeof(*line));. You might as well be saying: malloc(n); because sizeof(char) is always 1. But best of all would be:

    line = malloc(strlen(file_reading) + 1);
    strcpy(line, file_reading);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some text that I have extracted from a PDF document that has
I have just Extracted the cells from the excel sheet using Apache POI, everything
I have some html extracted to a string var, and want to then use
I have to extract defined variable and function names from a js code passed
I have extracted html source from a web page, and was wondering how to
I have extracted records from a database and stored them on an HTML page
I have extracted a number of OleObject files from a Word DOCX document (oleObject1.bin
I have extracted this from the book called Rhomobile Beginner's Guide Abhishek Nalwaya Published
I have the following code to read data from my .plist for use in
I have to migrate data from one database to another. As preparation we have

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.