c - Issue with NULL pointers on Harvard Architecture platform -


interesting issue came across here week.

we working in c on harvard architecture embedded platform, has 16-bit data addresses , 32-bit code addresses.

the issue occurs when working function pointers. if have code

if (fp) fp(); 

or

if (fp != 0) fp(); 

everything fine.

however if have code like

if (fp != null) fp(); 

then, because null defined (void *) 0, compiler (gcc in case) a) not warn , b) 16-bit comparison against function pointer instead of 32-bit comparison. fine long function pointer doesn't happen lie on 64k boundary bottom 16 bits 0.

at moment have large swathes of code contain explicit checks against null. of them data pointers, of them function pointers. quick grep != null or == null revealed on 3000 results, many go through manually check.

so, either

  1. a way find cases function pointers (but not data pointers) compared (so can instead have them compare against fp_null define 32-bit 0), or

  2. to redefine null in such way right thing.

  3. (or, suppose, update our gcc port detect , correctly handle case).

i can't think of approach works 1. approach can think of 2 redefine null 0 function pointer, wasteful vast majority of comparisons against data pointers. (a 32-bit compare 4 instructions, 16-bit compare 1 instruction).

any thoughts or suggestions?

it seems me easiest way replace occurrences of null 0. works function pointer (as say) , object pointers.

this variant of (2) redefine null plain 0.

but fact cannot compare function pointers null bug in implementation. c99 states comparison of null pointer constant possible both object , function pointers, , null should expand constant.

small addition c-faq question 5.8:

q: null valid pointers functions?
a: yes (but see question 4.13)

mixing function pointers (void *) 0

(a reply r..'s comment). believe using function pointers , (void *) 0 well-defined. in reasoning refer sections of c99 draft 1256, not quote large parts keep readable. should applicable c89.

  • 6.3.2.3 (3) defines integer constant expression 0 , such expressions cast (void *) null pointer constant. and: "if null pointer constant converted pointer type, resulting pointer, called null pointer, guaranteed compare unequal pointer object or function."
  • 6.8.9 defines == , != operands (among other) pointer operand , null pointer constant. these: "if 1 operand pointer , other null pointer constant, null pointer constant converted type of pointer."

conclusion: in fp == (void *) 0, null pointer constant converted type of fp. null pointer can compared fp , guaranteed unequal fp if points function. assignment (=) has similar clause, fp = (void *) 0; well-defined c.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -