A question i could not find anything about on the internet. I have this little piece of C-code running on a Linux Wheezy distro (Raspberry Pi, but thats not relevant):
void function(const char * command)
{
// Define commands for in between parameters
char commandPre[] = "echo ";
// Get the lengths of the strings
int len= strlen(command) + strlen(commandPre);
// Allocate the command
char * fullCommand = (char *) malloc(len * sizeof(char));
// Build the command
strcat(fullCommand, commandPre);
strcat(fullCommand, command);
// Execute command
system(fullCommand);
// Free resources
free(fullCommand);
}
Now, I'm running this piece of code from a daemon program. But when it reaches free(fullCommand) a second time (when function gets called a second time in my program), the program crashes and exists. When i remove the free(fullCommand), it works as expected.
My question is: Is system() already freeing "fullCommand" for me? If so, why does it work the second time and not the first time? Am I missing something here?
P.S. Actually command is build up of several strings strcat'ed together, but above is the code in its most basic form