tags:

views:

57

answers:

1

I'm reading through the LZMA SDK source code and noticed that they assign pointers passed into a method to themselves - example (from the SDK, C/Util/7z/7zAlloc.c):

void *SzAlloc(void *p, size_t size)
{
  p = p;     <-- !
  if (size == 0)
    return 0;
  #ifdef _SZ_ALLOC_DEBUG
  fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount);
  g_allocCount++;
  #endif
  return malloc(size);
}

Can someone explain why they do this?

+8  A: 

To avoid compiler warnings on unused parameters.

Vicky
It's nicer to define a macro for this like #define UNUSED(x) ((x) = (x)). But it boils down to the same thing.
Vicky
Oh yeah, makes sense :) Thanks
VolkA
In these cases I prefer to remove the parameter name like this `void *SzAlloc(void *, size_t size)` but I'm not sure if it is a universally valid solution.
UncleZeiv
@UncleZiev: dropping the parameter name is one way to solve the unused parameter problem, but there can be reasons to not use that technique - it's not valid in C++ and there are times when you want to use the parameter in conditional code. It's not in the provided snippet, but the parameter might have been dumped in the `_SZ_ALLOC_DEBUG` block, for example.
Michael Burr