一个人能解释在Solaris中的“的dirent'结构的这一定义吗? [英] Can someone explain this definition of the 'dirent' struct in Solaris?

查看:166
本文介绍了一个人能解释在Solaris中的“的dirent'结构的这一定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在寻找(在dirent.h)了的dirent'结构,有点不解其定义。

Recently I was looking at the 'dirent' structure (in dirent.h) and was a little puzzled by its definition.

请注意:这个头文件是从Solaris机器在我的学校

NOTE: This header file is from a Solaris machine at my school.


typedef struct dirent {
    ino_t		d_ino;
    off_t		d_off;
    unsigned short	d_reclen;
    char		d_name[1];
} dirent_t;

特别是d_name领域。这将如何工作的操作系统?如果你需要存储一个空结束的字符串什么好是一个单一的字符数组?我知道,你可以通过它的第一个元素得到一个数组的地址,但我仍然感到困惑。显然,事情正在发生,但我不知道是什么。在我的Fedora Linux系统在国内这一领域被简单地定义为:

Particularly the d_name field. How would this work in the operating system? If you need to store a null terminated string what good is an array of a single char? I know that you can get the address of an array by its first element but I am still confused. Obviously something is happening, but I don't know what. On my Fedora Linux system at home this field is simply defined as:

char d_name[256];

现在,让显而易见的原因很多更有意义。有人可以解释为什么在Solaris头文件定义的结构,因为它呢?

Now that makes a lot more sense for obvious reasons. Can someone explain why the Solaris header file defines the struct as it does?

推荐答案

正如其他人所指出的那样,该结构的最后一个成员没有任何集的大小。该阵列是但是长的实现决定它需要以容纳它希望把在它的字符。它通过动态分配内存的结构,如的malloc 做到这一点。

As others have pointed out, the last member of the struct doesn't have any set size. The array is however long the implementation decides it needs to be to accommodate the characters it wants to put in it. It does this by dynamically allocating the memory for the struct, such as with malloc.

这是方便成员声明为具有大小为1,不过,因为它很容易确定有多少内存被任何的dirent 变量 D占用

It's convenient to declare the member as having size 1, though, because it's easy to determine how much memory is occupied by any dirent variable d:

sizeof(dirent) + strlen(d.d_name)

使用尺寸1也阻碍了这种结构值从接受者尝试自己的名字保存在它,而不是分配自己的的dirent 值。使用Linux的定义,这是合理的假设您有任何的dirent 值将acept 255个字符的字符串,但的Solaris不保证其的dirent 值将存储任何更多的字符比需要的。

Using size 1 also discourages the recipient of such struct values from trying to store their own names in it instead of allocating their own dirent values. Using the Linux definition, it's reasonable to assume that any dirent value you have will acept a 255-character string, but Solaris makes no guarantee that its dirent values will store any more characters than they need to.

我觉得为:C 99介绍了一种特殊情况,一个结构的最后一个成员。该结构可以声明如下代替:

I think it was C 99 that introduced a special case for the last member of a struct. The struct could be declared like this instead:

typedef struct dirent {
  ino_t d_ino;
  off_t d_off;
  unsigned short d_reclen;
  char d_name[];
} dirent_t;

该阵列没有声明的大小。这就是所谓的的灵活的数组成员的。它完成同样的事情与Solaris版本,但没有幻想本身的结构可以容纳的任何的名称。你知道通过看它有更给它。

The array has no declared size. This is known as the flexible array member. It accomplishes the same thing as the Solaris version, except that there's no illusion that the struct by itself could hold any name. You know by looking at it that there's more to it.

使用灵活的声明,占用的内存量将有所调整,像这样:

Using the "flexible" declaration, the amount of memory occupied would be adjusted like so:

sizeof(dirent) + strlen(d.d_name) + 1

这是因为灵活的阵列成员没有到结构的尺寸因素。

That's because the flexible array member does not factor in to the size of the struct.

您没有看到灵活的声明一样,更多的时候,尤其是在OS库code究其原因,可能是为了兼容性的缘故与不支持设施较旧的编译器。它也是书面的目标当前的定义,如果结构的大小改变这样它会打破code兼容性。

The reason you don't see flexible declarations like that more often, especially in OS library code, is likely for the sake of compatibility with older compilers that don't support that facility. It's also for compatibility with code written to target the current definition, which would break if the size of the struct changed like that.

这篇关于一个人能解释在Solaris中的“的dirent'结构的这一定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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