Products > Programming

mycc built-in field_sizeof(), sizeof struct field

(1/3) > >>

DiTBho:
so, in in mycc it's a build-in operator since the end of 2022 :D :D :D


--- Code: ---2022/12/20 query/lib_file_query_v1.c:        mystring_clean(query_item[i0].name, field_sizeof(file_query_item_t, name));
2022/12/20 query/lib_file_query_v1.c:        mystring_clean(query_item[i0].mape, field_sizeof(file_query_item_t, mape));

--- End code ---
(first time I needed something like this, probably a silly case ...
... anyway it's when I put the functionality in the compiler
after that I started using it wherever it is needed/makes the code more clean)


--- Code: ---typedef struct
{
    uint32_t  fp;
    char_t    name[file_query_item_name_size];
    char_t    mape[file_query_item_mape_size];
    uint32_t  flags;
    uint32_t  mode;
    boolean_t is_empty;
} file_query_item_t;

--- End code ---
(xinu code)

field_sizeof(file_query_item_t, name)
             retuns file_query_item_name_size

field_sizeof(file_query_item_t, mape)
             retuns file_query_item_mape_size

it's a *very* usefult feature!

C/89 doesn't have it and in Linux you have to "emulate" it by this

--- Code: ---#define field_sizeof(t, f) (sizeof(((t*)0)->f))

--- End code ---

My opinion: this topic is informative, the field_sizeof() operator should be built-in in every C/C-like compiler! :D

Nominal Animal:

--- Quote from: DiTBho on May 16, 2024, 09:55:28 am ---C/89 doesn't have it and in Linux you have to "emulate" it by this

--- Code: ---#define field_sizeof(t, f) (sizeof(((t*)0)->f))

--- End code ---

--- End quote ---
In the C23 standard, however, footnote 4 explicitly states that (void *)0 is valid in the operand of alignas, alignof, and sizeof expressions.  Thus, in C23,
    #define  field_sizeof(type, member)  (sizeof ((type *)0)->member)
can be understood/accepted as being valid (as long as member is not a variable length array at least).

There are also quite a few userspace users of that, and in definitions of the related offsetof macro,
    #define  offsetof(type, member)  ((size_t) &(((type *)0)->member))
so any compiler that cannot handle these will fail to compile quite lot of existing C code, too.

(It is important to remember that aside from C11, the C standard has evolved by including stuff compilers have implemented and users use; not by defining or dictating completely new features not implemented yet by any compiler.)


--- Quote from: DiTBho on May 16, 2024, 09:55:28 am ---My opinion: this topic is informative, the field_sizeof() operator should be built-in in every C/C-like compiler! :D
--- End quote ---
Agreed.

GCC has traditionally implemented many related operators as an extension to C:
* __alignof__, standardized in C11 as _Alignof and in C23 as alignof
* __alignas__, standardized in C11 as _Alignas and in C23 as alignas
* typeof, standardized in C23 as typeof (and typeof_unqual for the non-atomic unqualified type of the expression)which are similarly useful.  typeof and compound statements in parentheses (another GCC extension) allow macros that only evaluate their parameters only once (as long as they are not variably modified types):
    #define  foo(x, y)  ({ typeof (x) _x = (x); typeof (y) _y = (y); use _x and _y ... ; result; })

SiliconWizard:
Yes, all this has been available on many C compilers for a long time, but was not strictly allowed by the C standard until C23.

DiTBho:

--- Code: ---         mystring_clean(query_item[i0].mape, file_query_item_t.name'size);

--- End code ---
2024/5/16: now, mycc can do this  :o :o :o

DiTBho:

--- Quote from: SiliconWizard on May 16, 2024, 11:04:08 pm ---Yes, all this has been available on many C compilers for a long time, but was not strictly allowed by the C standard until C23.

--- End quote ---

Yup, the above trick (first post) is used in recent linux kernels, so I have already updated the builder database (which checks the environment before allowing the user to compile things) to make sure kernels and part of the Catalyst rootfs, which use the same trick, are compiled with the correct version of Gcc and right flags, in order to have C23 support.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod