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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:27:42+00:00 2026-05-18T08:27:42+00:00

How do you use a file to initialize a global const object that is

  • 0

How do you use a file to initialize a global const object that is too large to be created in the stack? This is my attempt so far:

// test.h
#pragma once
#include <boost/array.hpp>

typedef boost::array<int,100000> bigLut_t;
extern const bigLut_t constLut;

// test.cpp
#include <fstream>
#include <boost/filesystem.hpp>
#include "test.h"

bigLut_t& initializeConstLut()
{
    if( boost::filesystem::exists("my_binary_file") == false ) {
        std::ofstream outStream( "my_binary_file", ios::out | ios::binary );
        bigLut_t* tempLut = new bigLut_t;
        for(int i = 0; i < 100000; ++i) {
            // Imagine this taking a long time,
            // which is why we're using a file in the first place
            tempLut->at(i) = i;
        }
        outStream.write( reinterpret_cast<char*>(tempLut), sizeof(bigLut_t) );
        outStream.close();
        delete tempLut;
    }
    // We can't write "bigLut_t lut;" because that would cause a stack overflow
    bigLut_t* lut = new bigLut_t; // lut gets never deallocated
    std::ifstream inStream( "my_binary_file", ios::in | ios::binary );
    inStream.read( reinterpret_cast<char*>(lut), sizeof(bigLut_t) );
    inStream.close();
    return *lut;
}

const bigLut_t constLut = initializeConstLut();

AFAIK this works in a sense that constLut gets corretly initialized, but there’s a memory leak since bigLut_t* lut gets never deallocated. I tried using a smart pointer for it, but that resulted in the values in constLut being quite random. I’m baffled by the lack of information I found by trying to google the solution.

  • 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-18T08:27:42+00:00Added an answer on May 18, 2026 at 8:27 am

    How did you use shared_ptr ? Try the following :

    // test.h
    #pragma once
    #include <boost/array.hpp>
    
    typedef boost::array<int,100000> bigLut_t;
    extern const bigLut_t constLut;
    

    // test.cpp
    #include <fstream>
    #include <boost/filesystem.hpp>
    #include "test.h"
    
    boost::shared_ptr<bigLut_t> initializeConstLut()
    {
        if( boost::filesystem::exists("my_binary_file") == false ) {
            std::ofstream outStream( "my_binary_file", ios::out | ios::binary );
            bigLut_t* tempLut = new bigLut_t;
            for(int i = 0; i < 100000; ++i) {
                // Imagine this taking a long time,
                // which is why we're using a file in the first place
                tempLut->at(i) = i;
            }
            outStream.write( reinterpret_cast<char*>(tempLut), sizeof(bigLut_t) );
            outStream.close();
            delete tempLut;
        }
        // We can't write "bigLut_t lut;" because that would cause a stack overflow
        boost::shared_ptr<bigLut_t> lut(new bigLut_t); // lut gets never deallocated
        std::ifstream inStream( "my_binary_file", ios::in | ios::binary );
        inStream.read( reinterpret_cast<char*>(lut), sizeof(bigLut_t) );
        inStream.close();
        return lut;
    }
    
    const bigLut_t constLut = *(initializeConstLut().get());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a temp directory that will be unique to this build.
I have a simple log4j.property file and a simple program that use log4j. But
When should I use .resx file and when go for .config file? What is
I use an XML file in App_Data in conjunction with a Repeater on the
Should I use the resource file directly in the aspx page like <asp:Literal ID=userManagementSave
I'm trying to use System.IO.File.Replace to update a file, and it's throwing System.IOException if
Is it possible to use single XML file for Ruby on Rails as an
Is it possible to somehow use a .bat file to script the schema and/or
What is the easiest way to use a DLL file from within Python ?
I'm trying to read a value from a file and use it in a

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.