Endianess-check with increased difference compared to the result for a small code

Hello everyone. I am doing a preliminary check to see if my system is large or cowardly. In LInux, by default it should be low-threaded, but I just wanted to double check. I used 2 approaches

  • using endian forward support
  • using the code i found on the internet

I used the following static statement

BOOST_STATIC_ASSERT(!BIG_ENDIAN);

which fails during compilation, so though ... mmxx ... is my system a big endian? This is the error that I have

error: invalid application of ‘sizeof’ to incomplete type 
boost::STATIC_ASSERTION_FAILURE<false>’ 

If I run the test using some code similar to the one below, it confirms that the system has a low-drive character. Do you know what I'm doing wrong and am using Boost macro correctly?

bool is_big_endian_v3 () {

    long x = 0x34333231;
    char *y = (char *) &x;

    if(std::strncmp(y,"1234",4)){
        printf("Big Endian");
        return true;
    }else{
        printf("Little Endian");
        return false;
    }
    std::runtime_error ex("I cannot be here");
    throw ex;

}
+3
2

BIG_ENDIAN Boost.

#include <boost/detail/endian.hpp>

, BOOST_BIG_ENDIAN, BOOST_LITTLE_ENDIAN BOOST_PDP_ENDIAN. , :

BOOST_STATIC_ASSERT(!defined(BOOST_BIG_ENDIAN)); 

, :

BOOST_STATIC_ASSERT(defined(BOOST_LITTLE_ENDIAN)); 

Edit:

, ,

#if !defined(BOOST_BIG_ENDIAN) 
BOOST_STATIC_ASSERT(false);
#endif

. .

+4

Boost , ( 2019 .) :

#include <boost/predef/other/endian.h>
#include <stdlib.h>
#include <iostream>

int main()
{
#if BOOST_ENDIAN_BIG_BYTE
    std::cout << "Big endian." << std::endl;
#elif BOOST_ENDIAN_LITTLE_BYTE
    std::cout << "Little endian." << std::endl;
#else
    std::cout << "Unknown endian." << std::endl;
#endif
    exit(EXIT_SUCCESS);
}
0

Source: https://habr.com/ru/post/1778295/


All Articles