473,668 Members | 2,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reinterpret_cas t issue on solaris

Hi all,
This is regarding the issue I am facing while porting the my
application from SuSe Linux to
Sun Solaris.

The Existing system details:
Itanium boxes 1mhz 4 processor 8 gb machines with SuSe Linux

$ uname -a
Linux longrtedged01 2.4.21-215-itanium2-smp #1 SMP Mon Apr 26 16:28:29
UTC 2004 ia64

The New system Details:
Fujitsu 850 machines 2mhz 16 Sparc processor 32 GB machines running
solaris 10
$ uname -a
SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M

reinterpret_cas t is dumping the core in solaris

*reinterpret_ca st< ValueType* >( mNextWriteAddre ss ) = aValue;

Here aValue is a Template ValueType which can be either int or long or
double. i.e

ValueType aValue;

Same thing is perfectly working in Linux.

I use g++ compiler
g++ -v
Reading specs from
/usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs
Configured with: ../configure --with-as=/usr/ccs/bin/as
--with-ld=/usr/ccs/bin/ld --disable-nls
Thread model: posix
gcc version 3.3.2

Help me sort out the issue

Sep 14 '06 #1
4 2661
tk*********@gma il.com wrote:
Hi all,

reinterpret_cas t is dumping the core in solaris

*reinterpret_ca st< ValueType* >( mNextWriteAddre ss ) = aValue;

Here aValue is a Template ValueType which can be either int or long or
double. i.e
Hard to say, but my first guess is an alignment problem. The Sparc
processors cannot access types which need alignment on a non-aligned
address. So if mNextWriteAdres s is not on an alignment boundary, the
application normally terminates with a SIGBUS signal. Check, if it is a
SIGBUS and look if mNextWriteAdres s is an aligned adress.

If that is not the case, you should present a bit more code.
lg,
Mike
Sep 14 '06 #2
Thanks Michael for the message -
It is actually a SIGBUS.
And I could not quite understand about the alignment and non-alignment
concept that you have written. Can you please elaborate, so that I
could check mNextWriteAdres s is an aligned address or no-aligned?

Thanks in Advance

Michael Oswald wrote:
tk*********@gma il.com wrote:
Hi all,

reinterpret_cas t is dumping the core in solaris

*reinterpret_ca st< ValueType* >( mNextWriteAddre ss ) = aValue;

Here aValue is a Template ValueType which can be either int or long or
double. i.e

Hard to say, but my first guess is an alignment problem. The Sparc
processors cannot access types which need alignment on a non-aligned
address. So if mNextWriteAdres s is not on an alignment boundary, the
application normally terminates with a SIGBUS signal. Check, if it is a
SIGBUS and look if mNextWriteAdres s is an aligned adress.

If that is not the case, you should present a bit more code.
lg,
Mike
Sep 15 '06 #3
tk*********@gma il.com wrote:
Thanks Michael for the message -
It is actually a SIGBUS.
And I could not quite understand about the alignment and non-alignment
concept that you have written. Can you please elaborate, so that I
could check mNextWriteAdres s is an aligned address or no-aligned?

Ok, as an example: lets assume aValue is a double and a double has a
sizeof(double) == 8. Let's assume further the Sparc has an alignment of
4 Bytes (the Sparcs I worked with had an alignment of 4 bytes, could be
different for yours...).

If you take the address of a double like

double d;
double* ptr = &d;

the ptr would (on this theoretical machine) be aligned to an 4 byte
boundary, this means the address must be divideable by 4 without
remainder (assuming this machine is a 32 Bit machine with 32 Bit
pointers and a long is 32 Bits) the following MUST hold:

(reinterpret_ca st<unsigned long>(ptr) % 4) == 0

In your example

*reinterpret_ca st< ValueType* >( mNextWriteAddre ss ) = aValue;

mNextWriteAddre ss could be anything, so the expression

(reinterpret_ca st<unsigned long>( mNextWriteAddre ss) %8) could be != 0
which results in the SIGBUS.

Of course this is machine specific, so your Sparc might have a different
alignment.

You can imagine it like this:

Address Value
0x0000 0x11 0x22 0x33 0x44
0x0004 0x00 0x00 0x00 0x00

at 0x0000 there would be an aligned 32 Bit value 0x11223344 (let's
silently forget the byte order issues) and on 0x004 the value woudl be
0. Both values can be accessed with a normal 32-Bit instruction

In your example this could happen:
0x0000 0x00 0x11 0x22 0x33
0x0004 0x44 0x00 0x00 0x00

So the value 0x11223344 would start on address 0x0001 and not on 0x0000,
but the 32-Bit instruction cannot access 32 Bits from 0x0001.
On Intel machines, this is normally no problem, a Sparc is a little pickier.

The solution would be either use a different design, or make sure, that
mNextWriteAddre ss is aligned.
For example on a 4-Byte alignment (untested code!):

// convert to some pointer to byte-size
uint8_t* ptr = reinterpret_cas t<uint8_t*>(mNe xtWriteAddress) ;
// align to 4 byte boundary
ptr += (ptr % 4 ? (4 - ptr % 4) : 0);
// set value
*reinterpret_ca st< ValueType* >( ptr ) = aValue;

