Freeing memory after calling strtok () causes an error

could you help me? My code performs tokenization, so I created this code:

  • I allocate some memory
  • I strcpy(malloced_memory, argv)
  • Doing strtok(mallocted_memory, ".")
  • Give it a try free(mallocted_memory).

    filename = malloc(strlen(argv));
    
    strcpy(filename, argv);
    strk_ptr = malloc(sizeof(filename));
    strk_ptr = strtok(filename,".");//
    i++;
    sprintf(in->file_name,"%s",strk_ptr);
    
    while(strk_ptr = strtok(NULL,"."))//
    {
        i++;
        sprintf(in->file_name,"%s.%s",in->file_name,strk_ptr);
        sprintf(in->file_ext ,"%s",strk_ptr);
    }
    free(strk_ptr);
    free(filename);
    

This code has a problem that I cannot free(filename). If I try free(filename), then the program will receive SIGTRAP. But the program works.

I want to fix this problem. What should I do?

+4
source share
4 answers

This line:

filename = malloc(sizeof(argv));

should be as follows:

filename = malloc(strlen(argv) + 1);     /* +1 for the '\0' at the end */
if (filename == NULL) { /* take some action */ }

And this line:

strk_ptr = malloc(sizeof(filename));

only creates a memory leak, since it is followed by:

strk_ptr = strtok(filename,".");

And you should check the return value:

strk_ptr = strtok(filename,".");
if (strk_ptr == NULL) { /* take some action */ }

BTW, strtok() , (filename ). , ( , ). strtok(), , ( ) tokenize, readonly. , : strtok("sample.txt", ".") - -go.

, , :

while (strk_ptr = strtok(NULL,".")) { ... }

:

while ((strk_ptr = strtok(NULL,".")) != NULL) { ... }
+3

strtok()

, malloc(), . str_ptr:

strk_ptr = malloc(sizeof(filename));

malloc() , strk_ptr. strtok(), , _:

strk_ptr = strtok(filename,".");

, , malloc(), strk_ptr - . free(str_ptr), filename. free(filename) . strk_ptr.

, , strtok. , , , , .

int main(int argc, char **argv) {

    char *strk_ptr;
    char *filename = malloc(strlen(argv[0]) + 1);

    strcpy(filename, argv[0]);

    printf("filename = %s, size = %zu\n", filename, sizeof(filename));

    // Do not malloc this
    //strk_ptr = malloc(strlen(filename) + 1);
    strk_ptr = strtok(filename,".");//
    printf("%s\n", strk_ptr);

    while( (strk_ptr = strtok(NULL,".")) )
    {
        printf("%s\n", strk_ptr);
    }
    free(filename);

    return 0;
}

argv char **, , , , argv [0], .

sizeof(filename) , , _ . strlen(filename) + 1.

strtok (_), , strk_ptr.

strtok :

   for (strk_ptr = strtok(filename, "."); strk_ptr; strk_ptr = strtok(NULL, "."))
    {
        printf("%s\n", strk_ptr);
    }
+2
 filename = malloc(strlen(argv));
 strk_ptr = malloc(sizeof(filename));

strk_ptr , , strk_ptr , .

malloc strk_ptr. char *,

+1
strk_ptr = malloc(sizeof(filename));
strk_ptr = strtok(filename,".");//
...
free(strk_ptr);

. strk_ptr malloc'd, - , malloc'd , , free .

Edit:

malloc(sizeof(filename)), , -. char* strk_ptr; (.. 4 8 ). , , , free .

char* strk_ptr;
strk_ptr = strtok(filename,".");

Or, if that was not your intention, note that it sizeof(filename)does not return the length of the string, but simply the size of the pointer variable filename, i.e. usually 4 or 8, regardless of which string filenamepoints to. See also http://www.gnu.org/software/libc/manual/html_node/String-Length.html :

char string[32] = "hello, world";
char *ptr = string;
sizeof (string)32
sizeof (ptr)4  /* (on a machine with 4 byte pointers) */
0
source

Source: https://habr.com/ru/post/1624068/


All Articles