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

Can I use an elsewhere opened FILE* f = ... thing to initialize some
Under my VS2010 solution I've this situation: WEBSITE Library1 Library2 On global.asax.cs I initialize
I have a global NSString variable that I declared in my ViewController.m file, outside
When I use file functions in PHP, I check for EOF. I wonder if
On Unix, everything is a file, so you can use file i/o functions with
When should I use .resx file and when go for .config file? What is
I need to use .htaccess file to replace a word in a URL; something
When you use the file import in drupal 6 it adds a whole lot
When i use a file upload i use to check file contenttype with regular
I don't understand what happens here : use PAR { file => 'foo.par', fallback

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.