Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:37:23+00:00 2026-06-07T13:37:23+00:00

I want to declare a constant array which can be accessed from multiple C

  • 0

I want to declare a constant array which can be accessed from multiple C files and whose content can be inlined by the compiler, without duplicating the memory in multiple compilation units. Performance is critical in my application.

Exhibit 1:

header.h:
static const int arr[2] = { 1, 2 };

file1.c:
#include "header.h"
void file1() { 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 replace arr[0] by 1 in file1. However, since arr is declared static const, its memory is duplicated in both C files. AFAIK the C standard requires the array addresses to be different in both files. I have verified this under Linux by printing out the addresses. No linker consolidation occurs even with -fmerge-all-constants in gcc.

Exhibit 2:

header.h:
extern const int arr[2];

file1.c:
#include "header.h"
void file1() { printf("%d\n", arr[0]); }

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

In that case, no memory duplication occurs but arr[0] is not inlined.

I consider the visibility scope defined by the C standard to be flawed. As such, a working solution under Linux/gcc which violates the C standard is acceptable to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:37:26+00:00Added an answer on June 7, 2026 at 1:37 pm

    One thing you might try:

    const int arr[2] __attribute__((weak)) = { 1, 2 };
    

    Now the array still exists in every *.o object, but when those objects are linked together in a program, GNU ld will reduce them to just one common chunk of data.

    If you don’t already have such a thing, you may want in some common header file:

    #ifndef __GNUC__
    #define __attribute__(x)
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to declare a huge list of constant array like: my @tlds =
I can: declare @idOrder int set @idOrder = 21319 I want: declare @idOrder int
I want to declare an array of handel as the following code: using namespace
How to declare a constant array in class with constant class variable? Is it
For example (using android) if I want to declare a constant of a Camera.Size
I want to define constants which can be used by all activities in an
If I want to declare a constant string (not necessarily changing based on language)
How do I declare static constant values in C++? I want to be able
I want to ask you if is possible to declare a constant in SQL
I want to declare some global variables and global constants. Normally, I would put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.