Reputation: 706
What's the difference between this:
void copydata(void *, void *, size_t);
and this:
void *copydata(void *, void *, size_t);
I use pointer functions when they must return a pointer, for example:
char *myName(int i)
{
switch (i) { case 0: return "Jack"; break; default: return "Adam"; }
}
but in the "void *copydata" example, the function doesn't have to return anything, and it compiles correctly in both ways, and the resulting asm code appears to be the same in both cases.
Upvotes: 1
Views: 129
Reputation: 41
In terms of memory allocations: 'void *copydata()' will behave exactly the same as 'int *copydata()' and 'double copydata()' etc. That means there will some memory allocated on the stack to store the return value of the function (subject to the compiler implementation and optimization). If you don't do 'return result;' that memory won't be correctly initialized. I think the result will be the same as trying to use uninitialized variable. Although it won't break your program if you don't use the result.
Upvotes: 0
Reputation: 7128
If return type is void * you must return a pointer with no type, where functions having void return type does not need to return anything. However that can use return; within the function (not necessary at the end) to return the control conditionally.
Upvotes: 0
Reputation: 227548
What's the difference between this:
void copydata(void *, void *, size_t); // (1)
and this:
void *copydata(void *, void *, size_t); // (2)
(1)
is a void
function. That means it does not return anything. (2)
is a function returning a pointer-to-void
, i.e. a void*
. It has to return something.
but in the "void *copydata" esample, the function doesn't have to return anything,
That is incorrect. It has to return something that is a void*
, or is convertible to one.
and it compiles correctly in both ways, and the resulting asm code appears to be the same in both cases.
Not returning from a non-void
function is undefined behaviour. The compiler doesn't have to produce an error, but the resulting code cannot be trusted.
Upvotes: 3
Reputation: 3162
void copydata(void *, void *, size_t);
This means the function doesnt return anything. where as,
void *copydata(void *, void *, size_t);
means that the function returns a pointer. In 2nd case the function should return a pointer to any thing.
Upvotes: 0
Reputation: 43662
The first one is a void
function: it returns no value.
The second one returns a pointer to void
: it returns a pointer.
In the code posted you're returning the addresses of char literals. If you don't return anything while your code is expecting a pointer you're getting undefined behavior since you're not even touching the pointer at the "return end" of the call and dirty memory will be in place (or in the best case zero-initialized if it's a global).
More info here: https://stackoverflow.com/a/1610454/1938163
Upvotes: 2
Reputation: 17605
The first function would return nothing while the second one would return a void pointer.
Upvotes: 0