Read carefully the documentation of scanf
, of malloc
, of realloc
, so read them several times. You could also refer to the C11 standard n1570 which mentions them (in §7.22.3).
Is this a correct implementation of realloc()?
You are not implementing realloc
. Here is a joke-implementation of it (see also this joke-implementation of malloc
):
void *realloc(void*ptr, size_t siz) {
errno = ENOMEM;
return NULL;
}
Of course you would actually use a serious implementation of realloc
(not the joke above), and it is provided by your C standard library implementation (e.g. above operating system primitives or system calls like mmap(2) on Linux).
So, no, you don't have any implementation of realloc
(and you would use the realloc
already implemented in your C standard library). And (on Linux at least) you can study the implementation of realloc
because it usually is implemented inside some free software (e.g. musl-libc or GNU glibc). Here (in its file src/malloc/malloc.c
line 369) is musl-libc
implementation of realloc
.
you are wrongly using realloc
Then, you are using scanf
as scanf("%s", input)
. But scanf
returns EOF
on failure, and the number of successfully input values on success. Usually EOF
is -1. So in your case, that scanf("%s", input)
can return -1 (on failure), 0 (if no input value has been processed) or 1 (if it has put something in input
).
The size_t
type is some unsigned integral type. On my Linux/x86-64 system, it is an unsigned 64 bits number, same as unsigned long
. So (size_t)(-1)
becomes a huge number, i.e. 264 - 1. Then realloc
will surely fail (because my system don't have that much memory) if given that (size_t)(-1)
so realloc(input, (size_t)-1)
should give NULL
.
If realloc
is given (size_t)0
, it is documented on Linux (see realloc(3)) to do what free
does: release the given memory. But the C standard don't require that behavior.
If realloc
is given (size_t)1
, if would (or at least is permitted to) shrink the memory zone (to only hold one byte, which is not enough for your needs).
So your program is completely wrong
BTW, you need to handle failure of realloc
, so coding input = realloc(input, newsize);
is very naive.
Also, your scanf("%6s", input);
is wrong (potential buffer overflow, for an input of exactly six bytes) since you need the space for the terminating NUL character.
So, throw your program to the garbage bin. Take some rest (or some fun). Read the documentation of standard functions (and the wikipage of C dynamic memory allocation). Think a little bit. And rewrite your program entirely.
Then compile your program with all warnings and debug info: gcc -Wall -Wextra -g
with GCC. Improve your code to get no warnings. Ensure that your program handle the failure cases (of scanf
, of malloc
, of realloc
, etc...). Read How to debug small programs. Use the gdb
debugger and valgrind. Be scared of undefined behavior.
For your program (the one you rewrite from scratch), you could be interested in using fgets (or even, on Linux, getline(3) or readline(3)...) for input. You may sometimes want to clear the input buffer with memset before doing the actual input.
Be aware that stdio is buffered, and stdout
is usually line-buffered. So take the habit of ending your printf
format string with \n
or use fflush
appropriately.
PS. valgrind
is available on Linux (but not on Windows), and that is one of the many reasons making Linux a very developer- and student- friendly operating system. So I do recommend using some Linux distribution to learn C programming.