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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:22+00:00 2026-05-18T02:31:22+00:00

Alright, I wanna know why this code is working, I just realized that I

  • 0

Alright, I wanna know why this code is working, I just realized that I have two variables with the same name within the same scope.

I’m using g++ (gcc 4.4).

for(int k = 0 ; k < n ; k++)
    {
        while(true)
        {
            i = Tools::randomInt(0, n);
            bool exists = false;

            for(int k = 0 ; k < p_new_solution_size ; k++)
                if( i == p_new_solution[k] )
                {
                    exists = true;
                    break;
                }
            if(!exists)
                break;
        }

        p_new_solution[p_new_solution_size] = i;
        p_new_solution_size++;
    }
  • 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-18T02:31:22+00:00Added an answer on May 18, 2026 at 2:31 am

    Alright, I wanna know why this code is working, I just realized that I have two variables with the same name within the same scope.

    You seem confused about scopes. They’re not “within the same” scope… the for loop’s k has it’s own nested/inner scope. More importantly, to see why the language allows it, consider:

    #define DO_SOMETHING \
        do { for (int i = 1; i <= 2; ++i) std::cout << i << '\n'; } while (false)
    
    void f()
    {
        for (int i = 1; i <= 10; ++i)
            DO_SOMETHING();
    }
    

    Here, the text substituted by the macro “DO_SOMETHING” gets evaluated in the same scope as i. If you’re writing DO_SOMETHING, you may need its expansion to store something in a variable, and settle on the identifier i – obviously you have no way of knowing if it’ll already exist in the calling context. You could try to pick something more obscure, but you’d have people using such convoluted variable names that their code maintainability suffered, and regardless sooner or later there would be a clash. So, the language just lets the inner scopes introduce variables with the same name: the innermost match is used until its scope exits.

    Even when you’re not dealing with macros, it’s a pain to have to stop and think about whether some outer scope is already using the same name. If you know you just want a quick operation you can pop it an indepedent (nested) scope without considering that larger context (as long as you don’t have code in there that actually wants to use the outer-scope variable: if you do then you can sometimes specify it explicitly (if it’s scoped by namespaces and classes, but if it’s in a function body you do end up needing to change your own loop variable’s name (or create a reference or something to it before introducing your same-named variable)).

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

Sidebar

Related Questions

Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright, currently I have my SWF hitting a php file that will go and
Alright, so here's the dealio: I'm working on a Ruby app that'll take data
#post The names of my variables are not imporant! This code will be deleted
Alright, I don't know if anyone has tried to do this yet, however. I
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright, I'm trying to read a comma delimited file and then put that into
Alright, I know how the fieldset / legend works out in HTML. Say you

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.