string s; & s + 1;法律? UB? [英] string s; &s+1; Legal? UB?

查看:116
本文介绍了string s; & s + 1;法律? UB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    string myAry[] = 
    {
        "Mary",
        "had", 
        "a",
        "Little",
        "Lamb"
    };
    const size_t numStrs = sizeof(myStr)/sizeof(myAry[0]);

    vector<string> myVec(&myAry[0], &myAry[numStrs]);

    copy( myVec.begin(), myVec.end(), ostream_iterator<string>(cout, " "));

    return 0;
}

这里感兴趣的是& myAry [numStrs] :numStrs等于5,因此& myAry [numStrs] 指向不存在的东西;数组中的第六个元素。在上面的代码中还有另一个例子: myVec.end(),它指向向量的一个结束 myVec 。取得不存在的这个元素的地址是完美的法律。我们知道 string 的大小,所以我们知道 string 的C风格数组的第6个元素的地址>必须指向。只要我们只评估这个指针,从不解除引用,我们就好了。我们甚至可以将其与其他指针相比较。 STL在对一系列迭代器起作用的算法中一直这样做。 end()迭代器指向超过结束,并且循环在计数器!= end()时保持循环。

Of interest here is &myAry[numStrs]: numStrs is equal to 5, so &myAry[numStrs] points to something that doesn't exist; the sixth element in the array. There is another example of this in the above code: myVec.end(), which points to one-past-the-end of the vector myVec. It's perfecly legal to take the address of this element that doesn't exist. We know the size of string, so we know where the address of the 6th element of a C-style array of strings must point to. So long as we only evaluate this pointer and never dereference it, we're fine. We can even compare it to other pointers for equality. The STL does this all the time in algorithms that act on a range of iterators. The end() iterator points past the end, and the loops keep looping while a counter != end().

现在考虑这样:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    string myStr = "Mary";
    string* myPtr = &myStr;
    vector<string> myVec2(myPtr, &myPtr[1]);

    copy( myVec2.begin(), myVec2.end(), ostream_iterator<string>(cout, " "));   

    return 0;
}

这段代码合法吗?将数组元素的地址放在末尾,如& myAry [numStrs] 是合法和明确的,所以它应该是合法的,并且明确定义假设 myPtr 也是一个数组?

Is this code legal and well-defined? It is legal and well-defined to take the address of an array element past the end, as in &myAry[numStrs], so should it be legal and well-defined to pretend that myPtr is also an array?

推荐答案

而不是UB有一个指针指向一个数组的一个过去的结束,任何单个对象可以被视为一个长度为1的数组;然而,由于& ptr [1] 解除引用的技术性,您需要使用 ptr + 1 采取地址。这也适用于& array [size] 成为 array + size

It is legal and not UB to have a pointer to "one past the end" of an array, and any single object can be treated as if it were in an array of length 1; however, you need to use ptr + 1 instead due to the technicality of &ptr[1] dereferencing and then taking the address. This also applies to &array[size] becoming array + size.

你所有的工作,你会期望在我知道的所有平台上,但考虑到使用明确正确的形式是多么容易,我没有理由不这样做。

What you have will work as you expect on all platforms of which I'm aware, but given how easy it is to use the unambiguously correct form, I see no reason not to do that instead.

这篇关于string s; &amp; s + 1;法律? UB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