-1

I need to generate a large number of random alphanumeric string, it is okay with the size below 10000, but when I tried to create the size 100,000, it returned the error -1073741571 (0xC00000FD) and my code won't run. Please tell me what is the error and how to solve it.

Below are my codes:

#include <iostream>
#include <ctime>
#include <unistd.h>

using namespace std;

string gen_random(const int len) {

    string tmp_s;
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    srand(rand() ^ (unsigned) time(NULL) * getpid());

    tmp_s.reserve(len);

    for (int i = 0; i < len; ++i)
        tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];

    return tmp_s;

}

bool repeat(string target, string arr[],int arr_size)
{
    for (int i=0; i<arr_size; i++)
    {
        if (arr[i].compare(target) == 0)
            return true;
    }
    return false;
}

int main(int argc, char *argv[])
{

    const int n = 100000;
    string data_set[n];

    for (int i=0; i<n; i++)
    {
        string s = gen_random(4) + "." + gen_random(5) + "@" + gen_random(5);

        if (!repeat(s,data_set,n))
          data_set[i] = s;
        else
            i--;
    }

    for (int i=0; i<n; i++)
        cout << data_set[i] << endl;

    return 0;
}
lalala
  • 13
  • 3
  • 1
    0xC00000FD == STATUS_STACK_OVERFLOW [source](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55?redirectedfrom=MSDN) – Jonas Kibildis Dec 16 '20 at 11:12
  • *I need to generate a large number of random alphanumeric string* -- If you are willing to accept code that has already been written, see the StringFuzzer [here](https://stackoverflow.com/questions/63046559/code-submission-on-spoj-gives-runtime-error-sigabrt/63048464#63048464). Then apply it [like this](http://coliru.stacked-crooked.com/a/7d75d06d420da193) – PaulMcKenzie Dec 16 '20 at 16:13

2 Answers2

0

The stack size of the single threaded program is determined when the thread is created.

The code contains contains array data_set, and n number of strings, each with length of 16 characters.

Assuming it takes 1 bytes for a character in C++, the approximate amount of memory required is = n*16 = (10^5 * 16) bytes.

All the arrays have a local scope, that means they will occupy the stack.

Now, this amount of memory is generally available for a process, which was not, in your case.

Try to increase the stack size of a process and try executing the code.

PS: OP changed the question and mentioned about CodeBlocks.

I think you are using Windows OS and there is a limit (varies with OS) on the size of variables that can be stored on the stack.

This code works fine on linux with n=10^5.

Also, read this article: Why should C++ programmers minimize use of 'new'?

Deepak Tatyaji Ahire
  • 3,469
  • 2
  • 6
  • 22
-1

The error is because of these lines:

    const int n = 100000;
    string data_set[n];

because you are storing this on the function stack, so that stack of the program is not big enough that can hold this that causes bug. If you decrease the size of n it compiles and runs fine.

to avoid this better use heap allocation with new.

The best approach will be is to use std::vector<std::string> Here is the modification to the code here:

#include <iostream>
#include <ctime>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

string gen_random(const int len) {

    string tmp_s;
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    srand(rand() ^ (unsigned) time(NULL) * getpid());

    tmp_s.reserve(len);

    for (int i = 0; i < len; ++i)
        tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];

    return tmp_s;

}

bool repeat(string target, std::vector<std::string> arr)
{
    for (int i=0; i<arr.size(); i++)
    {
        if (arr[i].compare(target) == 0)
            return true;
    }
    return false;
}

int main(int argc, char *argv[])
{

    const int n = 100000;
    vector<string> data_set(n);

    for (int i=0; i<n; i++)
    {
        string s = gen_random(4) + "." + gen_random(5) + "@" + gen_random(5);

        if (!repeat(s,data_set))
          data_set[i] = s;
        else
            i--;
    }

    for (int i=0; i<n; i++)
        cout << data_set[i] << endl;

    return 0;
}

foragerDev
  • 676
  • 1
  • 6
  • 17