473,624 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof(struct dirent.d_ino)

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(str uct 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_i no) +
sizeof(temp.d_o ff) +
sizeof(temp.d_r eclen) +
strlen(dirp->name) + 1;

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

Thank you,
Martin Pohlack

Nov 13 '05 #1
19 9220
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(str uct 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_i no) +
sizeof(temp.d_o ff) +
sizeof(temp.d_r eclen) +
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
Nov 13 '05 #2
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(str uct 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_i no) + sizeof(temp.d_o ff) +
sizeof(temp.d_r eclen) + 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

Nov 13 '05 #3
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
Nov 13 '05 #4
On 08 Sep 2003 23:23:33 +0200 Martin Dickopp (MD) wrote:

Hi,

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

Agreed.

MD> > len = sizeof (((struct dirent *) 0)->d_ino);
MD>
MD> This causes undefined behavior.

Can you elaborate on why this is undefined behaviour? It is implemented
in a similar manner to the offsetof macro.

MD> offsetof (struct foo, blub) - offsetof (struct foo, blah)
MD>
MD> yields the size of `blah', and

Tricks like this rely on the order of structure members being known and
never changing which is why we considered it sub-optimal.

-Udo.
Nov 13 '05 #5
Martin Pohlack <mp**@os.inf. tu-dresden.de> wrote:
<SNIP>
However, optional is not the same as forbidden.
So I consider the question of parantheses as a question of style. Agreed.

<SNIP>
I want to compute the size of *parts* of the struct *without* of having
an instance of it I have to accept that that's what you want, but may I ask what's so
terrible on having an instance of, or, at least, a pointer to the type
you want to know the size of? What's the reason for the restriction?

<SNIP> int a;
x = sizeof(x); ^^^
That's hopefully just a typo... :^)

<SNIP>btw. a (local) friend of mine suggested the following:

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

Hm, this looks like a legal C construct, as it does not dereference
the NULL pointer AFAICS, but I'm not among the experts, so maybe I'm
terribly wrong on this...

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #6
"Udo A. Steinberg" <us**@os.inf. tu-dresden.de> writes:
MD> > len = sizeof (((struct dirent *) 0)->d_ino);
MD>
MD> This causes undefined behavior.

Can you elaborate on why this is undefined behaviour?
Standard section 6.5.2.3#4 defines the behavior of the `->' operator
only for the case that the left operand points to an object, which a
null pointer of type `struct dirent *' does not. Section 4#2 makes it
clear that whenever no behavior is defined explicitly, the behavior
is undefined.
It is implemented in a similar manner to the offsetof macro.
The standard doesn't define how the `offsetof' is implemented.

Many C implementations do in fact apply the `->' operator to a null
pointer of appropriate type, but that doesn't mean it's also valid in
user code. :) The language implementor knows what extensions his
implementation supports and is therefore not bound by the standard.
In fact, I don't think there's any portable way to achive the effect
of the `offsetof' macro without actually using it.
MD> offsetof (struct foo, blub) - offsetof (struct foo, blah)
MD>
MD> yields the size of `blah', and

Tricks like this rely on the order of structure members being known and
never changing which is why we considered it sub-optimal.


I agree. Which brings me back to the question of what you (or rather the
OP, but you seem to be affiliated with the same TUD institute, so can I
assume you work on the same thing?) are trying to achieve?

Martin
Nov 13 '05 #7
On Tue, 9 Sep 2003 00:04:56 +0200
"Udo A. Steinberg" <us**@os.inf. tu-dresden.de> wrote:
On 08 Sep 2003 23:23:33 +0200 Martin Dickopp (MD) wrote:

Hi,

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

Agreed.

MD> > len = sizeof (((struct dirent *) 0)->d_ino);
MD>
MD> This causes undefined behavior.

Can you elaborate on why this is undefined behaviour? It is
implemented in a similar manner to the offsetof macro.
The implementation is allowed to use any non-standard tricks it like in
the headers it provides since those headers are only for that
implementation. It could even have special magic in the compiler to make
those headers work without making the same tricks work in user generated
code.

In the example above the null pointer is being dereferenced so it is
undefined behaviour in any code you write.

You could always use something like
{
struct dirent dummy;
len = sizeof dummy.d_ino;
}

I would not expect any decent compiler to allocate space for dummy.
MD> offsetof (struct foo, blub) - offsetof (struct foo, blah)
MD>
MD> yields the size of `blah', and

Tricks like this rely on the order of structure members being known
and never changing which is why we considered it sub-optimal.


It also has the potential to give a different result since there might
be padding between the end of blah and the start of blub and does not
work for the last element since there is no member after it.
--
Mark Gordon
Nov 13 '05 #8
Irrwahn Grausewitz <ir*****@freene t.de> writes:
Martin Pohlack <mp**@os.inf. tu-dresden.de> wrote:
len = sizeof (((struct dirent *) 0)->d_ino);

Hm, this looks like a legal C construct, as it does not dereference
the NULL pointer AFAICS,


It doesn't dereference the null pointer, since the operand of the `sizeof'
operator is not evaluated. But according to my understanding of the
standard, the behavior is still undefined. See my other posting in this
thread.

Martin
Nov 13 '05 #9
Mark Gordon <sp******@fla sh-gordon.me.uk> writes:
On Tue, 9 Sep 2003 00:04:56 +0200
"Udo A. Steinberg" <us**@os.inf. tu-dresden.de> wrote:
On 08 Sep 2003 23:23:33 +0200 Martin Dickopp (MD) wrote:

Hi,

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

Agreed.

MD> > len = sizeof (((struct dirent *) 0)->d_ino);
MD>
MD> This causes undefined behavior.
In the example above the null pointer is being dereferenced so it is
undefined behaviour in any code you write.


I don't think the null pointer is dereferenced, as the operand of the
`sizeof' operator is not evaluated. I believe it to be undefined behavior
because the standard only defines the behavior of `->' when its left
operand points to an object (6.5.2.3#4).
MD> offsetof (struct foo, blub) - offsetof (struct foo, blah)
MD>
MD> yields the size of `blah', and

Tricks like this rely on the order of structure members being known
and never changing which is why we considered it sub-optimal.


It also has the potential to give a different result since there might
be padding between the end of blah


Correct.
and the start of blub and does not work for the last element since there
is no member after it.


But there /is/ a member after the last one - don't you see it? ;)
Its offset from the beginning of the structure is `sizeof (struct foo)'.

Martin
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
2697
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it too, up until I started posting here! Thanks, Chris
7
7193
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure, sizeof(struct something), it will not give us the correct number (i.e. the size of the structure
15
15155
by: ak | last post by:
struct xy{ int x; int y; } _xy; size_of_xy(struct xy * a) { int len = sizeof a; printf( "\sizeof xy: %i\n", len ); }
10
3381
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
8
5901
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I can't think of a way to measure non-fundamental types without including a little bit of C, which I'd rather avoid. How safe would I be assuming that sizeof(struct timeval) == 2 * sizeof(long)
7
3191
by: sophia.agnes | last post by:
Dear all, why the following program is giving o/p as sizeof(struct emp) = 0 #include<stdio.h> #include<stdlib.h> struct emp
0
8238
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8174
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.