Re: long long problem

From: Keith Thompson (kst-u_at_mib.org)
Date: 03/04/05


Date: Fri, 04 Mar 2005 21:32:10 GMT

karl_m@acm.org writes:
> I'm having trouble with unsigned long long declarations on my SCO unix
> 7.1.1 compiler. Can you test the following code? I'm expecting both
> printf functions to report 1. Thanks, karl m

But you don't tell us how it behaves on your system.

> #include <stdio.h>
>
> typedef struct {
> unsigned long long cnt:16;
> unsigned long long right:48;
> } Page;

C only requires support for bitfields of types signed int, plain int,
and unsigned int (and _Bool in C99). Many compilers support other
types of bitfields as an extension.

> typedef struct {
> Page *frame;
> } Bt1;
>
> typedef struct {
> Page frame[1];
> } Bt2;
>
> char buff[4096];
>
> int main (int argc, char **argv)
> {
> Bt1 bt1[1];
> Bt2 bt2[1];
>
> bt1->frame = (Page*)buff;

That's interesting. I was about to complain that bt1 is an array, not
a struct pointer, but of course the array name decays to a pointer in
most contexts, including the left operand of a "->". It's legal and
it works, but I've never seen that idiom before.

I would have declared:

    Bt1 bt1;
    Bt2 bt2;

and used bt1.frame rather than bt1->frame.

As for assigning frame to point to buff, there's no guarantee that
buff is properly aligned for an object of type Page (though it's
likely that you'll be "lucky" and it will be). If you want to
allocate space for a Page, either declare an object of type Page or
use malloc().

> bt1->frame->cnt = 1;
> bt1->frame->right = 30;
> printf ("cnt1 = %d\n", bt1->frame->cnt);

You're using a "%d" format for an unsigned long long value. Try
"%lld". (I suspect that's the cause of your problem, but you didn't
tell us what problem, if any, you're actually having.)

> bt2->frame->cnt = 1;
> bt2->frame->right = 30;
> printf ("cnt2 = %d\n", bt2->frame->cnt);

And again.

> }

You should also return 0 from the main program. (It's not required in
C99, but it's good style anyway.)

BTW, when I ran the program, I got:

cnt1 = 1
cnt2 = 1

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.


Relevant Pages

  • Re: Ommiting void
    ... easily avoid the issue by always declarating "int main". ... I do, however, also drop K&R style declarations, along with implicit ... If you don't want to support them in your compiler, that's fine, ... actually, in my case I seem not to mix well with moderators, as moderators necessarily reject my posts, and English teachers necessarily give me very low grades, even when my grammar and spelling is correct. ...
    (comp.std.c)
  • Re: union
    ... > typedef struct { ... int is long is 32 bits. ... Not on any major WinXP compiler of which I'm aware. ...
    (comp.lang.c)
  • Re: assigment statement can use an expression when declaring a variable?
    ... int main ... initialize, you can use an expression on the right hand side of the ... But the declarations are not statements, ... if you're using the popular gcc compiler ...
    (comp.lang.c)
  • Re: simple assignment of structs vs. memcpy
    ... typedef lots int; //lots of data. ... typedef struct one_{ ... A naive compiler might call memcpy explicitly for the second case, but generate inline code for the first. ...
    (comp.lang.c)
  • Re: Problems with iterators ans lists
    ... >> If I use with a single list of int istīs ok but if the list containing ... >But a better solution would be to get a more modern version of the STL. ... Which compiler are you using at the ...
    (comp.lang.cpp)