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

  • Home
  • SEARCH
  • 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 8387659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:16:27+00:00 2026-06-09T18:16:27+00:00

Suppose I use a item of an array inside a loop, like INS *in;

  • 0

Suppose I use a item of an array inside a loop, like

INS *in;
for (int i = 0; i < LEN; i++) {
    in = g_ins[i];
    if (strcmp(in->mne, str) == 0)
        return in;
}

If i refer each reference using the index, this is optimized by the compiler to something like above, or it really access each index again and again?

for (int i = 0; i < LEN; i++) {
    if (strcmp(g_ins[i]->mne, str) == 0)
        return g_ins[i];
}
  • 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-09T18:16:29+00:00Added an answer on June 9, 2026 at 6:16 pm

    You can try it, if in doubt. For example, put your two pieces of code into functions:

    INS * f1 (INS *g_ins, const char * str)
    {
        for (int i = 0; i < LEN; i++) {
            INS *in = &g_ins[i];
            if (strcmp(in->mne, str) == 0)
                return in;
        }
        return NULL;
    }
    
    INS * f2 (INS *g_ins, const char * str)
    {
        for (int i = 0; i < LEN; i++) {
            if (strcmp(g_ins[i].mne, str) == 0)
                return &g_ins[i];
        }
        return NULL;
    }
    

    and then compile them with -O2 and -S (eg. gcc -std=c99 -O2 -S -Wall code.c -o code.s). -O2 sets the optimisation level and -S tells the compiler to produce the assembler code and stop.

    Then look at the assembler code.

    Even if you dont understand assembler very well, you should be able to get an idea of the relative sizes of the two functions. In the case of your functions, they compile to the same code.

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

Sidebar

Related Questions

Suppose Item A contains the link Item B would like to use as the
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
I use foreach to loop the array below and then making the order number
Suppose I have ten list item, in that case I can use both div
Suppose I use a custom :format to implement a gadget-oriented version of my site.
Suppose both parent and child use one pipe for writing and reading means when
Suppose I'm designing a robot that can pickup various tools and use them. I
Suppose I have 2 office locations which I want to use for my contact
Suppose I have a Window with TextBoxes I want to use the values. Right
Suppose I have a program that initializes a global variable for use by threads,

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.