Bug 34326 - pointer arithmetic on void pointers does not generate an error
Summary: pointer arithmetic on void pointers does not generate an error
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-12-03 16:00 UTC by John Parker
Modified: 2007-12-03 16:24 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
simple .c example (143 bytes, text/plain)
2007-12-03 16:03 UTC, John Parker
Details

Note You need to log in before you can comment on or make changes to this bug.
Description John Parker 2007-12-03 16:00:29 UTC
I was surprised that gcc did not find an error in the following example.
My understanding is that assignment to a void pointer is ok, but arithmetic
is _not_ ok. I have tried this code on my arm cross compiler gcc v. 4.1.1 and my ubuntu system compiler 4.1.3. Gcc treats the void pointer type as char 

gcc --version
        gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

Example pointer arithmetic on a void pointer that should have caused an error
   #include <stdlib.h>
   int main (int argc, char **argv)
   {
      void *x;
      x = malloc(1025 * sizeof(*x));

      x++;    // i expected an error on this line!

      return 1;
   }
Comment 1 John Parker 2007-12-03 16:03:53 UTC
Created attachment 14694 [details]
simple .c example
Comment 2 Richard Biener 2007-12-03 16:24:16 UTC
This is a GCC extension.  Use -pedantic to get a warning, -pedantic-errors to
get an error.
Comment 3 brian 2007-12-03 16:25:42 UTC
Subject: Re:   New: pointer arithmetic on void pointers does not 
 generate an error

This is a GNU C extension, see
<http://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html>.  I think you can
disable it with -std=c89 or -std=c99.