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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:48:07+00:00 2026-06-15T00:48:07+00:00

I’m new to c++ (and SO) so sorry if this is obvious. I’ve started

  • 0

I’m new to c++ (and SO) so sorry if this is obvious.

I’ve started using temporary arrays in my code to cut down on repetition and to make it easier to do the same thing to multiple objects. So instead of:

MyObject obj1, obj2, obj3, obj4;

obj1.doSomming(arg);
obj2.doSomming(arg);
obj3.doSomming(arg);
obj4.doSomming(arg);

I’m doing:

MyObject obj1, obj2, obj3, obj4;
MyObject* objs[] = {&obj1, &obj2, &obj3, &obj4};

for (int i = 0; i !=4; ++i)
    objs[i]->doSomming(arg);

Is this detrimental to performance? Like, does it cause unnecessary memory allocation? Is it good practice? Thanks.

  • 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-15T00:48:09+00:00Added an answer on June 15, 2026 at 12:48 am

    In general you just shouldn’t worry about performance at this level. Very often the things that end up being performance problems turn out to be completely different from what you might expect, especially if you don’t have a lot of experience with performance optimization.

    You should always think about writing clear code first, and if performance matters then you should think about it in algorithmic terms (i.e., big-O). Then you should measure performance and let that guide where you spend your effort at optimization.


    Now, you can make the code even clearer and more straightforward if you avoid the intermediate array and just use an array for the original objects:

    MyObject obj[4];
    
    for (int i = 0; i !=4; ++i)
      objs[i].doSomming(arg);
    

    But no, an optimizing compiler should generally have no problem with this.

    For example, if I take the code:

    struct MyObject {
        void doSomming() {
            std::printf("Hello\n");
        }
    };
    
    void foo1() {
        MyObject obj1, obj2, obj3, obj4;
    
        obj1.doSomming();
        obj2.doSomming();
        obj3.doSomming();
        obj4.doSomming();
    }
    
    void foo2() {
        MyObject obj1, obj2, obj3, obj4;
        MyObject* objs[] = {&obj1, &obj2, &obj3, &obj4};
    
        for (int i = 0; i !=4; ++i)
            objs[i]->doSomming();
    }
    
    void foo3() {
        MyObject obj[4];
    
        for (int i = 0; i !=4; ++i)
            obj[i].doSomming();
    }
    

    and produce LLVM IR (because it’s more compact than actual assembly), I get the following with -O3.

    define void @_Z4foo1v() nounwind uwtable ssp {
    entry:
      %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i1 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i2 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i3 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      ret void
    }
    
    define void @_Z4foo2v() nounwind uwtable ssp {
    entry:
      %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i.1 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i.2 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i.3 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      ret void
    }
    
    define void @_Z4foo3v() nounwind uwtable ssp {
    entry:
      %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i.1 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i.2 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %puts.i.3 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      ret void
    }
    

    At -O3 the loop gets unrolled and the code is identical with the original version. With -Os the loops don’t get unrolled, but the the pointer indirection and even the arrays disappear because they’re not needed after inlining:

    define void @_Z4foo2v() nounwind uwtable optsize ssp {
    entry:
      br label %for.body
    
    for.body:                                         ; preds = %entry, %for.body
      %i.05 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
      %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %inc = add nsw i32 %i.05, 1
      %cmp = icmp eq i32 %inc, 4
      br i1 %cmp, label %for.end, label %for.body
    
    for.end:                                          ; preds = %for.body
      ret void
    }
    
    define void @_Z4foo3v() nounwind uwtable optsize ssp {
    entry:
      br label %for.body
    
    for.body:                                         ; preds = %entry, %for.body
      %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
      %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
      %inc = add nsw i32 %i.03, 1
      %cmp = icmp eq i32 %inc, 4
      br i1 %cmp, label %for.end, label %for.body
    
    for.end:                                          ; preds = %for.body
      ret void
    }
    
    • 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'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.