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 3669896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:17:55+00:00 2026-05-19T02:17:55+00:00

I know this is a question that every programmer should know, but I do

  • 0

I know this is a question that every programmer should know, but I do not know. Long time no C programming and I’ve forgotten a lot of things.

My question is:

I have three huge static arrays defined inside a header file. Someone told me that It’s much better to declare them as extern in the header file, and define them in a single C or C++ source file.

How can I do that?

Here is my header file:

#ifndef _TEMPLE_OBJECT_H_
#define _TEMPLE_OBJECT_H_


#define NUM_TEMPLE_OBJECT_VERTEX 10818

static const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
static const float TEMPLENormals[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
static const float TEMPLETexCoords[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};

#endif

If a use a C++ source file, may I need to define a class?

UPDATE:
I think the problem is:
Every source file in which those headers are included (even indirectly) will generate its own definition for those static arrays. There’s no guarantee that the compiler/linker will optimize them into a single definition, even in source files where they’re unused. In fact, in many cases the compiler cannot optimize them away. This could result in your static data consuming a lot of disk space, and possibly runtime memory as well.

Thank you.

  • 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-05-19T02:17:56+00:00Added an answer on May 19, 2026 at 2:17 am

    static and extern at the same time makes no sense. static at file scope makes the array inaccessible by other files, while extern tells the compiler that your array is defined somewhere else.

    You can do what 321008 suggests, except that you don’t declare your arrays static which is illegal C and C++. That gives you three global variables that you can use wherever the header file is included.

    For example like this:

    // .h file:
    
    extern const float TEMPLEVertices[];
    
    // .cpp (or .c) file:
    
    const float TEMPLEVertices[] = { 1.0, 2.0, 5.6 /* or whatever*/ };
    

    Or you can do what fortran suggests, but that will give you file-scope access only, not global variables.

    You do not in any way have to define a class if you use a C++ source file. Unlike Java, C++ does not force you into an object oriented design (whether or not that is good can probably be discussed, but anyway).

    EDIT: As for your question update, that is because you define them as static. If you only want global variables, you should not do that, but instead keep one single definition (const float) and reference it with extern, as per my example above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this is a simple question for PHP guys but I don't
I know this question isn't directly programming related, but since I want to be
I know this specific question has been asked before , but I am not
There are many things that all programmers should know, but I am particularly interested
I don't know if this is too specific a question, if that is possible,
I know this question has been asked before, but I ran into a problem.
I know this question has been asked a bit before. But looking around I
I know this question might sound a little cheesy but this is the first
I know this is similar to this question , but I'm using SQL Server
I know this is a frequently asked question, however none of the solutions that

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.