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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:37:21+00:00 2026-05-13T23:37:21+00:00

int bar = 2; if (bar) { int bar; } Neither gcc or Clang

  • 0
int bar = 2;
if (bar)
{
   int bar;
}

Neither gcc or Clang manages to issue a warning (or error) for this, and the program crashes immediately on launch. Is there a good reason for this? It doesn’t seem like it would be something hard to catch. It’s the basics of block scoping: the nested scope inherits the names of the enclosing block…

Any explanations?

EDIT: It turns out the crash was due to using Clang. I’ve tested many times back and forth, and it seems certain that the combination of the variable redefinition and Clang causes the crash. However, I haven’t been able to reproduce the crash in a test project, so go figure.

The problem turned out to be Objective-C related. As Jonathan Leffler points out doing ´int bar = bar´ in the inner scope initializes the variable from itself, and that’s what causes the problem, when the initialization is done via an Objective-C method call.

The following shows the bug in action:

-(void)crasher
{
   NSNumber* bar = [NSNumber numberWithInt:2];
   if (bar)
   {
      NSString* bar = [self doit:bar];
   }
}

-(NSString*)doit:(NSNumber*)num
{
   NSString* str = [num stringValue];   // This line causes the crash
   return str;
}

Note that doing something similar in pure C does not produce a crash:

int bar = 2;
if (bar)
{
   char buff[10];
   int bar = sprintf(buff, "%d",bar);       
}
  • 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-13T23:37:22+00:00Added an answer on May 13, 2026 at 11:37 pm

    Extending the answer given by Douglas Leeder:

    #include <stdio.h>
    static int xx(int foo)
    {
        int bar = 2;
        if (foo > bar)
        {
            int foo = bar;
            int bar = bar;
            printf("inner: foo = %d, bar = %d\n", foo, bar);
        }
        printf("outer: foo = %d, bar = %d\n", foo, bar);
        return bar;
    }
    int main(void)
    {
        xx(13);
        return(0);
    }
    

    Note that the inner bar is initialized from itself – which gives undefined behaviour. But on MacOS X 10.6.2 (GCC 4.2.1) I get:

    inner: foo = 2, bar = 0
    outer: foo = 13, bar = 2
    

    Variant 1: Stack trampling – A

    Interestingly, I get the same output from this code, with a stack trampling function, regardless of whether I declare i before or after a.

    inner: foo = 2, bar = 20
    outer: foo = 13, bar = 2
    

    Code:

    #include <stdio.h>
    static void modify_stack(void)
    {
        int a[20];
        int i;
        for (i = 0; i < 20; i++)
        {
            a[i] = 0xFFFFFFFF ^ i;
            printf("a[i] = 0x%08X\n", a[i]);
        }
    }
    static int xx(int foo)
    {
        int bar = 2;
        if (foo > bar)
        {
            int foo = bar;
            int bar = bar;
            printf("inner: foo = %d, bar = %d\n", foo, bar);
        }
        printf("outer: foo = %d, bar = %d\n", foo, bar);
        return bar;
    }
    int main(void)
    {
        modify_stack();
        xx(13);
        return(0);
    }
    

    Since the behaviour is undefined, this result is fine.

    Variant 2: Stack trampling – B

    #include <stdio.h>
    static int modify_stack(void)
    {
        int a[20];
        int i;
        for (i = 0; i < 20; i++)
        {
            a[i] = 0xFFFFFFFF ^ i;
            printf("a[i] = 0x%08X\n", a[i]);
        }
        i = a[13];
        return(i);
    }
    static int xx(int foo)
    {
        int bar = 2;
        if (foo > bar)
        {
            int foo = bar;
            int bar = bar;
            printf("inner: foo = %d, bar = %d\n", foo, bar);
        }
        printf("outer: foo = %d, bar = %d\n", foo, bar);
        return bar;
    }
    int main(void)
    {
        int i = modify_stack();
        xx(13);
        return(i & 0xFF);
    }
    

    Output (apart from data printed in loop):

    inner: foo = 2, bar = -14
    outer: foo = 13, bar = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this program: class Foo { public: unsigned int bar ()
When you have an array like this: int foo[3][2][2]; and you make: int *bar
I have a type like this: class Foo { public: int bar[3]; /*Possibly some
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Specifically, is the following legal C++? class A{}; void foo(A*); void bar(const A&); int
int x = n / 3; // <-- make this faster // for instance
Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {}
Given the following code: public struct Foo { public Foo(int bar, int baz) :
[Serializable] class MyClass { [NonSerialized] int Foo { get; set; } // error [NonSerialized]
class Foo { int Bar; public: int& GetBar() const { return Bar; } }

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.