473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

reinterpret_cast 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_cast is dumping the core in solaris

*reinterpret_cast< ValueType* >( mNextWriteAddress ) = 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 2646
tk*********@gmail.com wrote:
Hi all,

reinterpret_cast is dumping the core in solaris

*reinterpret_cast< ValueType* >( mNextWriteAddress ) = 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 mNextWriteAdress is not on an alignment boundary, the
application normally terminates with a SIGBUS signal. Check, if it is a
SIGBUS and look if mNextWriteAdress 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 mNextWriteAdress is an aligned address or no-aligned?

Thanks in Advance

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

reinterpret_cast is dumping the core in solaris

*reinterpret_cast< ValueType* >( mNextWriteAddress ) = 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 mNextWriteAdress is not on an alignment boundary, the
application normally terminates with a SIGBUS signal. Check, if it is a
SIGBUS and look if mNextWriteAdress is an aligned adress.

If that is not the case, you should present a bit more code.
lg,
Mike
Sep 15 '06 #3
tk*********@gmail.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 mNextWriteAdress 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_cast<unsigned long>(ptr) % 4) == 0

In your example

*reinterpret_cast< ValueType* >( mNextWriteAddress ) = aValue;

mNextWriteAddress could be anything, so the expression

(reinterpret_cast<unsigned long>( mNextWriteAddress) %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
mNextWriteAddress is aligned.
For example on a 4-Byte alignment (untested code!):

// convert to some pointer to byte-size
uint8_t* ptr = reinterpret_cast<uint8_t*>(mNextWriteAddress);
// align to 4 byte boundary
ptr += (ptr % 4 ? (4 - ptr % 4) : 0);
// set value
*reinterpret_cast< 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*********@gmail.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 mNextWriteAdress 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_cast<unsigned long>(ptr) % 4) == 0

In your example

*reinterpret_cast< ValueType* >( mNextWriteAddress ) = aValue;

mNextWriteAddress could be anything, so the expression

(reinterpret_cast<unsigned long>( mNextWriteAddress) %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
mNextWriteAddress is aligned.
For example on a 4-Byte alignment (untested code!):

// convert to some pointer to byte-size
uint8_t* ptr = reinterpret_cast<uint8_t*>(mNextWriteAddress);
// align to 4 byte boundary
ptr += (ptr % 4 ? (4 - ptr % 4) : 0);
// set value
*reinterpret_cast< 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
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. ...
2
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...
7
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....
27
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...
13
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...
12
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...
2
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
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.