Compatibility of long and ULong_t64

Hi,

I have one question regarding compatibility of long (32bit system) and Ulong_t64 when importing from a Tree and using an external compiler without using a generated dictionary.
Would following example work correctly?

[code]TTree* sometree = new TTree(“whatsoever”,“Testtree”);

struct { ULong_t64 test1;
ULong_t64 test2;
} test_struct;

sometree->Branch(“teststruct”, &test_struct, “test1/l:test2”);
sometree->Fill();

struct { unsigned long test1;
unsigned long test2;
} test_struct32;

sometree->SetBranchAddress(“teststruct”, &test_struct32);
sometree->GetEntry(0);[/code]

Or will it read in bad data?
Thanks for every help.

BR

This will work when reading on a 64 bits machine, but not on 32 bits.
I do not understand why you do not want to declare the variable as ULong64_t when reading! you do not need any dictionary to do it. I am probably missing something.

Rene

Thank you for your quick answer. I have a big pile of C code which I am reusing which utilises a lot of structs with a lot of data type long. So I only wondered if I can save some time. But I will redeclare all of the long variables to ULong64_t now.

I have one more small question.

There is no problem if I do …

[code]ULong64_t test1;
unsigned long test2;

test1 = (ULong64_t)test2;[/code]
or is there?

Because I am reading in a 32bit binary file and have to use a struct which contains unsigned longs. Or should I use ULong_t to be able to also read the 32bit binary file on a 64 bit system? If this is possible at all.

Thank you very much for all your time. I really appreciate it.

BR

No, this will not work.
The best you can do is to change your declaration to unsigned long long if you work on a Linux machine,
but this declaration is not portable (eg on Windows, but may be you don’t care.

Rene

Okay. But if I read in the data on 32 bit system then the code in the last post would work, or am I wrong again?
I would only typecast the unsigned long into a ULong64_t to be able to insert the data into a Tree. Or is it impossible to typecast an unsigned long into a ULong64_t?

Thanks again.

BR

No, let me repeat, what you post will not work. You will simply overwrite your code!
On a 32 bit machine, “unsigned long” is a 4 byte integer

Rene

I understand that the code in my first post won’t work. But why won’t the typecast itself not work? Perhaps it is a silly question and I am getting a lot wrong. Isn’t it possible to store a long in Long64_t?

BR

How to phrase it? Your assumption that a “long” on a 32 bit system is an 8 byte integer is wrong.
just print the value of sizeof(long) to check it.

Rene

Okay. So I cannot typecast like this …

long b; Long64_t a = (Long64_t)b;
But how should I read in data of a 32 bit binary which contains “long” and how should I handle it to add it to a Tree? Because I have to use “long” to read it out of the binary file. Do you have an ideas?
Thank’s again.

BR

if your original data is a 32 bit unsigned integer, declare it as “unsigned int”. This is a portable type and it will work OK on 32 and 64 bits machines.

Rene

Thank’s. I will do it that way.

BR

Hi,

Please note that if instead of using the leaflist technique of branch creation you were to generate a dictionary for the struct and use the object technique of branch creation then I/O would be able to do schema evolution to and from any of the numerical type. However you still need to be careful since reading a 64 bits number into a 32 bits variable might/will still lead to truncation. So you are still better of making sure to use a portable type.

Cheers,
Philippe