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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:57:11+00:00 2026-05-27T03:57:11+00:00

I have this following program which gets the number of files from user, then

  • 0

I have this following program which gets the number of files from user, then the filename and reads the content of the file in to a priority queue. When I execute the program, after I enter the first filename it gives segmentation fault.

#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <fstream>
using namespace std;

int main() {
    char *filename;
    int fnum;

    cout<<"Number of files:"<<endl;
    cin>>fnum;

    int i;
    priority_queue<int, vector<int>, greater<int> > pqi;
    for(i = 0; i<fnum;i++){
        cout <<"Enter Filename:"<<endl;
        cin>>filename;
        ifstream inFile(filename);
        long n;
        while(!inFile.eof()){
            inFile >> n;
            pqi.push(n);
        }
        inFile.close();
        inFile.clear();
    }
    while(!pqi.empty()) {
        cout << pqi.top() << ' ';
        pqi.pop();
    }
}

Not able to figure out why.

  • 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-27T03:57:12+00:00Added an answer on May 27, 2026 at 3:57 am

    The problem is with your char* definition. You just define a pointer and don’t allocate any memory to it. You have to allocate memory to it using the new keyword:

    char *filename = new char[256];
    //... rest of your code ...
    //When you no longer need filename (usually at the end of the code)
    //you have to free the memory used by it manually:
    delete[] filename;
    

    In this simple case you can also use a static array:

    char filename[256];
    //No need to delete[] anything in this way.
    

    Both of the above ways allocate a fixed amount of memory to filename, which means if user enters a file name longer than 256 bytes in the above examples we encounter a buffer overflow. You can use string type which automatically does the memory management for you and is easy to use:

    #include <string>
    string filename;
    cin >> filename;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have this function which gets called multiple times during my program. //global
According to this site , I have done the following program which sorts strings.
I have the following classes in my c++ program: game_entity game_creature which inherits from
I have written the following C program. The output is 32. Why is this?
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have this following code which will return all the current semesters. How do
i have the following problem. The 1st step is to implement a program, which
I have this following code fragment which is accessed by different threads. try {
I have a program which gets commands as a string. Each character in the
In some crunching number program, I have a function which can be just 1

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.