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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:39:40+00:00 2026-05-26T07:39:40+00:00

Is it more memory or possibly computationally efficient to declare variables late? Example: int

  • 0

Is it more memory or possibly computationally efficient to declare variables late?

Example:

int x;
code
..
.
.
. x is able to be used in all this code
.
actually used here
.
end

versus

code
..
.
.
.
int x;
actually used here
.
end

Thanks.

  • 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-26T07:39:41+00:00Added an answer on May 26, 2026 at 7:39 am

    Write whatever logically makes most sense (usually closer to use). The compiler can and will spot things like this and produce code that makes the most sense for your target architecture.

    Your time is far more valuable than trying to second guess the interactions of the compiler and the cache on the processor.


    For example on x86 this program:

    #include <iostream>
    
    int main() {
      for (int j = 0; j < 1000; ++j) {
        std::cout << j << std::endl;
      }
      int i = 999;
      std::cout << i << std::endl;
    }
    

    compared to:

    #include <iostream>
    
    int main() {
      int i = 999;
      for (int j = 0; j < 1000; ++j) {
        std::cout << j << std::endl;
      }
      std::cout << i << std::endl;
    }
    

    compiled with:

    g++ -Wall -Wextra -O4 -S measure.c
    g++ -Wall -Wextra -O4 -S measure2.c
    

    When inspecting the output with diff measure*.s gives:

    <       .file   "measure2.cc"
    ---
    >       .file   "measure.cc"
    

    Even for:

    #include <iostream>
    
    namespace {
      struct foo {
        foo() { }
        ~foo() { }
      };
    }
    
    std::ostream& operator<<(std::ostream& out, const foo&) {
      return out << "foo";
    }
    
    int main() {
      for (int j = 0; j < 1000; ++j) {
        std::cout << j << std::endl;
      }
      foo i;
      std::cout << i << std::endl;
    }
    

    vs

    #include <iostream>
    
    namespace {
      struct foo {
        foo() { }
        ~foo() { }
      };
    }
    
    std::ostream& operator<<(std::ostream& out, const foo&) {
      return out << "foo";
    }
    
    int main() {
      foo i;
      for (int j = 0; j < 1000; ++j) {
        std::cout << j << std::endl;
      }
      std::cout << i << std::endl;
    }
    

    the results of the diff of the assembly produced by g++ -S are still identical except for the filename, because there are no side effects. If there were side effects then that would dictate where you constructed the object – at what point did you want the side effects to occur?

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

Sidebar

Related Questions

In C# which is more memory efficient: Option #1 or Option #2? public void
EDIT To make this post a bit more constructive, and let it possibly help
Is it possible to compare whole memory regions in a single processor cycle? More
Does Java 6 consume more memory than you expect for largish applications? I have
C#: Which uses more memory overhead? A string or a char holding a sequence
I am using ARM926EJS. I am getting 20 % more memory speed in memory
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it
I want to go backwards and learn more about how compilers, processors and memory
This is possibly a candidate for a one-line answer. I would like know it

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.