Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6938605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:30:52+00:00 2026-05-27T12:30:52+00:00

Please consider the following code: float float_value = x; // x is any valid

  • 0

Please consider the following code:

float float_value = x; // x is any valid float value
int int_value = 0;
size_t size = sizeof(int) < sizeof(float) ? sizeof(int) : sizeof(float);
memcpy(&int_value, &float_value, size);

As far as i know this could result in an trap representation. My questions:

  1. Is that true?
  2. If not, why?
  3. If not, is there another way avoiding a possible trap representation?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:30:52+00:00Added an answer on May 27, 2026 at 12:30 pm

    The sanctioned way which won’t produce any trap representation is

    unsigned char obj[sizeof float];
    memcpy(obj, &float_value, sizeof float);
    

    Then you can use the bytes of the object representation to build your desired int.

    But using fixed-width integers as mentioned by Stephen Canon is better – unless you have a weird float size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Please consider the following code: vector<int> myVector; myVector.push_back(10); myVector.erase(myVector.end()); This code compiles and runs
Please consider the following code: typedef struct { int type; } object_t; typedef struct
please consider the following code: typedef struct Person* PersonRef; struct Person { int age;
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.