悬空指针/对int和char *常量的引用

时间:2019-04-22 16:13:32

标签: c++ pointers reference dangling-pointer

我正在阅读Vandevoorde,Josuttis和Gregor撰写的有关C ++模板的书,但不理解它们对悬挂参考的警告。 这是代码:

#include <cstring>

// maximum of two values of any type (call-by-reference)
template<typename T>
T const& max (T const& a, T const& b)
{
  return  b < a ? a : b;
}

// maximum of two C-strings (call-by-value)
char const* max (char const* a, char const* b)
{
  return  std::strcmp(b,a) < 0  ? a : b;
}

// maximum of three values of any type (call-by-reference)
template<typename T>
T const& max (T const& a, T const& b, T const& c)
{
  return max (max(a,b), c);       // error if max(a,b) uses call-by-value
}

int main ()
{
  auto m1 = ::max(7, 42, 68);     // OK

  char const* s1 = "frederic";
  char const* s2 = "anica";
  char const* s3 = "lucas";
  auto m2 = ::max(s1, s2, s3);    // run-time ERROR
}

给定的消息是,对于C字符串,嵌套的max(a,b)创建一个悬空引用,而对于int,则不会。 那么,鉴于char的指针与int的引用相比有什么特别之处,假设两者均实现为指向在max函数外部分配的对象的指针?

2 个答案:

答案 0 :(得分:3)

此:

   char const* max (char const* a, char const* b)

返回一个无名的临时指针值,然后返回:

    return max (max(a,b), c);

返回对其的引用。

答案 1 :(得分:1)

  

...不理解他们对悬挂参考的警告

::max(s1, s2, s3)使用template<typename T> T const& max (T const& a, T const& b, T const& c)返回引用

如果template<typename T> T const& max (T const& a, T const& b, T const& c)的定义更改为:

template<typename T>
T const& max (T const& a, T const& b, T const& c)
{
  return (a > b) ? ((a > c) ? a : c)
                 : ((b > c) ? b : c);
}

没有问题,因为它已经有了引用。

但是在::max(s1, s2, s3)中, T const char*,因此在max (max(a,b), c)中, max char const* max (char const* a, char const* b),它不会返回a引用,因为编译器会将char const* max (char const* a, char const* b)的结果保存在堆栈上的临时变量中,并返回对该临时变量的引用,从而产生消息和相关问题。就像您执行int & f() { int v = 0; return v; }一样,只是临时变量是由编译器本身创建的。

当然,问题template<typename T> T const max (T const& a, T const& b, T const& c)消失了(返回值而不是引用),因为char const* max (char const* a, char const* b)返回的值可以直接返回。

::max(7, 42, 68)的注释没有问题,因为max (max(a,b), c)中的 max template<typename T> T const& max (T const& a, T const& b),它返回一个引用。

在其他情况下,要继续返回引用,您可以将char *的max专门化,例如:

// maximum of two C-strings (call-by-value)
template<>
char const* const & max (char const* const & a, char const* const & b)
{
  return  std::strcmp(b,a) < 0  ? a : b;
}

或将其定义为

char const* const & max (char const* const & a, char const* const & b)
{
  return  std::strcmp(b,a) < 0  ? a : b;
}

返回带有三个参数的版本的引用,而不必使用临时变量并返回对其的引用。

(我个人更喜欢专业化,因为拥有模板版本看起来很自然)


#include <cstring>
#include <iostream>

// maximum of two values of any type (call-by-reference)
template<typename T>
T const& max (T const& a, T const& b)
{
  return  b < a ? a : b;
}

// MODIFIED
// maximum of two C-strings (call-by-value)
template<>
char const* const & max (char const* const & a, char const* const & b)
{
  return  std::strcmp(b,a) < 0  ? a : b;
}

// maximum of three values of any type (call-by-reference)
template<typename T>
T const& max (T const& a, T const& b, T const& c)
{
  return max (max(a,b), c);       // error if max(a,b) uses call-by-value
}

int main ()
{
  auto m1 = ::max(7, 42, 68);     // OK

  char const* s1 = "frederic";
  char const* s2 = "anica";
  char const* s3 = "lucas";
  auto m2 = ::max(s1, s2, s3);    // run-time ERROR

  std::cout << m2 << std::endl; // << ADDED TO CHECK
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra s.cc
s.cc: In function ‘int main()’:
s.cc:28:8: warning: unused variable ‘m1’ [-Wunused-variable]
   auto m1 = ::max(7, 42, 68);     // OK
        ^~
pi@raspberrypi:/tmp $ ./a.out
lucas
相关问题