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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:39:33+00:00 2026-05-24T15:39:33+00:00

I have a function and i’m accessing a struct’s members a lot of times

  • 0

I have a function and i’m accessing a struct’s members a lot of times in it.
What I was wondering about is what is the good practice to go about this?

For example:

struct s
{
    int x;
    int y;
}

and I have allocated memory for 10 objects of that struct using malloc.

So, whenever I need to use only one of the object in a function, I usually create (or is passed as argument) pointer and point it to the required object (My superior told me to avoid array indexing because it adds a calculation when accessing any member of the struct)

But is this the right way? I understand that dereferencing is not as expensive as creating a copy, but what if I’m dereferencing a number of times (like 20 to 30) in the function.

Would it be better if i created temporary variables for the struct variables (only the ones I need, I certainly don’t use all the members) and copy over the value and then set the actual struct’s value before returning?

Also, is this unnecessary micro optimization? Please note that this is for embedded devices.

  • 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-24T15:39:34+00:00Added an answer on May 24, 2026 at 3:39 pm

    First of all, indexing an array is not very expensive (only like one operation more expensive than a pointer dereference, or sometimes none, depending on the situation).

    Secondly, most compilers will perform what is called RVO or return value optimisation when returning structs by value. This is where the caller allocates space for the return value of the function it calls, and secretly passes the address of that memory to the function for it to use, and the effect is that no copies are made. It does this automatically, so

    struct mystruct blah = func();
    

    Only constructs one object, passes it to func for it to use transparently to the programmer, and no copying need be done.

    What I do not know is if you assign an array index the return value of the function, like this:

    someArray[0] = func();
    

    will the compiler pass the address of someArray[0] and do RVO that way, or will it just not do that optimisation? You’ll have to get a more experienced programmer to answer that. I would guess that the compiler is smart enough to do it though, but it’s just a guess.

    And yes, I would call it micro optimisation. But we’re C programmers. And that’s how we roll.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
I have function defined this way: def f1 (a, b, c = None, d
I have this code in my controller (admin): function save(){ $model = $this->getModel('mymodel'); if
i have function like this. function load($name, $arg1, $arg2, $arg3, $arg4){ $this->$name = new
I have function like this typedef vector<vector<string> > vecArray; vecArray dul(); vecArray dul() {
I have function as below: foo :: Int -> a -> [a] foo n
I have function called getdoctor () in asmx file I wannna call this webmethod

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.