Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #67964

Re: type safety on simple data types - without structs?

From mathog <dmathog@gmail.com>
Newsgroups comp.lang.c
Subject Re: type safety on simple data types - without structs?
Date 2015-08-21 13:47 -0700
Organization A noiseless patient Spider
Message-ID <mr82ms$u7e$1@dont-email.me> (permalink)
References <mqtoj6$8co$1@dont-email.me> <f1de5bec-5e5f-4848-b751-b389ddc69ee9@googlegroups.com>

Show all headers | View raw


Malcolm McLean wrote:
> On Monday, August 17, 2015 at 11:54:11 PM UTC+1, mathog wrote:
>>
>> magic_here char * type_safe_char_ptr_A;
>> magic_here char * type_safe_char_ptr_B;
>>
>> where "magic_here" has the same syntax as typedef, such that
>>
>> int get_A(type_safe_char_ptr_A ptr){
>>     // do something
>> }
>>
>> will throw an error at compilation time if it is called like:
>>
>>   In this example there is no offset
>> to either of the desired values, and there are no other values in
>> the structs. So the struct method seems a bit clunky, in that one must
>> use an "access member" syntax when it seems like it shouldn't be needed.
>>
> I'd say that the struct method isn't clunky, unless you need to
> micro-optimise.

I tried it and it wasn't all that terrible looking when it was finished. 
  It wasn't perfect though.  One of the types was
a uint32_t record number which was stored in an array.
A struct with a "payload" like this:

    uint32_t val;

has a sizeof() on the 64bit system of 8 and not 4, so it wasn't possible 
to cast the array and pull out the struct based types directly - even 
though all the uint32_t's were aligned properly for the data type, they 
were not aligned for the struct.

All in all though, it was still better with with the extra level of type 
safety, since there were locations with 2 or 3 different types of 
integer indices and their associated char * pointers in play, and this 
keeps them from being used accidentally in the wrong places.

I suppose some compilers may have a switch that would force the size of 
the struct down to something smaller than the native machine's word 
size, but that isn't portable as far as the language is concerned.

Of course, the problem mentioned above for the 32 bit type would also
occur for all 64bit ints on a 128 bit machine, should this code ever 
encounter such a beast.

Regards,

David Mathog

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

type safety on simple data types - without structs? mathog <dmathog@gmail.com> - 2015-08-17 15:52 -0700
  Re: type safety on simple data types - without structs? Ian Collins <ian-news@hotmail.com> - 2015-08-18 11:11 +1200
    Re: type safety on simple data types - without structs? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-08-18 18:16 +0000
  Re: type safety on simple data types - without structs? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-18 04:14 -0700
    Re: type safety on simple data types - without structs? mathog <dmathog@gmail.com> - 2015-08-21 13:47 -0700

csiph-web