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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:43:44+00:00 2026-05-19T00:43:44+00:00

One of my friends sent this code to me, saying it doesn’t work as

  • 0

One of my friends sent this code to me, saying it doesn’t work as expected:

#include<stdio.h>

void main()
{
    int a [10] ={23, 100, 20, 30, 25, 45, 40, 55, 43, 42};
    int sizeOfInput = sizeof(a)/sizeof(int);
    int b, outer, inner, c;
    printf("Size is : %d \n", sizeOfInput);

    printf("Values before bubble sort are  : \n");
    for ( b = 0; b &lt; sizeOfInput; b++)
        printf("%d\n", a[b]);

    printf("End of values before bubble sort... \n");

    for ( outer = sizeOfInput; outer > 0; outer-- )
    {
        for (  inner = 0 ; inner < outer ; inner++)
        {
        printf ( "Comparing positions: %d and %d\n",inner,inner+1);
        if ( a[inner] > a[inner + 1] )
        {
                int tmp = a[inner];
                a[inner] = a [inner+1];
            a[inner+1] = tmp;
            }
        }
        printf ( "Bubble sort total array size after inner loop is %d :\n",sizeOfInput);
        printf ( "Bubble sort sizeOfInput after inner loop is %d :\n",sizeOfInput);
    }
    printf ( "Bubble sort total array size at the end is %d :\n",sizeOfInput);
    for ( c = 0 ; c < sizeOfInput; c++)
        printf("Element: %d\n", a[c]);
}

I am using Micosoft Visual Studio Command Line Tool for compiling this on a Windows XP machine.
cl /EHsc bubblesort01.c

My friend gets the correct output on a dinosaur machine (code is compiled using TCC there).

My output is unexpected. The array mysteriously grows in size, in between.

If you change the code so that the variable sizeOfInput is changed to sizeOfInputt, it gives the expected results!

A search done at Microsoft Visual C++ Developer Center doesn’t give any results for “sizeOfInput”.

I am not a C/C++ expert, and am curious to find out why this happens – any C/C++ experts who can shed some light on this?
Unrelated note: I seriously thought of rewriting the whole code to use quicksort or merge sort before posting it here. But, after all, it is not Stooge sort…

Edit: I know the code is not correct (it reads beyond the last element), but I am curious why the variable name makes a difference.

  • 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-19T00:43:45+00:00Added an answer on May 19, 2026 at 12:43 am

    Like interjay’s answer mentioned, once you get into undefined behavior all bets are off. However, when you said that merely renaming the variable changed the behavior of the program, I got curious about what was going on (undefined or not).

    First, I didn’t believe that renaming the variable would change the compiler’s output (all other things being equal), but sure enough – when I tried it, I was surprised to see exactly what you described.

    So I had the compiler dump the assembly for the code it was generating for each version of the source file, and ran a comparison. Here’s what I found in the compilers description of how it was laying out the local variables:

        ***** test.sizeOfInput.cod
        _c$ = -56                    ; size = 4
        _b$ = -52                    ; size = 4
        _inner$ = -48                ; size = 4
        _a$ = -44                    ; size = 40
    >>> _sizeOfInput$ = -4           ; size = 4
        _main   PROC
        ***** test.sizeOfInputt.cod
        _c$ = -56                    ; size = 4
    >>> _sizeOfInputt$ = -52         ; size = 4
        _b$ = -48                    ; size = 4
        _inner$ = -44                ; size = 4
        _a$ = -40                    ; size = 40
        _main   PROC
        *****
    

    What you’ll notice is that when the variable is named sizeOfInput, he compiler places it at a higher address than array a (just past the end of the array), and when the variable is named sizeOfInputt it places it at a lower address than array a instead of just past the end of the array. That means that in the build that has the variable named sizeOfInput, the undefined behavior that occurs when you modify a[10] is changing the value of sizeOfInput. In the build that uses the name sizeOfInputt, since that variable isn’t at the end of the array, the write to a[10] trashes something else.

    As to why the compiler would lay out the variables differently when the name of one changes in an apparently insignificant way – I have no idea.

    But this is a good example of why you shouldn’t count on the layout of local variables (or pretty much any variables, though you can count on the layout order of struct elements), and why when it comes to undefined behavior, “it works on my machine” doesn’t cut it as proof that something works.

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

Sidebar

Related Questions

My Friends, I spent quite some time on this one... but cannot yet figure
i got this code that keep on returning undefined msg instead of the expected
One of my friends raised this problem to me. I was stuck because I
This is a debate I was having with one of my friends: What would
I had an argument with one of my friends who is an architect at
I've set up a self-referencing relationship model for users to be friends with one
Does Java have a built-in Antivirus? One of my friends told me there is
In one class there is a list with all friends and their IDs. Now
hello friends i want to use toggle on <li> when one <li> is open
Problem One friend suggested an interesting problem. Given the following code: public class OuterClass

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.