Clang抱怨:“指针由临时数组初始化” [英] Clang complains: "pointer is initialized by a temporary array"

查看:293
本文介绍了Clang抱怨:“指针由临时数组初始化”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同长度的数组(指针)数组,我学会了我可以使用复合文字定义:

I have an array of (pointers to) arrays of different lengths, which I learned I could define using compound literals:

const uint8_t *const minutes[] = {
    (const uint8_t[]) {END},
    (const uint8_t[]) {1, 2, 3, 4, 5 END},
    (const uint8_t[]) {8, 9, END},
    (const uint8_t[]) {10, 11, 12, END},
    ...
}; 

gcc接受这个很好,但clang说:临时数组,它将在full-expression 结尾处销毁。这是什么意思?代码似乎正常工作,但是,当它们指向不再分配的内存时,很多事情似乎工作。这是我需要担心的吗? (最终我真的需要它与gcc一起工作。)

gcc accepts this just fine, but clang says: pointer is initialized by a temporary array, which will be destroyed at the end of the full-expression. What does this mean? The code seems to be working, but then again, lots of things seem to work when they point to memory that's no longer allocated. Is this something I need to worry about? (Ultimately I only really need it to work with gcc.)

更新:有些腥味正在发生。它说此处

Update: Something fishy is going on. It says here that:


复合文字产生左值。这意味着您可以获取复合字面值的地址,复合字面值是复合字面值声明的未命名对象的地址。只要复合字面值没有const限定类型,就可以使用指针修改它。

Compound literals yield lvalues. This means that you can take the address of a compound literal, which is the address of the unnamed object declared by the compound literal. As long as the compound literal does not have a const-qualified type, you can use the pointer to modify it.

   `struct POINT *p;
   p = &(struct POINT) {1, 1};


此示例代码似乎正在做我正在做的要做:指向由复合字面量定义的东西的指针。那么clang错误消息是否合法?

This example code seems to be doing exactly what I'm trying to do: a pointer to something defined by a compound literal. So is the clang error message legitimate? Will this end up pointing to unallocated memory when compiled with either clang or gcc?

更新2
找到一些文档:在C中,复合文字指定具有静态或自动存储的未命名对象持续时间。在C ++中,复合字面值指定一个临时对象,它只会生存到其完整表达式的结尾。
所以看起来clang是正确的警告,gcc可能应该即使使用 -Wall -Wextra

Update 2: Found some documentation: "In C, a compound literal designates an unnamed object with static or automatic storage duration. In C++, a compound literal designates a temporary object, which only lives until the end of its full-expression." So it seems that clang is right to warn about this, and gcc probably ought to as well, but doesn't, even with -Wall -Wextra.

我不能猜测为什么

推荐答案

好的,clang是一个非常有用的C特性从C ++中删除,没有提供完美的替代方法来完成同样的事情。 ,这应该这样做:

Well, clang is right, and this shall be done that way:

namespace elements
{
    const uint8_t row1[] = {END};
    const uint8_t row2[] = {1, 2, 3, 4, 5, END};
    ...
}
const uint8_t *const minutes[] = {
    elements::row1,
    elements::row2,
    ...
}; 

你可以想到更多的C ++解决方案,比如使用 std :: tuple

You can think of more C++ solution, like using std::tuple:

#include <tuple>

constexpr auto minutes = std::make_tuple(
    std::make_tuple(), 
    std::make_tuple(1,2,3,4,5),
    std::make_tuple(8,9,10)); 

#include <iostream>
#include <type_traits>
int main() {
    std::cout << std::tuple_size<decltype(minutes)>::value << std::endl;
    std::cout << std::tuple_size<std::remove_reference_t<decltype(std::get<1>(minutes))>>::value << std::endl;
}

这篇关于Clang抱怨:“指针由临时数组初始化”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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