473,800 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

64 bit Endianness issue

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

May 11 '06 #1
17 3010
In article <11************ **********@q12g 2000cwa.googleg roups.com>,
<su***********@ gmail.com> 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
May 11 '06 #2
su***********@g mail.com 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_t hat_takes_a_poi nter_to_an_int( &tmp);
if (tmp < 0) /* do something */ ;
my_struct.val1 = tmp;

I assume some_function_t hat_takes_a_poi nter_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

May 11 '06 #3
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?

May 12 '06 #4
su***********@g mail.com 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.
May 12 '06 #5
Iam getting warning message only in 64bit m/c warning: passing argument
... from incompatible pointer type
How to solve this?

May 12 '06 #6
su***********@g mail.com 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.
May 12 '06 #7
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

May 12 '06 #8
>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
May 12 '06 #9
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.
May 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
674
by: pramod | last post by:
Two different platforms communicate over protocols which consist of functions and arguments in ascii form. System might be little endian/big endian. It is possible to format string using sprintf and retreive it using sscanf. Each parameter has a delimiter, data type size is ported to the platform, and expected argument order is known. Is this approach portable w.r.t. endianess ?
3
496
by: kelvSYC | last post by:
Are there any endianness concerns in C++, or does the compiler take care of those details? I ask because I'm not sure if code such as the following have consistent behavior on all platforms. typedef unsigned int u32; // sizeof(int) == 4 typedef unsigned char u8; u8 array = { 0x01, 0x23, 0x45, 0x67 }; *((u32*) array) = 0x89ABCDEF;
26
11677
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
2
4031
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
72
11630
by: gamehack | last post by:
Hi all, I was thinking today, suppose we have the number n = 0xAB 0xFF which is equivalent to 44031 in decimal. In big endian it will be stored as 10101011 11111111 but in little endian it will be 11111111 10101011 If we then apply a bit shift n << 2; that would give us completely
18
14047
by: friend.05 | last post by:
Code to check endianness of machine
18
2835
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass structures using sockets across platforms, we need to take care of Endianness issues at the application level. But for example, for the code using bitwise AND to figure out if a number is odd or even, how does C know the LSB position?
5
6819
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. Accordingly, if i need the program to work fine in a little-endian system. I understand that the code needs to be changed. ( I couldn't find any statement in C90 about endianness, hence i'm assuming that c programs are not portable if the endianness...
19
6937
by: perry.yuan | last post by:
How could I determine the endianness of my compile environment at compile time, instead of run time? I need a macro ("some_expression"), i.e. #if some_expression #define TARGET_IS_LITTLE_ENDIAN #else #define TARGET_IS_BIG_ENDIAN No way or some way? TIA.
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.