Skip to main content
deleted 52 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Problem with char* variable malloc/free. Empty contents on recepientrecipient variable after using free

I am working on a custom SD Cardcard data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{
 
    char *filename = (char *)malloc(13 * sizeof(char));
 
    const char *uScore = "_";
 
    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);
 
    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }
 
    delete[] num;
 
    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output.

The issue is that when using the functions as follows, the contents of fileToDelete.fileName areis EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly, however. However, I am concerned about undefined behaviour or erratic responses.

Here is an example of a filename successfully created when NOT using free():

enter image description here

Do I invariably need to free the pointer after each use? Why the contents are empty even after assigning the contents to another variable?

EDIT:

Please, instead of downvoting my question, comment on what is wrong with my questions and I will try to improve it. Be mature.

Problem with char* variable malloc/free. Empty contents on recepient variable after using free

I am working on a custom SD Card data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{
 
    char *filename = (char *)malloc(13 * sizeof(char));
 
    const char *uScore = "_";
 
    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);
 
    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }
 
    delete[] num;
 
    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output

The issue is that when using the functions as follows, the contents of fileToDelete.fileName are EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly, however, I am concerned about undefined behaviour or erratic responses.

Here is an example of a filename successfully created when NOT using free():

enter image description here

Do I invariably need to free the pointer after each use? Why the contents are empty even after assigning the contents to another variable?

EDIT:

Please, instead of downvoting my question, comment on what is wrong with my questions and I will try to improve it. Be mature.

Problem with char* variable malloc/free. Empty contents on recipient variable after using free

I am working on a custom SD card data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{
    char *filename = (char *)malloc(13 * sizeof(char));
    const char *uScore = "_";
    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);
    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }
    delete[] num;
    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output.

The issue is that when using the functions as follows, fileToDelete.fileName is EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly. However, I am concerned about undefined behaviour or erratic responses.

Here is an example of a filename successfully created when NOT using free():

enter image description here

Do I invariably need to free the pointer after each use? Why the contents are empty even after assigning the contents to another variable?

EDIT:

Please, instead of downvoting my question, comment on what is wrong with my questions and I will try to improve it. Be mature.

added 391 characters in body
Source Link

I am working on a custom SD Card data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{

    char *filename = (char *)malloc(13 * sizeof(char));

    const char *uScore = "_";

    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);

    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }

    delete[] num;

    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output

The issue is that when using the functions as follows, the contents of fileToDelete.fileName are EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly, however, I am concerned about undefined behaviour or erratic responses.

Here is an example of a filename successfully created when NOT using free():

enter image description here

Do I invariably need to free the pointer after each use? Why the contents are empty even after assigning the contents to another variable?

EDIT:

Please, instead of downvoting my question, comment on what is wrong with my questions and I will try to improve it. Be mature.

I am working on a custom SD Card data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{

    char *filename = (char *)malloc(13 * sizeof(char));

    const char *uScore = "_";

    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);

    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }

    delete[] num;

    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output

The issue is that when using the functions as follows, the contents of fileToDelete.fileName are EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly, however, I am concerned about undefined behaviour or erratic responses.

Do I invariably need to free the pointer after each use?

I am working on a custom SD Card data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{

    char *filename = (char *)malloc(13 * sizeof(char));

    const char *uScore = "_";

    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);

    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }

    delete[] num;

    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output

The issue is that when using the functions as follows, the contents of fileToDelete.fileName are EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly, however, I am concerned about undefined behaviour or erratic responses.

Here is an example of a filename successfully created when NOT using free():

enter image description here

Do I invariably need to free the pointer after each use? Why the contents are empty even after assigning the contents to another variable?

EDIT:

Please, instead of downvoting my question, comment on what is wrong with my questions and I will try to improve it. Be mature.

Source Link

Problem with char* variable malloc/free. Empty contents on recepient variable after using free

I am working on a custom SD Card data logger using the following function and struct:

char *filenameCreator(const char *prefix, const char *num, bool addExtension)
{

    char *filename = (char *)malloc(13 * sizeof(char));

    const char *uScore = "_";

    strcpy(filename, prefix);
    strcat(filename, uScore);
    strcat(filename, num);

    if (addExtension)
    {
        const char *ext = LOGFILE_EXTENSION;
        strcat(filename, ext);
    }

    delete[] num;

    return filename;
}

where the output will be saved on a variable of the following type:

struct FileInfo
{
    const char *fileName;
    double fileSize;
};

with FileInfo fileToDelete; and a global variable char* createdFilename = nullptr; to temporarily store the function's output

The issue is that when using the functions as follows, the contents of fileToDelete.fileName are EMPTY:

createdFilename= filenameCreator("FILE", numericConverter(receivedIndexSATCOM), true);
fileToDelete.fileName = createdFilename;

free(createdFilename);    // seems to clear the contents of the variable where the filename was stored, namely, fileToDelete.fileName
createdFilename = nullptr;

I am trying to stick to the rules of freeing up memory used by malloc'd variables.

Please note that if I DON'T USE free(createdFilename), the code works fine and the fileName is created correctly, however, I am concerned about undefined behaviour or erratic responses.

Do I invariably need to free the pointer after each use?