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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:24:00+00:00 2026-05-24T05:24:00+00:00

If I have a function which only exists for a short amount of time,

  • 0

If I have a function which only exists for a short amount of time, does making the list of colors a constant make a difference?

string getrandcolor(){
    const string colors[5] = {"red", "blue", "green", "yellow", "purple"};
    return colors[rand() % 5];
}

Note: The actual list of colors contains hundreds of colors not just the small sample I have shown, not sure if this makes a difference either.

  • 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-24T05:24:01+00:00Added an answer on May 24, 2026 at 5:24 am

    It prevents you from accidentally overwriting variables that you didn’t mean to change. “Oops!”-protection is probably const‘s most important function.

    Hypothetically a compiler could divine some kind of optimization from knowing a variable isn’t supposed to change, but my testing has never found one that actually does something meaningful in the situation you describe.

    const static does have an important difference in your particular code sample. Because your colors[] array is local, it must be constructed afresh each time the getrandcolor() function is called. That means the string constructor gets run five times each time you call getrandcolor(), which is very wasteful. const static means that the data is constructed only once — the first time the function is run — and put into a shared memory space.

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

Sidebar

Related Questions

I have a function which is constant to its argument, for example let is_prime
I have a windowless timer (no WM_TIMER) which fires a callback function only once
I have been wondering about a function in Java, which I hope exists. I
I have a javascript library which does returns a list of results to a
I intended to create a class which only have static members and static functions.
I have a function which parses one string into two strings. In C# I
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through

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.