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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:12:32+00:00 2026-06-08T00:12:32+00:00

I have a program that reads data from a .csv and creates a multimap

  • 0

I have a program that reads data from a .csv and creates a multimap. I’ve pasted a simple version below. It works fine, but runs really slowly on exit (i.e., once the program prints out the output and gets to return 0;. I’m working relatively big files, e.g., anywhere from 50 to a few hundred megabytes. The delay on exit is, unsurprisingly, proportional to the file size.

I’m wondering if there is any way to speed up the exit? My guess is that it’s slow because it’s clearing data from memory. CTRL+c in a terminal window shuts it down immediately, though. I’ve looked but haven’t found much.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

int main (int argc, char* argv[])
{

std::string filename = "edge_data_mid.csv";

// obtain multimaps
std::ifstream fin;

fin.open(filename.c_str(), std::ios_base::in|std::ios_base::binary);
if (!fin.is_open()) {
    std::cout << "could not open the file '" << filename << "'" << std::endl;
    exit(EXIT_FAILURE);
}

std::multimap<std::string, std::string> gciting;

std::string line;

// this loop parses the file one line at a time
while (getline(fin, line)) {
    std::istringstream linestream(line);
    std::string item;

    std::string nodea;
    std::string nodeb;

    getline(linestream, item, ',');
    nodea = item;
    getline(linestream, item);
    nodeb = item;

    gciting.insert(std::pair<std::string, std::string>(nodeb, nodea));
}

fin.close();       

std::cout << gciting.count("3800035") << std::endl;

return 0;
}
  • 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-08T00:12:36+00:00Added an answer on June 8, 2026 at 12:12 am

    How would I make my object not to be destructed?

    If you really want to stop the object from deallocating itself and it’s containing elements (since you know the application will terminate) you could allocate it on the heap using new and then never free the memory (normally done through delete).

    This normally isn’t recommended but major operating-systems today are smart enough to release any allocated memory back to the OS when an application terminates, so you should be safe by using this approach.


    Please note that I used the word "should" and not "will" in the above.

    There is nothing in the standard saying that the memory allocated will be released back to the OS or what will happen if you don’t release it (other than that you have leakage).

    If you are writing code to be run on a minor system (such as the controller of a fridge), do not use this approach before actually verifying that this platform will indeed free memory after termination.

    We don’t want our food to go bad.


    Potential reasons for it being slow besides memory deallocation

    If you are running your application in debug mode a lot more than what is required to happen will take place to aid eventual debugging of your application.

    Try compiling it with optimization flags turned on, and in (what is normally referred to as) release mode.


    Using placement new..

    If you, for some odd reason, still want your object to reside on the stack you could make use of placement-new, as in the below snippet. placement-new will try to make use of the address you pass to it and put the object there.

    c++11

    #include <type_traits>
    

    typedef std::multimap<std::string,std::string> SSMap;
    
    std::aligned_storage<
      sizeof(SSMap), std::alignment_of<SSMap>::value
    >::type storage;
    
    SSMap& gciting = * new (&storage) SSMap;
    
    /* use gciting exactly as before */
    

    c++03

    In C++03 the best/easiest way to ensure proper alignment is to use malloc to allocate memory which will meet the strictest alignment available (that will work for every type).

    With this said there is no easy way of having the object still residing on the stack, though the interface of which to use the object stays the same, we can still create a reference to an object residing on the heap.

    #include <cstdlib>
    
    typedef std::multimap<std::string,std::string> SSMap;
    
    char * storage = static_cast<char*> (std::malloc (sizeof (SSMap))); 
    SSMap& gciting = *new (storage) SSMap;
    
    /* use gciting exactly as before */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple program that reads data from a PNG into a 2D
I have a program that reads from a serial port. the program works fine
I have a program that reads data from a file line-by-line. I would like
I have a program that reads arbitrary data from a file system and outputs
I'm learning java and I have made simple program that simply reads value from
actually i have a java program that reads the data from a specific XML
I have a server program that reads data from a remote stream via TCP,
I have a vb program that reads data from an excel sheet and displays
I have a small program that reads in a file and processes the data
I have a program that reads from a file that grabs 4 bytes from

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.