1

I've made a program that identifies and returns the highest streak of consecutive, repeating digits. When I run the program, I receive the error listed above. I'm not calling any recursive functions, just one function that analyzes the digits. The code is listed below. I'm not including the digits analyzed for obvious reasons, just know it is 10 million digits.

// pi.c
#include <stdio.h>
#define SIZE 10000000

unsigned int findDigits(const char pi[], char *mostRepeatingDigitPtr, unsigned int *repeatLocationPtr);

int main(void)
{
    // 10 million digits of pi
    const char pi[] = "INSERT 10 MILLION CHARACTERS";
    
    unsigned short int repeatStreak;
    
    char mostRepeatingDigit;
    
    unsigned int repeatLocation;
    
    // pass to findDigits()
    repeatStreak = findDigits(pi, &mostRepeatingDigit, &repeatLocation);
    
    printf("The largest amount of repeating digits was: %hu. The repeated digit was: %c. The position on the array was %u.", repeatStreak, mostRepeatingDigit, repeatLocation);
    
    return 0;
}

unsigned int findDigits(const char pi[], char *mostRepeatingDigitPtr, unsigned int *repeatLocationPtr)
{
    char lastDigit = '3';
    
    unsigned short int repeatStreak = 0;
    
    unsigned short int highestRepeat = 0;
    
    unsigned int repeatLocation = 0;
    
    // loop through 10 million digits
    for (unsigned int i = 0; i < SIZE; ++i) {
        if (pi[i] == lastDigit) {           
            ++repeatStreak;
            
            repeatLocation = i - 1;
        }
        else {          
            if (repeatStreak > highestRepeat) {
                highestRepeat = repeatStreak;
                
                *mostRepeatingDigitPtr = lastDigit;
                
                *repeatLocationPtr = i - repeatStreak;
            }
            
            lastDigit = pi[i];
            
            repeatStreak = 0;
        }
    }
    
    // return highest streak
    return highestRepeat + 1;
}

Thanks in advance.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Feb 12 '21 at 01:23
  • The program returns the error right after the main function. –  Feb 12 '21 at 01:27
  • Does the debugger show the line `return 0;` in `main` being executed? Or is that line not reached? – Andreas Wenzel Feb 12 '21 at 01:31
  • That line is not reached. I added a puts statement at the very start of main, and it still returned the error. –  Feb 12 '21 at 01:36
  • Also, some things that might be relevant: I'm running Windows 10 Pro 64-bit and I have 16GB of RAM. –  Feb 12 '21 at 01:37
  • If you put an `fflush(stdout);` statement immediately after the `puts` statement, does is still not output the `puts` statement? – Andreas Wenzel Feb 12 '21 at 01:37
  • No still doesn't work. –  Feb 12 '21 at 01:39
  • Ok, that probably means that the end of `main` is never reached. Now the task is to find out how far the program gets before it exits. Does the program reach the start of the `for` loop? Does it get past the `for` loop? Or does it exit inside the `for` loop? You can answer these questions by setting breakpoints in a debugger. Instead of using a debugger, you can also find out by using `puts`or `printf` statements, but in that case, you should always call `fflush(stdout);` afterwards, otherwise you may not see the output if your program crashes while the output buffer has not been emptied yet. – Andreas Wenzel Feb 12 '21 at 01:44
  • No, I'm telling you the program gets literally nothing done. It returns an error at the START of the main function. –  Feb 12 '21 at 01:55
  • Does the problem go away if you change `const char pi[] =` to `const char *pi =`? If this fixes the problem, then see my answer for the reason why it does. If it does not fix the problem, then my answer is wrong and the problem is something else. – Andreas Wenzel Feb 12 '21 at 01:59

1 Answers1

1

The most likely reason for your program crashing is a stack overflow.

On Microsoft Windows, the maximum stack size is set to about 1 megabyte, by default. This can be changed in the linker settings.

By putting 10 million characters on the stack, you are exceeding this limit.

A simple solution is to change the line

const char pi[] = "INSERT 10 MILLION CHARACTERS";

to

const char *pi = "INSERT 10 MILLION CHARACTERS";

so that the 10 million characters are no longer stored directly on the stack. Instead, you will only have a single pointer on the stack, which points to the string literal in read-only memory.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39