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

  • Home
  • SEARCH
  • 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 6667937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:59:10+00:00 2026-05-26T02:59:10+00:00

For example there is a function doing something. How should i declare and define

  • 0

For example there is a function doing something. How should i declare and define an array inside the function i’d like to be allocated/initialized only once?

void someclass::somefunction(/*parameters here*/)
{
  static const my_array[4] = {1,2,3,4}; // #1
  /*or just*/
  const my_array[4] = {1,2,3,4}; // #2
}

As far as i know in the case #1 “my_array” will be allocated in data segment and initialized once at the first time of “somefunction” calling. But a colleague of mine made an supposition that the case #2 works in the same manner and there is no need to write “static” key-word.

So i’d like to ask does the standart say something about cases #1 & #2 and, if it does, what exactly? How should i define such type of arrays to be sured that it will be allocated/initialized only once?

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-26T02:59:10+00:00Added an answer on May 26, 2026 at 2:59 am

    Declare and define it the most logical and clear way, and only if profiling shows it to be a bottleneck would I suggest changing the code.

    In this case some compilers may very well generate the same code. Others may generate different code due to the slightly different initialization semantics (for example in some cases g++ protects initialization of statics with a mutex lock).

    In fact the only way to know for sure for your particular compiler is to look at the disassembly.

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

Sidebar

Related Questions

Is there php function to remove the space inside the string? for example: $abcd=this
Is there any javascript function that can encrypt data: For example i want to
is there any example on using java method to call rhino-javascript function and return
Is there an equivalent of the Oracle NVL function in SAS? For example, if
I will explain problem with an example: There is two table in my database,
At http://docs.jquery.com/UI/Tabs#Example there is a short HTML example of how to use JQuery UI
On the Groovy example page there is an example of how to use Groovy
I need to explain this by example: Is there a best practice or preference
The example below may not be problematic as is, but it should be enough
I have a function, in which there is a loop which calls up the

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.