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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:24:20+00:00 2026-06-15T12:24:20+00:00

I have written a short (450 lines) program that calculates some situations that can

  • 0

I have written a short (450 lines) program that calculates some situations that can actually occur in a Connect Four game. The program tries to build the game tree and it recognizes if a situation already occured (also I skip branches if only the symmetric version happened).

I count the number of times that I come to an situation that already happened with int alreadyCounter. But I’ve noticed something strange when executing:

Adding

printf("abcdefghijklm"); 

changes the result of my alreadyCounter!

I don’t use other cores/ processes / threads. I compile the C-program with

gcc -g -W -Wall -Werror -std=c99 -o connectfour

The full source is at GitHub. (I’m sorry, I couldn’t make the code significantly shorter)

Take a look at connectfour.c and connectfour-strange.c.
They only differ in line 386.

But they give – apart from many “abcdefghijklm” – different results:

connectfour:

[...]abcdefghijklmabcdefghijklm########################Finish:
Maximum of 20000 reached
alreadyCounter: 1547
mirroredCounter: 0

connectfour-strange:

[...]
########################Finish:
Maximum of 20000 reached
alreadyCounter: 1566
mirroredCounter: 0

Why do I get different results on a single core / process / thread program when I only add some constant output?

  • 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-06-15T12:24:21+00:00Added an answer on June 15, 2026 at 12:24 pm

    I don’t know if that is the cause for your observations, but in lines 334ff,

    char mirrored[BOARD_WIDTH][BOARD_HEIGHT];
    for (int x = 0; x<BOARD_WIDTH; x++) {
        for (int y=0; y<BOARD_HEIGHT; y++) {
            mirrored[x+1-BOARD_WIDTH][y] = board[x][y];
        }
    }
    

    you write outside the allocated memory (your row index goes from -(BOARD_WIDTH) to -1), thus invoking undefined behaviour.

    Also, that doesn’t mirror the board. You very probably meant

    mirrored[BOARD_WIDTH - 1 - x][y] = board[x][y];
    

    there. Apart from that, I see no obvious candidates like out-of-bounds writes.

    In isBoardFinished (line 43ff), you stop checking before you have reached row/column 0:

    while (xTemp > 0)
    

    (ditto for y in the top-down check, and both in the diagonals checks). That should be x >= 0 to use the full board. But this could not overwrite alreadyCounter, still it may be useful.

    But compiling the code with warnings turned on and optimisations shows the probable cause:

    connect-four.c: In function ‘getFirstIndex’:
    connect-four.c:202:19: warning: ‘index’ may be used uninitialized in this function [-Wuninitialized]
    connect-four.c: In function ‘getNewIndex’:
    connect-four.c:202:19: warning: ‘index’ may be used uninitialized in this function [-Wuninitialized]
    connect-four.c:199:18: note: ‘index’ was declared here
    connect-four.c: In function ‘getMyIndex’:
    connect-four.c:202:19: warning: ‘index’ may be used uninitialized in this function [-Wuninitialized]
    connect-four.c:199:18: note: ‘index’ was declared here
    connect-four.c: In function ‘makeTurns’:
    connect-four.c:202:19: warning: ‘index’ may be used uninitialized in this function [-Wuninitialized]
    connect-four.c:199:18: note: ‘index’ was declared here
    connect-four.c:202:19: warning: ‘index’ may be used uninitialized in this function [-Wuninitialized]
    connect-four.c:199:18: note: ‘index’ was declared here
    

    So the warning and accompanying note (it appears multiple times with different function names but the same location due to inlining) tell us that it should be

    unsigned int getFirstIndex(char board[BOARD_WIDTH][BOARD_HEIGHT]) {
        unsigned int index = 0;
        //               ^^^^^^^
    

    to get deterministic results from invoking getFirstIndex with identical boards. Since you take the modulus with respect to MAXIMUM_SITUATIONS before returning an index, the returned index shouldn’t be out of bounds, however, so no crash is to be expected as a consequence of that. But when you print out something, that may cause allocation on the stack (need not, the arguments and return address could be all passed in registers), and if it does, that can influence the bit-pattern at the place where index is allocated the next time getFirstIndex is called. That would change the returned value, and you look in the wrong slot in database, so you miss a previous occurrence of an identical board, thus you get fewer duplicates than if the value getFirstIndex produces only depends on the passed-in board (and not also on what bits happened to occupy a particular stack location).

    Note that it is – with my version of gcc – necessary to have both, warnings tuned on and optimisations, to get the warning. My clang doesn’t warn about it even with warnings and optimisations turned to the highest level. Other versions of gcc and clang, and other compilers may have different success in identifying the problem.

    With index initialised to 0 in getFirstIndex, I get a consistent

    ########################Finish:
    Maximum of 20000 reached
    alreadyCounter: 4412
    

    independent from whether the string is printed and the optimisation level. Without the initialisation, the value of alreadyCounter depends on both factors.

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

Sidebar

Related Questions

I have written a short program in a ruby file that runs correctly on
I have written a short python script that opens Google music in web view
I have written this short script (which I've stripped away some minor detail for
I have written a short jquery method that on the keypress event of a
I have written a short String reverse program in C++. I decided to write
I have written this short html and javascript code. You can access the code
I have written a small example C++ program, using boost::thread. Since it's 215 lines,
I have written some code that used strings to represent time such as 0620,
I have written a short Scala program to read a large file, process it
Intro: I have written some short excel macros (tested, they work fine) and want

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.