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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:12:50+00:00 2026-06-05T15:12:50+00:00

for( int i = 0; i < lines; i++ ) { std::unique_ptr<BYTE[]> pLine( new

  • 0
for( int i = 0; i < lines; i++ ) {
    std::unique_ptr<BYTE[]> pLine( new BYTE[lineSize] );
    //Do stuff
}

Now, pLine is declared inside the loop because it’s only used in the loop body. However, wouldn’t allocating only once outside the loop reduce the amount of allocations performed (avoiding memory fragmentation)?

std::unique_ptr<BYTE[]> pLine( new BYTE[lineSize] );
for( int i = 0; i < lines; i++ ) {
    //Do stuff
}

I could believe the compiler would be able to optimize the first version easily if it knew lineSize stays the same throughout iterations; but it does change throughout function calls, so I can’t make it a constant.

I also think micro optimizations like this should be avoided until a performance problem is detected, so I guess I would stick with the first version. What do you guys think?

  • 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-05T15:12:51+00:00Added an answer on June 5, 2026 at 3:12 pm

    The question is really what is in the //do Stuff

    for( int i = 0; i < lines; i++ ) {
        std::unique_ptr<BYTE[]> pLine( new BYTE[lineSize] );
        //Do stuff
    }
    

    I would assume you are allocating the memory inside the loop to unique_ptr because somewhere in the //Do stuff section you are passing ownership of that memory to another object.

    This if you try and do this:

    std::unique_ptr<BYTE[]> pLine( new BYTE[lineSize] );
    for( int i = 0; i < lines; i++ ) {
        //Do stuff
    }
    

    The first time around the loop ownership is transfered and now pLine contains a NULL pointer for the second a subsequent iteration.

    If you are not transfering ownership then you are probably using the completely wrong method for memory management and a vector is probably a better idea.

    for( int i = 0; i < lines; i++ ) {
        std::vector<BYTE> pLine(lineSize);
        //Do stuff
    }
    

    The advantage of putting it inside the loop it is re-initialized so all members are zero on every iteration of the loop. If the //Do Stuff is affected by the state of the pLine then it may be incorrect to move it outside the loop (without also doing some more work to make sure the state of pLine is correct after each iteration.

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

Sidebar

Related Questions

SOLUTION Thanks to 1111... vector<std::string> split_at_line(string str, int lines) { vector<std::string> nine_ln_strs; string temp;
Lets say I have these lines of code; std::vector<int> ints; std::for_each(ints.begin(), ints.end(), [](int& val){
Is there a way to avoid writing std::vector<int> twice on lines such as these?
I'm trying to do something along these lines: int var = 5; std::numeric_limits<typeid(var)>::max(); but
#include <memory> struct foo { std::unique_ptr<int> p; }; int main() { foo bar {
Say I have something along the lines of: int main() { const char* someString
In C++, the following lines have me confused: int temp = (int)(0×00); int temp
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have a class containing a std::unique_ptr<> and I want to put instances of
i wanna read all the lines from a text so am doing this int

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.