Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Ethereal-dev: Re: [Ethereal-dev] Help with "different 'const' qualifiers", "pointer mismatch f

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <gharris@xxxxxxxxx>
Date: Mon, 07 Feb 2005 12:10:23 -0800
Francisco Alcoba (TS/EEM) wrote:

So, if I have

	address tmp_src, tmp_dst;
	guint32 ipv4_address;

and do
      g_free(tmp_src.data);
or
	g_memmove(tmp_src.data,&ipv4_address,4);

I get both warnings in a row. I do not have them if I do

 	g_free((void *)tmp_src.data)

but I'm not sure this is OK -meaning, it will do what I need without breaking anything
else-.

That's the best you can do when freeing data, as far as I know; it's what we do elsewhere.

The underlying problem is that there is, as far as I know, no good way in C to indicate that some piece of data is immutable, i.e. once it's been initialized its value can't be changed, but can be freed, so I think all you can do is throw in a cast to indicate that it's OK to free the data.

For initializing the data, I'd do

	guint32 *src_data;

	src_data = g_malloc(sizeof *src_data);
	*src_data = ipv4_address;
	tmp_src.data = &src_data;

I've found a reference to a similar situation, that claims this to be some sort of VC6-specific behaviour,

The claim isn't true - the idea of warning about a const pointer being passed to a routine where the matching argument isn't VC6-specific, only the text of the message is:

	$ cat foo.c
	#include <stdlib.h>

	void
	freeit(const char *p)
	{
		free(p);
	}
	$ gcc -c -O2 -Wall -W foo.c
	foo.c: In function `freeit':
foo.c:6: warning: passing arg 1 of `free' discards qualifiers from pointer target type