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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:35:08+00:00 2026-06-14T16:35:08+00:00

A static variable inside a function is only allocated once during the lifetime of

  • 0

A static variable inside a function is only allocated once during the lifetime of the program.

So if I have a function like:

void f(int n) {

  static int *a = new int[n];

}

and I first call

f(1)

and then

f(3)

how big will the array a be after the second call?

  • 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-14T16:35:10+00:00Added an answer on June 14, 2026 at 4:35 pm

    static local variables will be initialized when the control flow reaches the declaration for the first time. In this case, since the first time you used 1 as the n parameter, you’ll be allocating size for one int.

    Doing this kind of stuff is a bad idea. You should just use a local, non-static, std::vector or some other higher level container.

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

Sidebar

Related Questions

Suppose I have a static variable declared inside a function in C. If I
I have one static variable declared inside a function, which is not initialized to
I've read that static variables are used inside function when one doesn't want the
I want to declare static(or not static) variable inside Enum. I need this because
I cannot get/set a static variable inside a method. How can I do it?
in C, if I declare a static variable inside a local strucutre, where does
I have a static variable partner in the class. And I want to set
I have a static variable that I use as a counter and a non-static
Is there a difference between declaring a static variable outside of a function and
I am trying to change a static variable inside a DLL, so when 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.