Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How do I determine the underlying type?

Reply
Thread Tools

How do I determine the underlying type?

 
 
Ike Naar
Guest
Posts: n/a
 
      06-27-2013
On 2013-06-27, Bob Williamson <(E-Mail Removed)> wrote:
> I am not sure how to interpret this output. Is dev_t unsigned long or long int?


Just out of curiosity: why do you need to know the underlying type of dev_t?
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-27-2013
On 6/27/2013 3:07 PM, Bob Williamson wrote:
> On 2013-06-27, James Kuyper <(E-Mail Removed)> wrote:
>> On 06/27/2013 02:39 PM, Bob Williamson wrote:
>> ...
>>> bobw@home/tmp$ cat a.c
>>> #include <sys/stat.h>
>>>
>>> bobw@home/tmp$ gcc -E a.c | grep -w dev_t
>>> typedef __dev_t dev_t;
>>>
>>> bobw@home/tmp$ gcc -E a.c | grep -w __dev_t
>>> __extension__ typedef __u_quad_t __dev_t;

>> ...
>>>
>>> bobw@home/tmp$ gcc -E a.c | grep -w __u_quad_t
>>> __extension__ typedef unsigned long long int __u_quad_t;

>> ...
>>>
>>> I am not sure how to interpret this output. Is dev_t unsigned long or long int?

>>
>> dev_t is a typedef for __dev_t. __dev_t is a typedef for __u_quad_t.
>> __u_quad_t is a typedef for unsigned long long int. "long long" is a
>> type specifier that was introduced in C99, 14 years ago. It's guaranteed
>> to be at least 64 bits in size.
>>

>
> James thank you for clarifying this for me. I am not a C programmer so I did
> not know that. I want to understand what gcc is doing on Linux. Ike's idea
> and your explanation here helped quite a bit. Thanks for mentioning the
> debugger angle earlier but something static like this helps me a lot more.


Keep in mind that this definition of dev_t is specific to
the particular implementation you're using. That implementation
defines dev_t (eventually) as unsigned long long int, but other
implementations might use something else: unsigned long int, or
unsigned int, perhaps plain unadorned int, or possibly something
quite strange indeed. That's the reason for using names like
`dev_t' in the first place: You don't need to learn the details
of how every (POSIX-conforming) system on the planet encodes its
device identifiers. In a sense, dev_t is like a contract or an
interface: You just utter the magic word `dev_t', and the local
system's header files declare it as the Right Thing for the local
customs, matching up with the encodings the library traffics in.

Some other examples of this sort of thing are size_t (an integer
of some kind or other that can count any object's bytes), time_t
(some sort of arithmetic type that can represent times and dates),
and FILE (some unspecified sort of thing that holds the information
necessary to read and write data). All of these are likely to resolve
to different fundamental types on different systems, but as long as
you use the "abstract" names you'll get the right result on any system.

So in a sense, the type underlying dev_t is ... dev_t!

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-27-2013
Ike Naar <(E-Mail Removed)> writes:
> On 2013-06-27, Bob Williamson <(E-Mail Removed)> wrote:
>> Hi. How can I determine the underlying type of typedefs? For example the
>> stat structure has various fields in it. Suppose I want to know what dev_t
>> is, where can I find it? If I look in stat.h it's all gobbledygook to
>> me. Ideally I would like a tool to be able to tell me what the type is for
>> any typedef'd name. Is there anything like that?

>
> One can use the C preprocessor to do some of the dirty work.
> Here's what I did to find the underlying type of dev_t on
> the machine I'm currently using:
>
> 1) Created a one-line C file that includes the header for 'stat':
>
> /* begin a.c */
> #include <sys/stat.h>
> /* end a.c */
>
> 2) ran this file through the C preprocessor, and searched for
> the word 'dev_t':
>
> CC -E a.c | grep -w dev_t


Should be cc, as you pointed out in a followup.

> which produced the following output:
>
> typedef __uint64_t dev_t;
> dev_t st_dev;
> dev_t st_rdev;
> int mknod(const char *, __mode_t, dev_t) __asm("__mknod50");
>
> The answer is on the first line of output.


At least for gcc, and assuming a Unix-like sysetm, this is a bit simpler:

echo '#include <sys/stat.h>' | gcc -E - | grep -w dev_t

Though the output on my system is less illuminating:

typedef __dev_t dev_t;

You might need to iterate to find the actual definition.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bob Williamson
Guest
Posts: n/a
 
      06-28-2013
On 2013-06-27, Ike Naar <(E-Mail Removed)> wrote:
> On 2013-06-27, Bob Williamson <(E-Mail Removed)> wrote:
>> I am not sure how to interpret this output. Is dev_t unsigned long or long int?

