Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 64 bit Endianness issue

Reply
Thread Tools

64 bit Endianness issue

 
 
sureshjayaram@gmail.com
Guest
Posts: n/a
 
      05-11-2006
I have problem with the code only with 64 bit big endian machine.
i have a member size_t val1 in my structure. The size of size_t is
32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
machines.

I am passing address of this variable to a different function (where it
accepts only pointer to unsigned int) unsigned int *. Basically passing
a unsigned long pointer instead of unsigned int pointer. I think the
value is getting truncated while dereferencing int *. In this case how
do I make my code portable? Can functions like htonl/ntohl will help?
or casting could be of any help?

Thanks,
Suresh

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-11-2006
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> wrote:
>I have problem with the code only with 64 bit big endian machine.
>i have a member size_t val1 in my structure. The size of size_t is
>32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
>machines.


>I am passing address of this variable to a different function (where it
>accepts only pointer to unsigned int) unsigned int *. Basically passing
>a unsigned long pointer instead of unsigned int pointer. I think the
>value is getting truncated while dereferencing int *. In this case how
>do I make my code portable?


On the 64 bit machines, can the values to be stored in the structure
member legitimately exceed 32 bits? If they can, then chances
are your only choice is to rewrite the other function. There is
nothing you can do to "remotely" force another routine to operate on
more bits than it was coded for.
--
All is vanity. -- Ecclesiastes
 
Reply With Quote
 
 
 
 
Charles Mills
Guest
Posts: n/a
 
      05-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have problem with the code only with 64 bit big endian machine.
> i have a member size_t val1 in my structure. The size of size_t is
> 32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
> machines.
>
> I am passing address of this variable to a different function (where it
> accepts only pointer to unsigned int) unsigned int *. Basically passing
> a unsigned long pointer instead of unsigned int pointer. I think the
> value is getting truncated while dereferencing int *. In this case how
> do I make my code portable? Can functions like htonl/ntohl will help?
> or casting could be of any help?
>
> Thanks,
> Suresh


Do the circumstances allow the following?

struct {
size_t val1;
} my_struct;
int tmp = 0;
some_function_that_takes_a_pointer_to_an_int(&tmp) ;
if (tmp < 0) /* do something */ ;
my_struct.val1 = tmp;

I assume some_function_that_takes_a_pointer_to_an_int() will be
dereferencing the pointer passed to it later. In this case you still
have some options, but it depends on how you are going to be using the
structure.

Charlie

 
Reply With Quote
 
sureshjayaram@gmail.com
Guest
Posts: n/a
 
      05-12-2006
It's like..

struct {
size_t val1; // can be unsigned long in 64 bit
} my_struct;
struct my_struct val;

func(&val.val1);

void func(unsigned int *)
{
// val1 can be deferenced
}

Is there workaround in case of 64bit?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-12-2006
(E-Mail Removed) wrote:
> It's like..
>

What's like? Please quote what you are replying to, see
<http://cfaj.freeshell.org/google/>.

> struct {
> size_t val1; // can be unsigned long in 64 bit
> } my_struct;
> struct my_struct val;
>
> func(&val.val1);
>
> void func(unsigned int *)
> {
> // val1 can be deferenced
> }
>
> Is there workaround in case of 64bit?
>

You should get a diagnostic as you are passing a pointer to an
incompatible type.

--
Ian Collins.
 
Reply With Quote
 
sureshjayaram@gmail.com
Guest
Posts: n/a
 
      05-12-2006
Iam getting warning message only in 64bit m/c warning: passing argument
... from incompatible pointer type
How to solve this?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-12-2006
(E-Mail Removed) wrote:
> Iam getting warning message only in 64bit m/c warning: passing argument
> ... from incompatible pointer type
> How to solve this?
>

Solve what? I asked you politely to quote context. If you don't want
to help me, I won't help you.

--
Ian Collins.
 
Reply With Quote
 
sureshjayaram@gmail.com
Guest
Posts: n/a
 
      05-12-2006
Sorry I did't notice the first part of the previous message. I should
have provided the context in the first place.

I have problem with the code only with 64 bit big endian machine.
i have a member size_t val1 in my structure. The size of size_t is
32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
machines.

I am passing address of this variable to a different function (where it

accepts only pointer to unsigned int) unsigned int *. Basically passing

a unsigned long pointer instead of unsigned int pointer. I think the
value is getting truncated while dereferencing int *. In this case how
do I make my code portable? Can functions like htonl/ntohl will help?
or casting could be of any help?

struct {
size_t val1; // can be unsigned long in 64 bit
} my_struct;
struct my_struct val;

func(&val.val1);

void func(unsigned int *val1)
{
// val1 can be deferenced here
}

When I dereference I will be getting only the truncated 32 bits which
is giving wrong value.

The below example when run on 3 different arch (i386, ia64 & s390x)
provided different output which confirms the same

#include <stdio.h>

int main()
{
size_t i = 1524;
unsigned int *k = &i;

printf("i is %d\n*k is %d\n", i, *k);
}

On i386
1524
1524

on ia64
1524
1524

on s390x (big endian)
1524
0

 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      05-12-2006
>I have problem with the code only with 64 bit big endian machine.
>i have a member size_t val1 in my structure.


>The size of size_t is
>32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
>machines.


I'm not convinced this generalization is valid for all 32-bit or
64-bit machines.

>I am passing address of this variable to a different function (where it
>
>accepts only pointer to unsigned int) unsigned int *.


This is your problem. Fix it. There are several approaches:

(1) Fix the function to accept a pointer to size_t.
(2) Copy the size_t into an unsigned int (by assignment), and pass a
pointer to *THAT*. If the function might change the value pointed
at, copy it back afterwards. However, you're going to have trouble
if the value won't FIT in an unsigned int, which itself presents problems.
(3) Change the type of the member to unsigned int.

>Basically passing
>a unsigned long pointer instead of unsigned int pointer. I think the
>value is getting truncated while dereferencing int *. In this case how
>do I make my code portable?


Fix the type mismatch so the types no longer mismatch.

>Can functions like htonl/ntohl will help?

No.

>or casting could be of any help?


Casting of *pointers* won't help here. Casting of values
might, but that's not what you are passing.

Gordon L. Burditt
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-12-2006
Gordon Burditt wrote:
>>I have problem with the code only with 64 bit big endian machine.
>>i have a member size_t val1 in my structure.

>
>
>>The size of size_t is
>>32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
>>machines.

>
>
> I'm not convinced this generalization is valid for all 32-bit or
> 64-bit machines.
>

size_t has to be able to hold the largest size value, so it has to be 64
bit on a 64 bit system.
>
>>I am passing address of this variable to a different function (where it
>>
>>accepts only pointer to unsigned int) unsigned int *.

>
>
> This is your problem. Fix it. There are several approaches:
>
> (1) Fix the function to accept a pointer to size_t.
> (2) Copy the size_t into an unsigned int (by assignment), and pass a
> pointer to *THAT*. If the function might change the value pointed
> at, copy it back afterwards. However, you're going to have trouble
> if the value won't FIT in an unsigned int, which itself presents problems.
> (3) Change the type of the member to unsigned int.
>

Agreed, 1 is the preferred solution.

--
Ian Collins.
 
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
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
endianness and 64-bit machines bob_jenkins@burtleburtle.net C Programming 7 05-20-2006 01:46 PM
Bit shifts and endianness gamehack C Programming 72 01-11-2006 06:01 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments