sizeof(struct dirent.d_ino) [英] sizeof(struct dirent.d_ino)

查看:93
本文介绍了sizeof(struct dirent.d_ino)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个功能,它将计算后来malloc的数量。在

这个函数中我需要一些struct成员的大小而没有

实例或结构的指针。


As "的sizeof(int)的"是合法的,我假设sizeof(struct x.y)是合法的

。但事实并非如此:


#include< dirent.h>


int dir_dirent_size(dir_t * dirp)

{

int len;


//语法错误

len = sizeof(struct dirent.d_ino)+

sizeof(struct dirent.d_off)+

sizeof(struct dirent.d_reclen)+

strlen(dirp-> name)+ 1;


返回len;

}

我可以使用指向dirent结构的虚拟指针解决这个问题:


#include< dirent.h>


int dir_dirent_size(dir_t * dirp)

{

int len;

struct dirent * temp;


//语法错误

len = sizeof(temp.d_ino)+

sizeof(temp.d_off)+

sizeof(temp.d_reclen)+

strlen(dirp-> name)+ 1;


返回len;

}

然而,我很好奇什么是''正确的事''(tm要做。


谢谢,

Martin Pohlack

解决方案

Martin Pohlack< mp ** @ inf.tu-dresden.de>写道:



我有一个功能,它将计算后来malloc的数量。在
这个函数中,我需要一些struct成员的大小而没有结构的
实例或指针。

As" sizeof(int)"是合法的,我假设sizeof(struct x.y)也是合法的
。但事实并非如此:

< dirent.h>

int dir_dirent_size(dir_t * dirp)
{
int len;

//语法错误
len = sizeof(struct dirent.d_ino)+
sizeof(struct dirent.d_off)+
sizeof(struct dirent.d_reclen)+
strlen(dirp-> name)+ 1;

返回len;
}

我可以使用指向dirent结构的虚指针来解决这个问题:

#include< dirent.h>


这是一个非标准的标题。无论如何,我想它声明

(至少)你后面提到的struct dirent ......

int dir_dirent_size(dir_t * dirp)
{
int len;
struct dirent * temp;

//语法错误
len = sizeof(temp.d_ino)+
sizeof(temp.d_off) +
sizeof(temp.d_reclen)+
strlen(dirp-> name)+ 1;

返回len;
}

但是,我很好奇将要做什么''正确''(tm)。




您应该将上述内容更改为:


len = sizeof temp-> d_ino +

sizeof temp-> d_off +

sizeof temp-> d_reclen +

...


因为temp是结构的*指针*而且parantheses是可选的,

在应用sizeof运算符时对象,而不是类型。


此外,如果您打算计算/ whole /

结构的大小,包括在内ible padding,你应该写:


len = sizeof * temp +

...


问候,


Irrwahn

-

新的运动:内联攀爬


< blockquote>首先,谢谢你的回答。


Irrwahn Grausewitz写道:

Martin Pohlack< mp ** @ inf.tu-dresden。德>写道:



我有一个功能,它将计算后来的malloc的数量。
在这个函数我需要一些结构成员的大小而不具有结构的实例或指针。

Assizeof(int)是合法的,我假设sizeof(struct x.y)也是合法的。但事实并非如此:

#include< dirent.h>

int dir_dirent_size(dir_t * dirp){int len;

//语法错误len = sizeof(struct dirent.d_ino)+ sizeof(struct
dirent.d_off)+ sizeof(struct dirent.d_reclen)+ strlen(dirp-> name)
+ 1;

返回len; }

我可以使用指向dirent结构的虚指针解决这个问题:

#include< dirent.h>
这是一个非标准的标题。无论如何,我想它(稍后)声明你稍后提到的struct dirent ......



是的,结构在那里声明,但不应该重要的是

我的例子。无论如何,结构看起来如下:


struct dirent

{

long d_ino;

off_t d_off;

unsigned short d_reclen;

char d_name [256];

};

int dir_dirent_size(dir_t * dirp){int len; struct dirent * temp;

//语法错误len = sizeof(temp.d_ino)+ sizeof(temp.d_off)+
sizeof(temp.d_reclen)+ strlen(dirp->名称)+ 1;

返回len; }

