Page 1 of 2

unsigned long long in GCC?

Posted: Fri Jul 21, 2006 7:30 am
by viral
Hello..
I used to develop my OS in Windows using DJGPP, In my memory management code I used memory map structure(to count RAM) as:

Code: Select all

struct mmap
{
       unsigned long long base;
       unsigned long long length;
       unsigned int type;
}
It worked very well... But now I have transfered my OSDEVing from Windows to Linux (using Red Hat 9, GCC 3).

Now this is giving a lot of probs... The kernel compile nicely.. But there is some prob in this structure.. When I print sizeof(struct mmap)... everytime it gives we weired numbers...

Why this is so? Is it so that GCC does not support "long long"? I want a data type to store 64 bit data(not double plz)..

Re:unsigned long long in GCC?

Posted: Fri Jul 21, 2006 7:51 am
by Solar
GCC certainly does support "long long", at least in C - C++ does not know that type (unfortunately).

AFAIK "long long" got introduced into the C standard only "recently" (C99?). It also requires some support functions in GCC (_ldiv...something), and padding settings might differ from toolchain to toolchain... what are your "weird numbers", may I ask?

Re:unsigned long long in GCC?

Posted: Fri Jul 21, 2006 2:59 pm
by viral
Hello...
Do I have to give some command line option to GCC to tell it that I am using long long and my code is in C++?
what are your "weird numbers", may I ask?
In a for loop I am printing sizeof(struct mmap) and instead of printing same values everytime it is printing 0x0 and 0xFF800000.

Is there no way to use long long in C++?

Re:unsigned long long in GCC?

Posted: Fri Jul 21, 2006 4:29 pm
by paulbarker
This looks like more of a problem with the printing code. What do you get if you print sizeof(int), sizeof(long) and sizeof(long long)?

Re:unsigned long long in GCC?

Posted: Fri Jul 21, 2006 10:14 pm
by viral
Hi...
There is no problem in printing code. I am getting exact values when I print sizeof(int), sizeof(long) and even sizeof(long long). The problem comes when I encapsulate "long long" in a structure.

Once "long long" is a part of structure, printing sizeof(struct THAT_CONTAIN_LONG_LONG) does not print proper size.

Solar has pointed here that "long long" does not work with C++, Is there any other way?

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 10:35 am
by spix
Once "long long" is a part of structure, printing sizeof(struct THAT_CONTAIN_LONG_LONG) does not print proper size.

Solar has pointed here that "long long" does not work with C++, Is there any other way?
This is just a shot in the dark, but if you put long long inside a structure, then the sizeof the structure is larger than long long. Wouldn't that cause some sort of overflow in your print code?

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 12:04 pm
by viral
Hi..
Wouldn't that cause some sort of overflow in your print code?
I know that the size of structure will certainly be larger than long long... What I am doing is:

Code: Select all

struct mmap
{
        unsigned long long base;
        unsigned long long length;
        unsigned int type;
};
       printf("\nStructSize = %d    LongLongSize=%d",
                       sizeof(struct mmap),
                       sizeof(long long)
            );
Now LongLongSize is 8 but StructSize is 0(not fixed) instead of 20.

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 1:10 pm
by Kemp
sizeof(mmap); ;) In yours I believe you may be defining a struct containing nothing.

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 1:57 pm
by paulbarker
Just to clarify:

in C, you want: struct mmap
in C++: mmap

You automatically get a 'typedef struct mmap mmap' in C++ (or near enough anyway, not sure if the semantics are exactly the same).

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 3:08 pm
by Solar
Huh?

I mean, huh?

paulbarker, could you point out where you got that difference in struct syntax between C and C++ from? I've never heard of it... (then again, I seldom if ever use "struct" in C++ code...)

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 3:26 pm
by paulbarker
@Solar:

If we have a struct defined as:

Code: Select all

struct mmap { ... };
In C, it can only be accessed via 'struct mmap', since it goes in the tag namespace (with unions and the like). In C++, there is no tag namespace, the name of the struct is simply 'mmap' rather than 'struct mmap'. It's still valid C++ to use 'struct mmap' but in practice it is never used.

So the effect of using C++ rather than C is like surrounding every struct and union with a typedef like this:

Code: Select all

typedef struct mmap { ... } mmap;
Therefore, this *is* valid C++ but invalid C:

Code: Select all

struct mmap
{
        char ch;
};

mmap mm;
@Viral

I compiled the printf code you posted with g++ and ran it, giving:
StructSize = 20 LongLongSize=8
.

The same result happens when gcc is used to compile it rather than g++, and when using 'sizeof(mmap)' instead of 'sizeof(struct mmap)'. I really havn't a clue whats causing your problem.

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 4:11 pm
by Kemp
To be completely honest, I only recall ever seeing code such as sizeof(mmap) (ie, without the struct keyword) whether I'm looking at C or C++ code. This is much the same as only seeing code such as "mmap anotherMap" (once again without the extra struct keyword) in both C and C++. To me (in both my experience and looking at it from a readability viewpoint, as well as from the viewpoint of what it actually means), using the extra keyword makes no sense.

Of course, if I'm proved wrong I will use the excuse that most of my recent experience has been with C++, so it has erased the old ways from my mind :P


Edit:
I am, of course, wrong. I still say my favoured way is best though (as anyone would about their favoured way ;D).

btw, for those going from C++ to C, or that have used C++ for long enough to forget some of the differences, here's a useful link:
http://people.cs.uchicago.edu/~iancooke/osstuff/ccc.html

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 5:04 pm
by Candy
AFAIK, you always need to say:

Code: Select all

typedef struct {

} xyz;
, both in C and C++, since they're based on the same language. For classes this typedef is implicit. At least, that's what I know.

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 5:07 pm
by Kemp
Structs actually changed quite substantially between C and C++, though by coincedence or design (are they even different these days?) one form of it will work in both.

Re:unsigned long long in GCC?

Posted: Sat Jul 22, 2006 5:33 pm
by bluecode
hi,

I've always been using C++ and

Code: Select all

struct myname{};
to define structures. And I can use them without the struct keyword. I never got any warnings with a C++ compiler. So struct implicitly defines a new type in C++ like the class keyword.