unsigned long long in GCC?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
viral

unsigned long long in GCC?

Post 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)..
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:unsigned long long in GCC?

Post 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?
Every good solution is obvious once you've found it.
viral

Re:unsigned long long in GCC?

Post 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++?
paulbarker

Re:unsigned long long in GCC?

Post 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)?
viral

Re:unsigned long long in GCC?

Post 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?
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

Re:unsigned long long in GCC?

Post 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?
viral

Re:unsigned long long in GCC?

Post 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.
Kemp

Re:unsigned long long in GCC?

Post by Kemp »

sizeof(mmap); ;) In yours I believe you may be defining a struct containing nothing.
paulbarker

Re:unsigned long long in GCC?

Post 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).
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:unsigned long long in GCC?

Post 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...)
Every good solution is obvious once you've found it.
paulbarker

Re:unsigned long long in GCC?

Post 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.
Kemp

Re:unsigned long long in GCC?

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:unsigned long long in GCC?

Post 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.
Kemp

Re:unsigned long long in GCC?

Post 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.
bluecode

Re:unsigned long long in GCC?

Post 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.
Post Reply