然而,我很好奇要做什么''正确''(tm)。



您应该将以上内容更改为:

len = sizeof temp-> d_ino + sizeof temp-> d_off + sizeof temp-> d_reclen
+ ...

因为temp是一个结构的*指针*和parantheses是可选的,
将sizeof运算符应用于一个对象而不是一个类型。



是的,你是对的。我必须承认我构建了这个例子而没有

试图在最后一次更改后重新编译它。所以,你写的是

我想到的是什么;-)。然而,可选与禁止不一样。

所以我认为parantheses的问题是风格问题。


此外,如果你的意图是要计算
/ whole / struct的大小,包括可能的填充,你应该写:

len = sizeof * temp + ...



是的,当然这就是我*会做的事情。但是我不想要那个。


我想计算结构*的*部分*的大小而没有*具有

的实例它,因为可以通过

来计算int的大小:


x = sizeof(int);


相反,它可以用一个实例来做:

int a;

x = sizeof(x);





int * a;

x = sizeof(* a);


了解更多复杂类型它似乎只能使用

最后一种情况之一。


或者有办法???

顺便说一句。我的一个(当地)朋友建议如下:


len = sizeof(((struct dirent *)0) - > d_ino);

greets ,

Martin


Martin Pohlack< mp ** @ os.inf.tu-dresden.de>写道:

我想计算结构*的*部分*的大小而没有*具有
它的实例,


为什么?


如果您唯一担心的是浪费空间,我不会担心。一个结构的

实例,除了它的成员是'sizeof''操作符的
操作数之外没用过,可能会被
$优化掉b $ b编译器。


如果您有其他原因,请继续阅读......

顺便说一句。我的一个(当地)朋友建议如下:

len = sizeof(((struct dirent *)0) - > d_ino);




