Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to stop implicit pointer conversions to void *

I need to find all such places in my source code where a pointer of any type is implicitly converted to void * or a way to stop these implicit conversions.

For example:

  1. int * to void *
  2. char * to void *
  3. Base * to void *

Is there any gcc warning or error flag which detects all such lines where a pointer is implicitly converted to void *?

like image 241
RYUZAKI Avatar asked Apr 30 '14 09:04

RYUZAKI


Video Answer


1 Answers

Say you have a simple program like

#include <string.h>
int main()
{
   char* apples = "apples and pears";
   char fruit[1024];
   void* avoid;
   int aint;
   float afloat;

   avoid = &aint;
   avoid = &afloat;
   memcpy(fruit, apples, strlen(apples) + 1);
}

Here is a trick that will tell you where some of the implicit conversions are happening. Add this at the start of the program

#define void double

You will get a lot of compile errors: including where the implicit conversions are happening. It won't spot void* = double* so you'll need to try again with

#define void int

to spot the rest.

like image 136
cup Avatar answered Oct 28 '22 02:10

cup