>
> Just out of curiosity: why do you need to know the underlying type of dev_t?


To use the stat interface from a different language. Thanks to you and
everyone who answered.

Bob
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-28-2013


"Bob Williamson" <(E-Mail Removed)> wrote in message
news:kqjged$pqa$(E-Mail Removed)...
> On 2013-06-27, Ike Naar <(E-Mail Removed)> wrote:
>> On 2013-06-27, Bob Williamson <(E-Mail Removed)> wrote:
>>> I am not sure how to interpret this output. Is dev_t unsigned long or
>>> long int?

>>
>> Just out of curiosity: why do you need to know the underlying type of
>> dev_t?

>
> To use the stat interface from a different language. Thanks to you and
> everyone who answered.


sizeof(dev_t) might be sufficient in that case.

--
Bartc

 
Reply With Quote
 
Bob Williamson
Guest
Posts: n/a
 
      06-28-2013
On 2013-06-28, BartC <(E-Mail Removed)> wrote:
>
>
> "Bob Williamson" <(E-Mail Removed)> wrote in message
> news:kqjged$pqa$(E-Mail Removed)...
>> On 2013-06-27, Ike Naar <(E-Mail Removed)> wrote:
>>> On 2013-06-27, Bob Williamson <(E-Mail Removed)> wrote:
>>>> I am not sure how to interpret this output. Is dev_t unsigned long or
>>>> long int?
>>>
>>> Just out of curiosity: why do you need to know the underlying type of
>>> dev_t?

>>
>> To use the stat interface from a different language. Thanks to you and
>> everyone who answered.

>
> sizeof(dev_t) might be sufficient in that case.


Bart thanks for your posting. It wouldn't be sufficient but it helps. We
would need to know what kind of data the field contains, signed/unsigned
etc. I'll add this to our list.

Bob
>

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-28-2013
"Bob Williamson" <(E-Mail Removed)> wrote in message
news:kqjhv3$u7t$(E-Mail Removed)...
> On 2013-06-28, BartC <(E-Mail Removed)> wrote:


>> "Bob Williamson" <(E-Mail Removed)> wrote in message


>>> To use the stat interface from a different language. Thanks to you and
>>> everyone who answered.

>>
>> sizeof(dev_t) might be sufficient in that case.

>
> Bart thanks for your posting. It wouldn't be sufficient but it helps. We
> would need to know what kind of data the field contains, signed/unsigned
> etc. I'll add this to our list.


OK, but my experience of dealing with APIs/etc specified in C is that the
size of a type is more important than its signedness! (So that you can build
correctly matching structs in the other language, although padding also
needs to correspond, push the right number of bytes in function calls, etc).

And you can't tell, in C, the size of most int types, without looking at the
specs for an implementation or using sizeof(). I don't know if a macro such
as issigned() can be created to tell you an int type's signedness.

--
Bartc

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-28-2013
"BartC" <(E-Mail Removed)> writes:
> "Bob Williamson" <(E-Mail Removed)> wrote in message
> news:kqjhv3$u7t$(E-Mail Removed)...
>> On 2013-06-28, BartC <(E-Mail Removed)> wrote:
>>> "Bob Williamson" <(E-Mail Removed)> wrote in message
>>>> To use the stat interface from a different language. Thanks to you and
>>>> everyone who answered.
>>>
>>> sizeof(dev_t) might be sufficient in that case.

>>
>> Bart thanks for your posting. It wouldn't be sufficient but it helps. We
>> would need to know what kind of data the field contains, signed/unsigned
>> etc. I'll add this to our list.

>
> OK, but my experience of dealing with APIs/etc specified in C is that the
> size of a type is more important than its signedness! (So that you can build
> correctly matching structs in the other language, although padding also
> needs to correspond, push the right number of bytes in function calls, etc).
>
> And you can't tell, in C, the size of most int types, without looking at the
> specs for an implementation or using sizeof(). I don't know if a macro such
> as issigned() can be created to tell you an int type's signedness.


#define IS_SIGNED(type) ((type)-1 < (type)0)
#define IS_FLOATING(type) ((type)1 / 2 != 0)

Untested.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break a bash command into an array consisting of the argumentsin the command? Peng Yu Perl Misc 3 05-13-2013 10:27 AM
When did the bool type make it into the language? Christopher Pisz C++ 13 05-05-2013 03:34 PM
How to capture the “same” RGB images for the same scene using EDSDK? azuring@gmail.com Digital Photography 0 04-28-2013 12:08 PM
How to get JSON values and how to trace sessions?? webmaster@terradon.nl Python 2 04-25-2013 02:12 PM
How to move text within Jlabels to the right clusardi2k@aol.com Java 6 04-13-2013 07:20 PM



Advertisments