Inlinable constant C array with no memory duplication -


I want to declare a continuous array that can be accessed from many files and whose contents can inline by the compiler , Without repeating the memory in multiple compilation units. Performance is important in my application.

Exhibit 1:

  header.h: static const int arr [2] = {1, 2}; File1.c: #include "header.h" Zero File 1 () {printf ("% d \ n", arr [0]); } File2.c: #include "header.h" int file2 () {for (int i = 0; i <2; i ++) printf ("% d \ n", arr [i]); }   

In that case, the compiler can change to file1 in arr [0] by 1 . However, since arr has been declared Static Constance , its memory is repeated in both C's files AFAIK C standard requires array addresses, which are separate in both files it occurs. I have verified it by printing addresses through Linux. There is also no linker consolidation with the -fmerge-all-constantents in GCC.

Exhibit 2:

  header.h: External Consultant AR arr [2]; File1.c: #include "header.h" Zero File 1 () {printf ("% d \ n", arr [0]); } File2.c: #include "header.h" const an arr [2] = {1, 2}; Int file2 () {for (int i = 0; i & lt; 2; i ++) printf ("% d \ n", arr [i]); }   

In that case, there is no memory duplication, but arr [0] is not inline.

I believe the standard of visibility scope may be faulty. For example, work solution under Linux / GCC which violates the standard, is acceptable to me.

One you can try:

  const ac arr [ 2] __attribute __ ((weakening)) = {1, 2};   

Now the octor is still present in every. .o object, but when those objects are added to a program, then GNU ld is only a normal Part of the data will be reduced in

If you do not have any such work, then you want in some general header file:

  #ifndef __GNUC__ #define________ #endif    

Comments