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.
On linux gcc, it takes all 100 inputs but at last gives the error: **stack smashing detected** Aborted (core dumped)
On windows msvc, it runs fine and produces the correct output.
I am completely unable to understand what is causing so different behavior in every case.
Can anyone help me understanding this?