本文介绍了在 C 中使用灵活的数组成员是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近读到在 C 中使用灵活的数组成员是一种糟糕的软件工程实践.然而,该声明没有任何论据支持.这是公认的事实吗?

I recently read that using flexible array members in C was poor software engineering practice. However, that statement was not backed by any argument. Is this an accepted fact?

(灵活数组成员是 C99 中引入的一项 C 特性,可以通过它声明最后一个元素是一个未指定大小的数组.例如:)

(Flexible array members are a C feature introduced in C99 whereby one can declare the last element to be an array of unspecified size. For example: )

struct header {
    size_t len;
    unsigned char data[];
};

推荐答案

使用 goto 是一种糟糕的软件工程实践,这是一个公认的事实".这并不能说明问题.有时 goto 很有用,特别是在处理清理和从汇编程序移植时.

It is an accepted "fact" that using goto is poor software engineering practice. That doesn't make it true. There are times when goto is useful, particularly when handling cleanup and when porting from assembler.

灵活的数组成员让我印象深刻,它有一个主要用途,这是我最想知道的,那就是在 RiscOS 上映射传统数据格式,例如窗口模板格式.大约 15 年前,它们在这方面会非常有用,而且我相信仍然有人在处理此类事情时会发现它们很有用.

Flexible array members strike me as having one main use, off the top of my head, which is mapping legacy data formats like window template formats on RiscOS. They would have been supremely useful for this about 15 years ago, and I'm sure there are still people out there dealing with such things who would find them useful.

如果使用灵活的数组成员是不好的做法,那么我建议我们都去告诉 C99 规范的作者.我怀疑他们可能会有不同的答案.

If using flexible array members is bad practice, then I suggest that we all go tell the authors of the C99 spec this. I suspect they might have a different answer.

这篇关于在 C 中使用灵活的数组成员是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 02:01