I get the following error below after calling realloc
void* realloc(sizeof(char)100);
The Error:
linex.c:23:16: error: expected declaration specifiers or ‘...’ before ‘sizeof’
Please help. Thank you. :)
I get the following error below after calling realloc
void* realloc(sizeof(char)100);
The Error:
linex.c:23:16: error: expected declaration specifiers or ‘...’ before ‘sizeof’
Please help. Thank you. :)
void realloc(sizeof(char)100);
Assuming you mean a call to the standard realloc()
function (not a declaration of a custom realloc()
) and you want to resize memory, this code is wrong regarding three points.
realloc()
needs a pointer to memory allocated by a memory-management and not already freed as first argument.
This is the prototype/declaration for standard realloc()
function:
void *realloc( void *ptr, size_t new_size );
Your realloc()
call omits this pointer argument.
Don't specify the return type (here void *
) when you want to call a function. With this you attempt to make a new declaration of the function realloc()
and not a call.
If you tried to do some casting in here, this is also wrong. For an implicit cast, you need to surround the type to be casted to by parentheses like (void *)
.
Note that in both cases the cast is redundant. With view on the cast of the return type, have a look at:
At the size argument you need the *
operator between sizeof(char)
and 100
.
Use:
realloc( ptr, sizeof(char) * 100 );
Note that you also always should check the return value of memory-management functions whether an error occured or not:
char * ptr2 = realloc( ptr1, sizeof(char) * 100);
if ( ptr2 == NULL )
{
fputs("Error at resizing the allocated memory!\n", stderr);
// error routine.
}
The same you always should do for malloc()
:
char * ptr1 = malloc( sizeof(char) * 50);
if ( ptr1 == NULL )
{
fputs("Error at memory allocation!\n", stderr);
// error routine.
}
Advanced lesson (You don't need to understand it at this point of time):
Note that in the case of realloc()
, I use a different pointer to catch the return value of realloc()
.
This is because realloc()
may or may not return the same pointer passed as argument.
An unfortunately common but bad practice is to reassign the pointer passed as first argument by the return value of realloc()
.
This is dangerous since the reference to the memory allocated first can then be lost if realloc()
allocated another "replacement" memory but didn't erased the first.
Related:
void* realloc(sizeof(char)*100);
is a mishmash of declaring a function and calling it.
When we declare a function, we say what its return type is and what types its parameters are, as in void *realloc(void *, size_t);
. We may also provide placeholder names for the parameters as helpful descriptions, as in void *realloc(void *pointer, size_t size);
. (Parameter names are required when fully defining the function.)
When we call a function, we do not give type names for the return value or the parameters. We just give expressions that provide the argument values, as in NewPointer = realloc(OldPointer, 100 * sizeof *NewPointer);
.
Sometimes type names may appear in or near a call to a function, but these are incidental and are not directly part of the function call. Examples are:
char *NewPointer = realloc(OldPointer, 100 * sizeof *NewPointer);
: This code has char *
because it declares and defines NewPointer
. Then NewPointer
is initialized with the realloc
call.NewPointer = (char *) realloc(OldPointer, 100 * sizeof *NewPointer);
: This code has (char *)
as a cast to convert the return value of realloc
to a specific pointer type. This is not needed in C.NewPointer = realloc(OldPointer, 100 * sizeof(char));
: This code has char *
as an operand to sizeof
to calculate the space needed. This is unnecessary because the size can be taken with sizeof *NewPointer
, and that is generally preferable because it automatically adjusts to changes in the declaration of NewPointer
, reducing the possibility that a bug might be introduced by changing the type in the declaration of NewPointer
but overlooking the sizeof
expression.