Returning a string from a function in C

前端 未结 7 1073
耶瑟儿~
耶瑟儿~ 2020-12-18 07:28

I have a c function that I want to return a string.

If I print the string before it is returned then I see croc_data_0186.idx

If I try and print the string

相关标签:
7条回答
  • 2020-12-18 07:41

    Line is a local variable and is removed at the end of the function.

    You should use malloc, or strcpy it to a string pointer passed as argument.

    0 讨论(0)
  • 2020-12-18 07:43

    It is because you return invalid pointers.

        char* fileNameString;
    

    is just a pointer.

        char line[lineLength];
    

    lives on the stack and is filled with a fgets() call.

        char *lineElements[3];
        lineElements[0] = strtok(line, ":");
        lineElements[1] = strtok(NULL, ":");
        lineElements[2] = strtok(NULL, ":");
    

    Here you store pointers into that array. One of them is

        fileNameString = lineElements[2];
    

    which you

        return fileNameString;
    

    afterwards.

    The solution would be to

    • either malloc enough space inside the function and copy your string to the new memory block or

    • have the caller provide a buffer you write the data into.

    0 讨论(0)
  • 2020-12-18 07:44

    You are returning a pointer to local data, which is not valid after the function returns. You have to allocate the string properly.

    This can be done in either the calling function, by supplying a buffer to the called function, and it copies the string over to the supplied buffer. Like this:

    char segmentFileName[SOME_SIZE];
    getSegmentFileName(file, lineLength, mid, segmentFileName);
    

    and the getSegmentFileName function:

    void getSegmentFileName(FILE *file, int lineLength, int lineNumber, char *segmentFileName)
    {
        /* ... */
    
        strcpy(segmentFileName, fileNameString);
    }
    

    The other solution is to allocate the memory for the string in getSegmentFileName:

    return strdup(fileNameString);
    

    but then you have to remember to free the string later.

    0 讨论(0)
  • 2020-12-18 07:48

    You are returning a pointer to a local variable that doesn't exist anymore when the function returns. You have to malloc storage for it and return that. Alternatively, you can let the caller pass in a buffer to be filled. In any case the caller is responsible for freeing the memory later.

    0 讨论(0)
  • 2020-12-18 07:55

    This is because you are returning a pointer to local. This is undefined behavior.

    strtok returns a pointer into the line character array. You put that pointer into fileNameString, and return to the caller. At this time the memory inside line becomes invalid: any garbage can be written into it.

    To avoid this problem, you should either pass a buffer/length pair for the return value, or use strdup() on the string that you are returning. In the later case you should remember to free the memory allocated for the returned string by strdup().

    On a related subject, you should avoid using strtok, because it is not re-entrant, and will cause issues in multithreaded environments. Consider using strtok_r instead.

    0 讨论(0)
  • 2020-12-18 07:56

    The problem is that you are returning an stack variable, lost when function returns. One way to make this is to use a char * arg in function parameter, with enough reserved space, and use it to store all information and returns it.

    0 讨论(0)
提交回复
热议问题