I'm writing a project in C and I'm getting this warning
local variable may point to deallocated memory
for the following code
int printSched()
{
char* schedString = (char*)malloc(strlen("cat /proc/sched\n")+sizeof(getppid()));
if (schedString) {
sprintf(schedString, "cat /proc/%d/sched\n", getppid());
int commandSize = numOfWords(schedString);
char** schedCommand = (char**) malloc((commandSize + 1) * sizeof (char*));
if(schedCommand == NULL)
{
free(schedString);
fprintf(stderr, "malloc failed");
exit(1);
}
schedCommand[commandSize] = NULL;
parseString(schedString, schedCommand);
exeCommand(schedCommand);
freeArr(schedCommand);
return 0;
}
else //malloc failed - handled in main
return -1;
}
about this particular free command
free(schedString);
I strognly believe that this message didn't appeared before the latest update of CLion to version 2021.1
Am I doing something wrong (atleast about this part) or it's a CLion problem?
numOfWords function
//receives a sentence and returns the number of words in it, ignoring blank spaces, assuming it ends with '\n'
int numOfWords(const char sentence[] )
{
int i = 0, wordCounter = 0;
while(sentence[i] != '\n')
{
if(sentence[i] != ' ' && (sentence[i+1] == ' ' || sentence[i+1] == '\n'))
wordCounter++;
i++;
}
return wordCounter;
}