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 923175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:09:06+00:00 2026-05-15T19:09:06+00:00

I used the following piece of code to read data from files as part

  • 0

I used the following piece of code to read data from files as part of a larger program.

double data_read(FILE *stream,int code) {
        char data[8];
        switch(code) {
        case 0x08:
            return (unsigned char)fgetc(stream);
        case 0x09:
            return (signed char)fgetc(stream);
        case 0x0b:
            data[1] = fgetc(stream);
            data[0] = fgetc(stream);
            return *(short*)data;
        case 0x0c:
            for(int i=3;i>=0;i--)
                data[i] = fgetc(stream);
            return *(int*)data;
        case 0x0d:
            for(int i=3;i>=0;i--)
                data[i] = fgetc(stream);
            return *(float*)data;
        case 0x0e:
            for(int i=7;i>=0;i--)
                data[i] = fgetc(stream);
            return *(double*)data;
        }
        die("data read failed");
        return 1;
    }

Now I am told to use -O2 and I get following gcc warning:
warning: dereferencing type-punned pointer will break strict-aliasing rules

Googleing I found two orthogonal answers:

  • Concluding: there’s no need to worry; gcc tries to be more law obedient than
    the actual law.

vs

  • So basically if you have an int* and a float* they are not allowed to point to the same memory location. If your code does not respect this, then the compiler’s optimizer will most likely break your code.

In the end I don’t want to ignore the warnings. What would you recommend?

[update] I substituted the toy example with the real function.

  • 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-15T19:09:06+00:00Added an answer on May 15, 2026 at 7:09 pm

    It looks a lot as if you really want to use fread:

    int data;
    fread(&data, sizeof(data), 1, stream);
    

    That said, if you do want to go the route of reading chars, then reinterpreting them as an int, the safe way to do it in C (but not in C++) is to use a union:

    union
    {
        char theChars[4];
        int theInt;
    } myunion;
    
    for(int i=0; i<4; i++)
        myunion.theChars[i] = fgetc(stream);
    return myunion.theInt;
    

    I’m not sure why the length of data in your original code is 3. I assume you wanted 4 bytes; at least I don’t know of any systems where an int is 3 bytes.

    Note that both your code and mine are highly non-portable.

    Edit: If you want to read ints of various lengths from a file, portably, try something like this:

    unsigned result=0;
    for(int i=0; i<4; i++)
        result = (result << 8) | fgetc(stream);
    

    (Note: In a real program, you would additionally want to test the return value of fgetc() against EOF.)

    This reads a 4-byte unsigned from the file in little-endian format, regardless of what the endianness of the system is. It should work on just about any system where an unsigned is at least 4 bytes.

    If you want to be endian-neutral, don’t use pointers or unions; use bit-shifts instead.

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

Sidebar

Related Questions

I've used following code for parsing XML file in c++. http://www.codeproject.com/Articles/176236/Parsing-an-XML-file-in-a-C-C-program . Now I
I found following piece of code written as a shell script to read from
The following piece of code is used to print the time in the logs:
I have used following code to create a simple PDF file. It executes fine
I need some explanation about the following piece of code: It is used for
I would like to move the following piece of code from a c# aspx.cs
I used the following piece of code in the service to debug the service
I am using the following piece of code to get results from DB as
Hey folks, I have the following piece of code from C++. for (int i=0;
I have the following piece of code: $item_list = array(); $item_list['PENCIL'] = Utility used

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.