Babaji
Babaji

Reputation: 408

Error in using getenv() to obtain number of threads

I am using the function getenv() to obtain the number of threads in the following manner:

char* var;
var = getenv("OMP_NUM_THREADS");

I get the following error:

'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable 
deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

I tried using _dupenv_s as suggested by the compiler and I got the following errors in the same line:

Error 1: argument of type "const char *" is incompatible with parameter of type "char **"

Error 2: too few arguments in function call

I am running this code on Microsoft Visual Studio 2019. While the complete code is irrelevant, here is a link to the same for reference:

https://pardiso-project.org/manual/pardiso_unsym.cpp

A small reproducible part of the code:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(){

using namespace std;

int      iparm[64];
char* var;

    var = getenv_s("OMP_NUM_THREADS");
    if (var != NULL)
        sscanf(var, "%d", &num_procs);
    else {
        printf("Set environment OMP_NUM_THREADS to 1");
        exit(1);
    }
    iparm[2] = num_procs;

    return 0;
}

I tried to use _dupenv_s, and rewrote the getenv line in the following manner. Please tell me if it is the correct way to go about:

char* var;
size_t sz = 10
_dupenv_s(&var, &sz, "OMP_NUM_THREADS");

Upvotes: 2

Views: 445

Answers (0)

Related Questions