c - Pointer representation -


i have question pointers representation in c. if have correctly understand paragraph of c11 standard :

c11 (n1570), § 6.2.5.28, types, p. 36

similarly, pointers qualified or unqualified versions of compatible types shall have same representation , alignment requirements.

i deduced types int * , int const * have same representation not types int ** , int const **. right ?

if so, want know why 2 types don't have same representation in second case ? mean, int , int const have same representation, idem int * , int const *, what's matter int ** , int const ** ?

as bart van ingen schenau stated think standardization committee wanted put few restrictions on representation of pointers possible. compiler point of view useful have distinct representation int * , int const *, because constants placed in bigger or smaller memory , consequently smaller pointers used point smaller memories. have meant %s format specifier of printf should come in 2 versions, 1 constant string , 1 non-constant string. break lot of legacy code , guess standardization committee lacked courage enforce onto community. , rightfully so. there no compelling reasons enforce same representation int ** , int const **, left way. using different representation these pointer hardly ever useful, except maybe corner case applications.

they might have decided only char * , char const * should have same representation, save %s, maybe there other interfaces require equality pointers other primitive types well.

similarly on systems desirable have different pointer representation in global memory, on stack , in heap. possible in c use pointer in such way may contain either such pointer (again think of varargs) , required @ least have pointer representation can represent of them.

in embedded-c extension on c memory space have been introduced explicitly allow programmer data different data-buses without need pointer type can represent them all. such memory spaces used specify dedicated memories constants, heap etc. allowing more efficient pointer representations.

a colleague of mine mentioned k&r prototypes, or better absence thereof, still allowed. in such cases compiler can not detect difference in representation between parameter declaration , use. undoubtedly lead many undetected problems in legacy software. these issues have been resolved requiring use of common representation on parameter passing (including varargs) example done float double in varags, compromise intended efficiency of different representation.


Comments