Hi,
I am trying to determine the endianness of the system as follows:
int i = 1;
bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is
valid, according to the Standard.
thanks!
--
jb
(reply address in rot13, unscramble first) 13 3093
signed char or unsigned char?
ben Hi,
I am trying to determine the endianness of the system as follows:
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard.
thanks! -- jb
(reply address in rot13, unscramble first)
"ben" <be******@hotma il.com> wrote in message
news:42******** **************@ news.optusnet.c om.au int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
signed char or unsigned char?
On my system signed, but why does this matter? I am testing against
0 ..
--
jb
(reply address in rot13, unscramble first)
"Jakob Bieling" <ar************ ****@rot13.com> skrev i en meddelelse
news:d4******** *****@news.t-online.com... Hi,
I am trying to determine the endianness of the system as follows:
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard.
thanks! -- jb
(reply address in rot13, unscramble first)
This is perfectly valid - just go ahead. The signedness of char is
irrelevant in this respect.
/Peter
"Jakob Bieling" <ar************ ****@rot13.com> wrote in message
news:d4******** *****@news.t-online.com... int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard.
It is not. There is no requirement that int* and char* be represented in
compatible ways.
In fact, I can't even think of any requirement that the bits in an int be
stored in any particular order. If an implementation decided to store all
the even-numbered bits together, and then all the odd-numbered bits, I don't
think there would be anything wrong with that.
"Andrew Koenig" <ar*@acm.org> wrote in message
news:IH******** ************@bg tnsc04-news.ops.worldn et.att.net "Jakob Bieling" <ar************ ****@rot13.com> wrote in message news:d4******** *****@news.t-online.com...
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard.
It is not. There is no requirement that int* and char* be represented in compatible ways.
In fact, I can't even think of any requirement that the bits in an int be stored in any particular order. If an implementation decided to store all the even-numbered bits together, and then all the odd-numbered bits, I don't think there would be anything wrong with that.
Guess I have to be more exact. Assuming the implementation uses
either the big- or the little-endian byte order and the binary
numeration system (as it 'shall' be used, according to 3.9.1/7).
Sticking to those restrictions, I assume the above code has still
unspecified behaviour, as your first point still stands, right? So I
came up with this:
char tmp [sizeof (int)];
int* i = new (tmp) int (1);
bool is_little = tmp [0] != 0;
Can I be sure of reading the memory used by the int, by accessing
'tmp' (still assuming that we have an implementation that conforms to
the restrictions I made above)?
Also, I was wondering, if I would have to call the d'tor there,
since it is just an int?
thanks!
--
jb
(reply address in rot13, unscramble first)
"Andrew Koenig" <ar*@acm.org> skrev i en meddelelse
news:IH******** ************@bg tnsc04-news.ops.worldn et.att.net... "Jakob Bieling" <ar************ ****@rot13.com> wrote in message news:d4******** *****@news.t-online.com...
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard.
It is not. There is no requirement that int* and char* be represented in compatible ways.
In fact, I can't even think of any requirement that the bits in an int be stored in any particular order. If an implementation decided to store all the even-numbered bits together, and then all the odd-numbered bits, I don't think there would be anything wrong with that.
I read the question as "is this reinterpret_cas t" legal and well-defined in
std C++. In that case the answer is a clear YES. If the question is in the
above code can determine all types of endianness, those in existence now as
well as any conceivable ones, then of course the answer is NO.
/Peter
Jakob Bieling wrote: Hi,
I am trying to determine the endianness of the system as follows:
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard.
I think this does the task:
#include <cstring>
#include <limits>
#include <vector>
bool IsLittleEndian( )
{
using namespace std;
unsigned int num= 0;
unsigned char buffer[sizeof(num)];
memcpy(buffer, &num, sizeof(num));
// Stores indices to modified bytes of num
unsigned indices[2];
const unsigned char *p= reinterpret_cas t<unsigned char *>(&num);
num= 1;
for(unsigned i=0; i<sizeof(num); ++i)
{
if(p[i]!= buffer[i])
{
indices[0]= i;
break;
}
}
num+= numeric_limits< unsigned char>::max();
for(unsigned i=0; i<sizeof(num); ++i)
{
if(p[i]!= buffer[i])
{
indices[1]= i;
break;
}
}
if(indices[0]< indices[1])
return true;
else
return false;
}
--
Ioannis Vranos http://www23.brinkster.com/noicys
On Sat, 30 Apr 2005 13:26:01 GMT, "Andrew Koenig" <ar*@acm.org> wrote: "Jakob Bieling" <ar************ ****@rot13.com> wrote in message news:d4******* ******@news.t-online.com...
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard. It is not. There is no requirement that int* and char* be represented in compatible ways.
Is "convertabl e so that one can always address the same memory
location as the other" a fair definition of what you mean by
"compatible "?
In fact, I can't even think of any requirement that the bits in an int be stored in any particular order. If an implementation decided to store all the even-numbered bits together, and then all the odd-numbered bits, I don't think there would be anything wrong with that.
Such as an Intercal machine? That's a scary thought.
Kanenas
On Sat, 30 Apr 2005 13:26:01 GMT, "Andrew Koenig" <ar*@acm.org> wrote: "Jakob Bieling" <ar************ ****@rot13.com> wrote in message news:d4******* ******@news.t-online.com...
int i = 1; bool is_little = *reinterpret_ca st <char*> (&i) != 0;
But now I was asking myself, if this use of reinterpret_cas t is valid, according to the Standard. It is not. There is no requirement that int* and char* be represented in compatible ways.
Is "convertabl e so that a char* can always be made to point to the
same memory location as an int*" a fair definition of what you mean by
"compatible "?
In fact, I can't even think of any requirement that the bits in an int be stored in any particular order. If an implementation decided to store all the even-numbered bits together, and then all the odd-numbered bits, I don't think there would be anything wrong with that.
Such as an Intercal machine? That's a scary thought.
Kanenas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Aman |
last post by:
Hi,
wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3
trying to see the byte order of an int or short int by converting to
char* . doesn't work . the char* cpt doesn't seem to be initialized !!.
why would that be ?
int main()
{
|
by: johny smith |
last post by:
Can someone please give me some good reference site/information for
understanding when and why I would use
1.) const_cast
Don't know why you would do this?
2.) reinterpret_cast.
I have used this a little when I converted a primitive to a class, but
would like to see other applications.
|
by: E. Robert Tisdale |
last post by:
In the context of the comp.lang.c newsgroup,
the term "undefined behavior" actually refers to
behavior not defined by the ANSI/ISO C 9 standard.
Specifically, it is *not* true that "anything can happen"
if your C code invokes "undefined behavior".
Behavior not defined by the ANSI/ISO C 9 standard
may be defined by some other standard (i.e. POSIX) or
it may be defined by your compiler,
your operating system or your machine architecture.
|
by: Alex Vinokur |
last post by:
Should 'delete below (after casting to void*)' work fine?
------------------------------------------
char* p1 = new (nothrow) char;
if (p1 == 0) return;
void* p2 = reinterpret_cast<void*> (p1);
delete p2;
------------------------------------------
Alex Vinokur
|
by: Lucy Ludmiller |
last post by:
I have two classes:
class base { ... };
and
class derived : public base { ... };
I have a function :
| |
by: Nate Barney |
last post by:
I have:
// base class for Vector and Matrix
template <unsigned N,typename Value=float>
class NonScalar
{
public:
Value *ptr() { return e; }
const Value *ptr() const { return e; }
|
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...
|
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;
|
by: saurabh |
last post by:
I am trying to understand reinterpret_cast,here is what i tried,
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int x=1;
int* y=&x;
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
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
| |