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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:08:26+00:00 2026-05-12T09:08:26+00:00

What is the difference (memory wise) bewteen: for(int x=0;x<100;x++) { int y = 1+x;

  • 0

What is the difference (memory wise) bewteen:

for(int x=0;x<100;x++)
{
  int y = 1+x;
}


and


int y = 0;
for(int x=0;x<100;x++)
{
  y = 1+x;
}

I've always wondered if they are the same or the first is a waste of memory?...

  • 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-12T09:08:26+00:00Added an answer on May 12, 2026 at 9:08 am

    Memory-wise, there is no difference. y is on the stack, wherever it’s declared within the method. Here the only difference is the scope of y: in the second case, it is restricted to the body of the for loop; in the first, it isn’t. This is purely at the language-level: again, y is allocated in exactly the same way, that is, on the stack.

    Just to make this point perfectly clear, here’s a code example:

    void method1() {
        for (;;) {
            int a = 10;
        }
    }
    
    void method2() {
        int a;
        for (;;) {
            a = 10;
        }
    }
    

    Here is the assembler generated in debug mode in both cases :

    # method1() 
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        eax  
    00000004  cmp         dword ptr ds:[00662E14h],0 
    0000000b  je          00000012 
    0000000d  call        5D9FE081 
    00000012  xor         edx,edx 
    00000014  mov         dword ptr [ebp-4],edx 
    00000017  mov         dword ptr [ebp-4],0Ah 
    0000001e  nop              
    0000001f  jmp         00000017 
    
    # method2() 
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        eax  
    00000004  cmp         dword ptr ds:[002B2E14h],0 
    0000000b  je          00000012 
    0000000d  call        5ED1E089 
    00000012  xor         edx,edx 
    00000014  mov         dword ptr [ebp-4],edx 
    00000017  mov         dword ptr [ebp-4],0Ah 
    0000001e  nop              
    0000001f  jmp         00000017 
    

    Even without knowing anything about assembly, you can see that both methods have exactly the same instructions. In other words, at the point where a is declared, nothing happens.

    There is an important difference however if you are using any type that has a constructor, say, an std::vector: at the point where it is declared, the constructor is called, so if you declare it within a loop, it will be reconstructed each time through the loop. For example:

    for (/* index */) {
        std::vector<int> a; // invokes the constructor of std::vector<int> everytime
    } // destructor called each time the object goes out of scope
    
    std::vector<int> a; // constructor only called once
    for (/* index */) {
    
    }
    

    The situation gets worse if you are using new: these two pieces of code behave very differently:

    for (/* index */) {
        char *a = new char[100]; // allocates 100 additional bytes every time !
    } // must remember to delete[] a in the loop, otherwise it's a memory leak !
    
    //////
    
    char *a = new char[100]; // only one allocation
    for (/* index */) {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between aligned and unaligned memory access? I work on an
With regards to SQL and queries, whats the difference between an in-memory table, temp
Hi all is the memory allocated for different types of variable say float, int
What is the difference between memory indirect call and register indirect call? I'm trying
Is there any difference in speed/memory usage for these two equivalent expressions: Regex.IsMatch(Message, 1000)
Possible Duplicate: String concatenation vs String Builder. Performance Any difference (performance and memory usage)
Does the order and/or size of shader in/out variables make any difference in memory
Does anybody know the memory difference between these two? Or how one would figure
What is the difference regarding memory allocation and efficiency between using a struct with
what are the differences between using these two algorithms. I've always wondered how I

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.