On May 6, 6:46 pm, j.j.fish...@[EMAIL PROTECTED]
wrote:
> Hi all
>
> I have a program which, with inessential details removed,
> looks like this:
>
> --
> #include <stdlib.h>
> #include <stdio.h>
>
> static int punme(void** dat,size_t newsize)
> {
> void *newdat = realloc(*dat,newsize);
>
> if (! newdat) return 1;
>
> *dat = newdat;
>
> return 0;
>
> }
>
> int main (void)
> {
> char *dat = malloc(30);
>
> int ret = punme((void**)&dat,40);
>
> printf("punme returns %i\n",ret);
>
> return 0;}
>
> --
>
> My compiler (gcc -Wall -O3) tells me that "typepun.c:19:
> warning: dereferencing type-punned pointer will break
> strict-aliasing rules". The code works as expected, ie,
> prints that punme returns 0.
>
> Who is the idiot? Me or the compiler?
You (cast) and assign a char ** to a void **. void ** is not like void
*.
Fixed your code:
> static int punme(void* dat,size_t newsize) {
> void **tmp = dat;
> void *newdat = realloc(*tmp, newsize);
You also don't free the allocated memory.


|