-1073741571 (0xC00000FD) error in code block c++

-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;
}
c++
asked on Stack Overflow Dec 16, 2020 by lalala

2 Answers

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'?

answered on Stack Overflow Dec 16, 2020 by Deepak Tatyaji Ahire • edited Dec 16, 2020 by Deepak Tatyaji Ahire
-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;
}

answered on Stack Overflow Dec 16, 2020 by foragerDev • edited Dec 16, 2020 by foragerDev

User contributions licensed under CC BY-SA 3.0