Thread: ProjectEuler 13: Program behaves abnormally/differently in different environments

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    1

    ProjectEuler 13: Program behaves abnormally/differently in different environments

    Follwing is my program to solve Project Euler problem 13.
    Code:
    #include<stdio.h>
    
    int main()
    {
        char carr[100][13];
        long long int iarr[100], sum=0;
    
        // This shows that windows gcc created 32 bit binary
        // windows msvc creates 64 bit program
        // linux gcc also created 64 bit program
        printf("This is %d bit program.\n", sizeof(void*) * 8);
    
        for(int i=0; i<100; ++i) // This loops should run 100 times but it runs only 97 times
        {
            // Takes the number as character array/string
            scanf("%s", carr[i]);
    
            // Prints input line number and input just taken
            printf("%d: %s -> ", i+1, carr[i]);
    
            // Makes 13th character as null so that sscanf reads only 12 characters
            carr[i][12] = 0;
    
            // Convert number in string to long long int
            sscanf(carr[i], "%lld", iarr+i);
    
            // prints the converted number
            printf("%lld\n", iarr[i]);
    
            // adds the number to sum
            sum += iarr[i];
        }
    
        // This statement doesn't get printed at all on windows gcc
        // On linux/ubuntu gcc shows error: **stack smashing detected** Aborted (core dumped)
        // Runs fine on msvc on windows
        printf("%lld", sum);
    
        return 0;
    }
    
    I have copied that problem input into a .txt file and pass that to program using redirection, i.e.
    on linux: ./a.out < ./input.txt
    on windows: euler.exe < input.txt

    On running the executable produced by windows gcc, it just reads 97 lines out of 100 lines and the program just ends. No errors, nothing, it just ends.
    ProjectEuler 13: Program behaves abnormally/differently in different environments-windows_gcc-jpg

    On linux gcc, it takes all 100 inputs but at last gives the error: **stack smashing detected** Aborted (core dumped)
    ProjectEuler 13: Program behaves abnormally/differently in different environments-linux_gcc-jpg

    On windows msvc, it runs fine and produces the correct output.
    ProjectEuler 13: Program behaves abnormally/differently in different environments-windows_msvc-jpg

    I am completely unable to understand what is causing so different behavior in every case.
    Can anyone help me understanding this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your input lines seem way longer than 12 characters if your screens are anything to go by.

    You should always believe the failure case as being the most useful. Accidental success proves nothing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    104
    I always heard bad things about mvsc, explicitly, that it sometimes adds things behind the scene to your code, perhaps to fix it or make it run fine, without your knowledge. Can't say with certainty cause I've never used it myself. Being Microsoft's if enough for me to shy away from it, merely from a principles perspective.

    You should run your code through a debugger. You can install gdb in linux and use -tui to get a small curses gui on the console. It will help you identify the issue in no time.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    Btw, other than a warning about the wrong format specifier in printf, you should use %lu for unsigned integer instead of %d, the code runs fine on my system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-13-2016, 12:22 AM
  2. BST program terminating abnormally
    By progmateur in forum C++ Programming
    Replies: 7
    Last Post: 01-26-2014, 03:36 PM
  3. Replies: 3
    Last Post: 07-28-2012, 05:48 AM
  4. scanf behaving abnormally
    By greendragons in forum C Programming
    Replies: 2
    Last Post: 06-13-2012, 04:11 PM
  5. 3 linux shell environments
    By SpEkTrE in forum Linux Programming
    Replies: 3
    Last Post: 10-21-2003, 02:26 PM

Tags for this Thread