简体   繁体   English

std :: string(empty char *)错误:basic_string :: _ S_construct null无效错误

[英]std::string(empty char*) error: basic_string::_S_construct null not valid error

On Debian, I need to read an environment variable, otherwise return an empty string. 在Debian上,我需要读取一个环境变量,否则返回一个空字符串。 Here is my code: 这是我的代码:

std::string getEnv(std::string var)
{
    char* val = ::getenv(var.c_str());
    std::string retVal(val);
    return retVal;
}

When the environment variable is not found, I'm getting the following error: 找不到环境变量时,出现以下错误:

basic_string::_S_construct null not valid error

After debugging, the error code happens on std::string retVal(val) when I try to load an empty val to it. 调试后,当我尝试向其加载空值时,错误代码会在std::string retVal(val)发生。

I tought loading an string from an empty char * would work, but seens not to. 我可以从一个空的char *加载一个字符串,但是可以,但是似乎不行。

How can I fix the above code so that I can make it work in case the enviroment variable does not exists ? 如何修复以上代码,以便在环境变量不存在的情况下可以正常工作?

From man getenv : 来自man getenv

The getenv() function returns a pointer to the value in the environment, or NULL if there is no match. getenv()函数返回指向环境中值的指针;如果不匹配,则返回NULL

So you need to account for the case where val == NULL . 因此,您需要考虑val == NULL的情况。

std::string getEnv(std::string var)
{
    const char* val = ::getenv(var.c_str());
    if (val == nullptr)
        return std::string("");
    return std::string(val);
}

would be a possible solution (return empty string if var is not found). 可能的解决方案(如果未找到var,则返回空字符串)。

you can use string::data() function to get the address of underling buffer pointer. 您可以使用string :: data()函数获取下缓冲指针的地址。

try var.c_str() or var.data(). 尝试var.c_str()或var.data()。

  std::string getEnv(std::string var)
    {
        char* val = ::getenv(var.data());
        std::string retVal(val ? val : "");
        return retVal;
    }

For me its working. 对我来说,它的工作。

val ? val : "" val ? val : "" to avoid passing NULL to std::string() constructor val ? val : ""避免将NULL传递给std::string()构造函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在函数参数/套接字中发送std :: string时basic_string :: _ S_construct null无效错误 - basic_string::_S_construct null not valid error while sending std::string in function argument/socket 字符串 std::logic_error: basic_string::_S_construct null 无效 - String std::logic_error: basic_string::_S_construct null not valid std :: logic_error:basic_string :: _ s_construct null无效 - std::logic_error: basic_string::_s_construct null not valid 抛出'std :: logic_error'what()实例后调用终止终止:basic_string :: __ S_construct null无效 - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid basic_string :: _ S_construct null无效 - basic_string::_S_construct null not valid openCV basic_string :: _ S_construct null无效 - openCV basic_string::_S_construct null not valid 野牛:what():basic_string :: _ S_construct null无效 - Bison: what(): basic_string::_S_construct null not valid 如何避免错误:在抛出'std :: logic_error'的实例后调用terminate what():basic_string :: _ S_construct null无效 - How to avoid the error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 为什么在抛出'std :: logic_error'what()的实例后显示终止调用:basic_string :: _S_construct null无效 - Why does it show terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 我运行程序后,C ++给我basic_string :: _ S_construct null无效错误 - C++ giving me basic_string::_S_construct null not valid error after I run my program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM