2

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");
Babaji
  • 398
  • 1
  • 4
  • 18
  • what exactly is your question? For the getenv warning (it is not an error) see here: https://stackoverflow.com/questions/48568707/getenv-this-function-or-variable-may-be-unsafe-really . Also note that the warning just says "it *may* be unsafe". If you know what you are doing there is no problem – 463035818_is_not_an_ai Apr 04 '20 at 10:06
  • 1
    Please show a [mre] with the code that isn't working – Alan Birtles Apr 04 '20 at 10:06
  • @idclev463035818 These are being shown in Microsoft Visual Studio as errors and not as warnings. As such, the compilation remains incomplete. As for the the link that you have shown, I have tried both the alternate solutions. _dupenv_s gives me the problem as mentioned above. getenv_s gives the error: no instance of overloaded function "getenv_s" matches the argument list. – Babaji Apr 04 '20 at 10:14
  • the complete error should refer to `-Werror` or similar flag that makes the compiler treat warnings as errrors. The message also contains instructions on how to silence the warning, but make sure to understand what causes the warning and that it wont affect you before – 463035818_is_not_an_ai Apr 04 '20 at 10:17
  • @AlanBirtles I have added a small reproducible part of the code for reference. – Babaji Apr 04 '20 at 10:31
  • 3
    did you read the [documentation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-s-wgetenv-s?view=vs-2019) for `getenv_s`? you're passing the wrong number of parameters – Alan Birtles Apr 04 '20 at 11:29
  • @AlanBirtles I have added a code at the very end of my post describing the use of _dupenv_s. Please tell me if the correct way to go about. My knowledge of buffers is very minimal. – Babaji Apr 06 '20 at 07:02
  • 1
    depends what `var` and `sz` are and how they're initialised – Alan Birtles Apr 06 '20 at 07:05
  • @AlanBirtles My bad. I added it. `var` is the variable that I want to possess the value of OMP_NUM_THREADS. I am unsure of what the second parameter of the function aims to do. Therefore I assigned a random number of 10. This may be wrong. Could you also explain to me what the second parameter is exactly for? According to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/dupenv-s-wdupenv-s?view=vs-2019 it is the size of the buffer. Which is where I am confused. – Babaji Apr 06 '20 at 07:10
  • 1
    the value of `sz` doesn't matter its set by `_dupenv_s` – Alan Birtles Apr 06 '20 at 08:29
  • 1
    If (at the level above) you are really after the number of OpenMP threads which can be used, all of this getenv stuff is a waste of time anyway! There is no need for OMP_NUM_THREADS to be set. To find out the number of threads which can be used, at the outer level of your code (before going parallel), call omp_get_max_threads() https://www.openmp.org/spec-html/5.0/openmpsu112.html – Jim Cownie Apr 07 '20 at 08:18

0 Answers0