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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:34:40+00:00 2026-05-21T02:34:40+00:00

EDIT : I’m sorry for my mistakes in my code snippets, now I see

  • 0

EDIT: I’m sorry for my mistakes in my code snippets, now I see both outputs were same. Below is an edited version.

Let’s say I have a structure:

typedef struct
{
    char m[5];
    char f[6];
} COUPLE;

And a file containing just phrase RomeoJuliet that I read into an array:

char *data = malloc(11);
FILE *f = fopen("myfile", "rb");
fread(data, 1, 11, f);
fclose(f);

I always use this code when I need to fill my structure from a byte array:

COUPLE titanic;
memcpy(&titanic, data, sizeof(data));
printf("%s and %s", titanic.m, titanic.f);

This works fine, but really my byte array can be very big, so below is my attempt to optimize my code.

COUPLE *titanic = (COUPLE *)data;
printf("%s and %s", titanic->m, titanic->f);

So, my questions are:

  1. (obsolete) Why do I get different outputs?
  2. (obsolete) How can I fill a structure just by casting from an array?
  3. Should I avoid this kind of optimization?
  4. Are there possible pitfalls in it?
  • 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-21T02:34:41+00:00Added an answer on May 21, 2026 at 2:34 am

    When made my comment to your question, I didn’t have the time to elaborate, so here’s an attempt to answer. The code has changed since, and I’m not sure I would add that comment to the code as it is now.

    Nevertheless, the underlying reason that made me add that comment still stands: Unless you measured and found that the code in question does indeed have a significant negative impact on performance, don’t attempt to optimize. Instead, strive to make your code as readable as possible.

    I seriously doubt that copying the data in memory will have a significant performance impact after copying them from the disk into memory. However, since your code as provided makes assumptions about the struct’s layout in memory anyway, directly reading into the struct wouldn’t really make the code less readable (or less vulnerable to changes to that layout):

    COUPLE titanic;
    FILE *f = fopen("myfile", "rb");
    fread(&titanic, sizeof(titanic), 1, f);
    

    Or, if you indeed have an array, read into the array directly:

    COUPLE titanic[SIZE];
    FILE *f = fopen("myfile", "rb");
    fread(&titanic, sizeof(titanic), SIZE, f);
    

    Depending on SIZE, the latter could indeed make a potentially huge difference in performance. Accessing the disk for bigger chunks is, in general, faster that doing it for smaller chunks. (Although disk caching might alleviate that.)

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

Sidebar

Related Questions

No related questions found

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.