With this code you have to be sure that reading and writing is
synchronous, so you'll have to do the same on reading. And, since there
will be some bytes skipped, these should be initialised before (with
e.g. 0). And always be aware, that this is machine specific and not
fully portable!
lg,
Michael
Sep 15 '06 #4
Thanks Michael - I would try with the way you said...

Michael Oswald wrote:
tk*********@gma il.com wrote:
Thanks Michael for the message -
It is actually a SIGBUS.
And I could not quite understand about the alignment and non-alignment
concept that you have written. Can you please elaborate, so that I
could check mNextWriteAdres s is an aligned address or no-aligned?


Ok, as an example: lets assume aValue is a double and a double has a
sizeof(double) == 8. Let's assume further the Sparc has an alignment of
4 Bytes (the Sparcs I worked with had an alignment of 4 bytes, could be
different for yours...).

If you take the address of a double like

double d;
double* ptr = &d;

the ptr would (on this theoretical machine) be aligned to an 4 byte
boundary, this means the address must be divideable by 4 without
remainder (assuming this machine is a 32 Bit machine with 32 Bit
pointers and a long is 32 Bits) the following MUST hold:

(reinterpret_ca st<unsigned long>(ptr) % 4) == 0

In your example

*reinterpret_ca st< ValueType* >( mNextWriteAddre ss ) = aValue;

mNextWriteAddre ss could be anything, so the expression

(reinterpret_ca st<unsigned long>( mNextWriteAddre ss) %8) could be != 0
which results in the SIGBUS.

Of course this is machine specific, so your Sparc might have a different
alignment.

You can imagine it like this:

Address Value
0x0000 0x11 0x22 0x33 0x44
0x0004 0x00 0x00 0x00 0x00

at 0x0000 there would be an aligned 32 Bit value 0x11223344 (let's
silently forget the byte order issues) and on 0x004 the value woudl be
0. Both values can be accessed with a normal 32-Bit instruction

In your example this could happen:
0x0000 0x00 0x11 0x22 0x33
0x0004 0x44 0x00 0x00 0x00

So the value 0x11223344 would start on address 0x0001 and not on 0x0000,
but the 32-Bit instruction cannot access 32 Bits from 0x0001.
On Intel machines, this is normally no problem, a Sparc is a little pickier.

The solution would be either use a different design, or make sure, that
mNextWriteAddre ss is aligned.
For example on a 4-Byte alignment (untested code!):

// convert to some pointer to byte-size
uint8_t* ptr = reinterpret_cas t<uint8_t*>(mNe xtWriteAddress) ;
// align to 4 byte boundary
ptr += (ptr % 4 ? (4 - ptr % 4) : 0);
// set value
*reinterpret_ca st< ValueType* >( ptr ) = aValue;

With this code you have to be sure that reading and writing is
synchronous, so you'll have to do the same on reading. And, since there
will be some bytes skipped, these should be initialised before (with
e.g. 0). And always be aware, that this is machine specific and not
fully portable!
lg,
Michael
Sep 18 '06 #5

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

Similar topics

17
12818
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. To get around this limitation on double, I'd like to keep the bits of the double the *same* but change its interpretation to long. I can use "&" on longs. I tried to use reinterpret_cast for this purpose, but it returned zero every time. double...
2
2434
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue was to assign a value to an array of pointers. The selection of array depends upon a condition and the void* ptr has to be casted to the correct type and inserted in the array.
7
10901
by: Peter | last post by:
I never used reinterpret_cast -- probably because I don't know what it means. Can somebody enlighten me? I looked into Stroustrup's "The annoted C++ reference manual" -- but this was no help. Can I assume that reinterpret_cast is not safe and should not be used? Does it always succeed even if the cast is garbage?
27
4321
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong type was the target of a cast. Of course with a reinterpret_cast nothing complains until the UB bites you in the ass. It seems to me that there ought to be a way to deal with these kinds of functions yet still retain some semblance of type...
13
1626
by: Howard Hinnant | last post by:
Hello, I'm writing a brief paper on LWG issue 206: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#206 It would help me immensely if I knew more about the current practice of several vendors. I am asking for volunteers to post the results of the following short program, along with the compiler (including version) which produced those results. To get the ball rolling, here are results
12
1763
by: ralpe | last post by:
Hi, I have a template class foo which stores pairs of Keys and Ts in a vector. Foo has an accessor function at(size_t) that returns a reference to the pair at the specified index. I do not want the users of foo to change the first value of a pair, so I want to return a reference to a pair<const Key, T>, just like std::map does. I cannot store pairs with a const first value in the vector because such pairs would not be assignable.
2
2955
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
2
2674
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when optimizing. #include <iostream> int main() { long D1 = 0xbcfbc4f0d9b65179; long D2 = 0xbcfbc4f0d9b65042;
0
1798
by: mg | last post by:
When make gets to the _ctypes section, I am getting the following in my output: building '_ctypes' extension creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes/libffi creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes/libffi/src
0
8893
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...
0
8797
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8583
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
7401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
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
5681
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();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.