here i have one function which is call many times in one application this application is running continuously.
Here i am take one character array size of 1024.
here i am declaring char input[1024];
so which is best way
1) char input[1024];
2) char input[1024] = NULL;
this thing will not create any memory issue after so many times if we used this function.char input[1024];
i think may be after using input we have to make NULL ?
or in declaration we have to declare this thing as char input[1024] = NULL; so when it will be called next time so that time first input make null than its taking any memory.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void do() {
char input[1024];
strcat(input,"ussp-push 04:18:0F:B1:48:B5@9");
strcat(input,"> dev/null&");
if(system(input) != 0)
{
printf("\nFailed command\n");
}
else
{
printf("\nSuccesss command\n");
}
}
Your program does not initialise
inputat all. This can lead to buffer overrun and even if that does not happen, your program is likely to have incorrect results. Thus your code invokes undefined behaviour.Presumably you actually want to initialize
inputto be an empty string. Like this:Once you have done this your calls to
strcatwill work.If you don’t want to initialise
inputthen you can usestrcpyinstead of the firststrcat:If ever you need to assign the empty string to
inputlater in the code do it like this:The code you suggest,
char input[1024] = NULLis not valid. It does not compile.