这会导致未定义的行为。但是,你可以使用`offsetof''

宏(在< stddef.h>中定义),它产生了一个结构的偏移量,从一开始就是
成员结构体。如果你有,例如,


struct foo {

int bar;

long baz;

float blah;

double blub;

void * platsch;

};


那么表达


offsetof(struct foo,blub) - offsetof(struct foo,blah)


产生'blah''的大小,和


sizeof(struct foo) - offsetof(struct foo,platsch)


产生'platsch''的大小。


马丁


Hi,

I have a funtion which shall compute the amount for a later malloc. In
this function I need the sizes of some struct members without having an
instance or pointer of the struct.

As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal
too. But is is not:

#include <dirent.h>

int dir_dirent_size(dir_t * dirp)
{
int len;

// syntax error
len = sizeof(struct dirent.d_ino) +
sizeof(struct dirent.d_off) +
sizeof(struct dirent.d_reclen) +
strlen(dirp->name) + 1;

return len;
}
I can fix this problem using a dummy pointer to a dirent struct:

#include <dirent.h>

int dir_dirent_size(dir_t * dirp)
{
int len;
struct dirent * temp;

// syntax error
len = sizeof(temp.d_ino) +
sizeof(temp.d_off) +
sizeof(temp.d_reclen) +
strlen(dirp->name) + 1;

return len;
}
However, I''m curious what would be the ''right thing'' (tm) to do.

Thank you,
Martin Pohlack

解决方案

Martin Pohlack <mp**@inf.tu-dresden.de> wrote:

Hi,

I have a funtion which shall compute the amount for a later malloc. In
this function I need the sizes of some struct members without having an
instance or pointer of the struct.

As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal
too. But is is not:

#include <dirent.h>

int dir_dirent_size(dir_t * dirp)
{
int len;

// syntax error
len = sizeof(struct dirent.d_ino) +
sizeof(struct dirent.d_off) +
sizeof(struct dirent.d_reclen) +
strlen(dirp->name) + 1;

return len;
}
I can fix this problem using a dummy pointer to a dirent struct:

#include <dirent.h>
This is a non-standard header. Anyway, I suppose it declares
(at least) the struct dirent you are referring to later...

int dir_dirent_size(dir_t * dirp)
{
int len;
struct dirent * temp;

// syntax error
len = sizeof(temp.d_ino) +
sizeof(temp.d_off) +
sizeof(temp.d_reclen) +
strlen(dirp->name) + 1;

return len;
}
However, I''m curious what would be the ''right thing'' (tm) to do.



You should change the above to:

len = sizeof temp->d_ino +
sizeof temp->d_off +
sizeof temp->d_reclen +
...

as temp is a *pointer* to a struct and the parantheses are optional,
when applying the sizeof operator to an object, not a type.

Further more, if your intention is to calculate the size of the /whole/
struct, inclusive possible padding, you should just write:

len = sizeof *temp +
...

Regards,

Irrwahn
--
New funsports: inline climbing


First of all, thank you for your answer.

Irrwahn Grausewitz wrote:

Martin Pohlack <mp**@inf.tu-dresden.de> wrote:

Hi,

I have a funtion which shall compute the amount for a later malloc.
In this function I need the sizes of some struct members without
having an instance or pointer of the struct.

As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be
legal too. But is is not:

#include <dirent.h>

int dir_dirent_size(dir_t * dirp) { int len;

// syntax error len = sizeof(struct dirent.d_ino) + sizeof(struct
dirent.d_off) + sizeof(struct dirent.d_reclen) + strlen(dirp->name)
+ 1;

return len; }
I can fix this problem using a dummy pointer to a dirent struct:

#include <dirent.h>
This is a non-standard header. Anyway, I suppose it declares (at
least) the struct dirent you are referring to later...


yes, the struct is declared there, but that should not be important for
my example. Anyway, the struct looks like follows:

struct dirent
{
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[256];
};

int dir_dirent_size(dir_t * dirp) { int len; struct dirent * temp;

// syntax error len = sizeof(temp.d_ino) + sizeof(temp.d_off) +
sizeof(temp.d_reclen) + strlen(dirp->name) + 1;

return len; }
However, I''m curious what would be the ''right thing'' (tm) to do.


You should change the above to:

len = sizeof temp->d_ino + sizeof temp->d_off + sizeof temp->d_reclen
+ ...

as temp is a *pointer* to a struct and the parantheses are optional,
when applying the sizeof operator to an object, not a type.


yes, you are right. I must confess I constructed the example without of
trying to recompile it after the last changes. So, what you wrote is
what I head in mind ;-). However, optional is not the same as forbidden.
So I consider the question of parantheses as a question of style.

Further more, if your intention is to calculate the size of the
/whole/ struct, inclusive possible padding, you should just write:

len = sizeof *temp + ...


Yes, of course that is what I *would* do. But I don''t want that.

I want to compute the size of *parts* of the struct *without* of having
an instance of it, as it is possible to compute the size of an int by
writing:

x = sizeof(int);

On the contrary it is possible to do it with an instance:
int a;
x = sizeof(x);

or

int *a;
x = sizeof(*a);

For more complex types it only seems to be possible to use one of the
last cases.

Or is there a way???
btw. a (local) friend of mine suggested the following:

len = sizeof (((struct dirent *) 0)->d_ino);
greets,
Martin


Martin Pohlack <mp**@os.inf.tu-dresden.de> writes:

I want to compute the size of *parts* of the struct *without* of having
an instance of it,
Why?

If your only concern is wasted space, I wouldn''t worry about it. An
instance of a structure which is not used except that its members are
operands to the `sizeof'' operator is likely to be optimized away by
the compiler.

If you have another reason, read on...
btw. a (local) friend of mine suggested the following:

len = sizeof (((struct dirent *) 0)->d_ino);



This causes undefined behavior. However, you could use the `offsetof''
macro (defined in <stddef.h>) which yields the offset of a structure
member from the beginning of the structure. If you have, for example,

struct foo {
int bar;
long baz;
float blah;
double blub;
void *platsch;
};

then the expression

offsetof (struct foo, blub) - offsetof (struct foo, blah)

yields the size of `blah'', and

sizeof (struct foo) - offsetof (struct foo, platsch)

yields the size of `platsch''.

Martin


这篇关于sizeof(struct dirent.d_ino)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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