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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:03:24+00:00 2026-06-02T14:03:24+00:00

A very often executed piece of code has the following calculation : long *lp

  • 0

A very often executed piece of code has the following calculation :

long *lp
char *ep, *cp
...
tlen = (ep - cp) / sizeof (*lp);

Would changing this to:

long *lp
char *ep, *cp
...
tlen = (ep - cp) / sizeof (long);

result in any more efficiency (since the sizeof of calculated at compile time) or would a modern compiler handle this at compile time already. what does gcc do ?

  • 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-06-02T14:03:34+00:00Added an answer on June 2, 2026 at 2:03 pm

    The sizeof operator is always a compile time evaluated construct 0, so there is no difference.

    The fragment …

     tlen = (ep - cp) / sizeof (*lp);
    

    will therefore be transformed into something not unlike …

     tlen = (ep - cp) / 4;
    

    (assuming that sizeof(long)==4 1.), with optimizations applied the next transformation is probably …

     tlen = (ep - cp) >> 2;
    

    More optimizations to come, of course; it’s just a demonstration of a possible consequence of it being a compile time construct 0.

    I would always prefer "sizeof(_var-name_)" over sizeof(_typename_), as its more generic and doesn’t require manual adjustment when you change the type of the variable (except when you change from array to pointer).


    0: Except for variable length arrays.

    1: Size differs with platform

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

Sidebar

Related Questions

I get this error in xml file very often. here is the code in
I have the following line of code called very often: var configValue = System.Configuration.ConfigurationManager.AppSettings[ConfigValueKey];
I'm finding this kind of overriding after a new in java code very often
Very often when Visual Studio generates code (for example generating an event handler stub)
I have the following code which lets me execute a workflow. This could be
I see this very often in the UK where websites will let you signup
I've been working on a piece of code recently where performance is very important,
Very often I want to convert a list wherein each index has identical element
sounds like very easy question but very often I am confused by this. Please
I very often see example of this form: template <typename T, typename U> auto

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.