I'm writing a native app for android using C. Somewhere I want to do a plain old job of getting a substring, which I have wrote a simple C code for it. This is the code:
char* get_part_allocated(const char* src, int start, int len)
{
LOGI("copying [%s] from [%d] len [%d]\n", src, start, len);
int nlen = len+1;
char* alloc = (char*)malloc(nlen*sizeof(char));
strncpy(alloc, src+start, len);
LOGI("result is: [%s]\n", alloc);
return alloc;
}
When I compile and run this code in PC, I get the expected result, but when I run it on android, it always has a strange character at the end which corrupts flow of my program and causes faults!
This is a screenshot of the logcat output:
I tried padding more NULLs, 2 or even 3, but didn't change anything!
Does someone know what is it and how can I get rid of it?