char input[500], output[500] = {}, buffer[2] = {' ', '\0'};
This is not a good way to allocate memory in C. What this means in C is that the memory is only guaranteed unique during the scope of the function. So if you have
str1 = strreverse(input1);
str2 = strreverse(input2);
Then str1
and str2
will often both point to the reverse of input2
, as you wrote over the first call on input1
.
This can happen in two different ways. Sometimes the compiler will allocate a small amount of memory to the function and leave it allocated for the entire program. You can force it to happen that way by declaring the variables as static
which says that the same variable will be used throughout the program's duration.
The second way that this can happen is that the memory can be implicitly allocated and deallocated during the scope of the function call. Then the memory is available to be allocated to someone else. If you call the same function twice in a row, it is possible if not likely to allocate the same memory both times. But in some ways it is worse if you do other things in between. Then the memory that pointed to output
the first time may be used for other variables and get overwritten wholly or partially by other operations.
Which method it uses is up to the compiler (unless you make the variable static
in which case it will always do it the first way; or make the function recursive in which case it can't do it the first way). The key point is that you should not rely on this pattern producing correct or even predictable results. Unlike a garbage collected language, C does not track memory usage until it has no pointers to it. It deallocates static allocations based on scope.
The first method is static allocation on the .bss or data segments. The second method is automatic allocation (which is still static) using the stack. If you use malloc
or another dynamic allocation method, you are using heap allocation. If you want to read more about how this works, you might start with What and where are the stack and heap? You can follow up with the Difference between static memory allocation and dynamic memory allocation.
The more normal way to handle this is to pass the output string into the function
strreverse(in, reversed, maximum_length);
Then the function doesn't have to worry about allocating memory. Another possibility would be to have the function allocate memory with malloc
or similar, but then you have to explicitly free
memory allocated implicitly by the function. By forcing the caller to do that, the caller can figure out how to get the memory allocated and deallocated.
char* in = malloc(256);
Any time you explicitly allocate memory like this, you should also explicitly free
the memory.
It won't matter in this program as the program will end and deallocate all the memory right after you would free
it (and some would leave it out for that reason). But in a real program this could cause a memory leak.
strcpy(input, c);
You should use strncpy
instead.
strncpy(input, c, 500);
input[499] = '\0';
This will guarantee that c
is never longer than input
can hold and that input
is always null terminated.
Alternately, you could check that c
is no longer than will fit in input
and do something if it is.
for(i = strlen(input); i >= 0; i--)
{
buffer[0] = input[i];
Note that on the first iteration, you will copy the null byte at the end of the string. The strcat
function handles this (by copying a zero length string into output
), but this is pure waste. This should be
for (i = strlen(input) - 1; i >= 0; i--)
{
buffer[0] = input[i];
output[]
. This is undefined behavior and a major code flaw. Goods answer should mention that. \$\endgroup\$