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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:44:03+00:00 2026-06-13T02:44:03+00:00

I have a program where i ask the user for the name of a

  • 0

I have a program where i ask the user for the name of a text file, I open the text file do stuff with it (read, write), then I close the file and exit the program.

Program.h

class Program
{
     char* fileName;
public:
     Program();
     ~Program();
     void ReadFile(void);
};

Program.cpp

Program::Program(){
     //contstructor
     fileName=NULL;
}

Program::~Program(){
     cout << "in destructor" ;
     delete []fileName;
}

void Program::ReadFile(void){
     fileName = new char[40];

     cout <<"Please enter the name of the file to open: ";
     cin.clear();
     cin.getline(fileName, 40);

     ifstream file (fileName);

     if(file.is_open()){
          //do stuff
     }
     file.close();
}

right now when I put delete []fileName; in the destructor it outputs “”in destructor” on the screen but fileName does not get deleted. If I take delete []fileName; and put it in ReadFile() after file.close() fileName gets deleted. Why is that?

The rest of my program works perfectly which is why none of that code is pasted. I am just trying to rid any memory leaks and fileName is the only one I am having trouble with so therefore I only pasted the code where fileName is used.

Any help is appreciated.

Additional information:
I am using Visual Studio to write this and am using the Memory Leak Detection. This is what it outputs:

Detected memory leaks!
Dumping objects ->
{132} normal block at 0x005D49A0, 40 bytes long.
Data: 6E 61 6D 65 73 2E 74 78 74 00 CD CD CD CD CD CD
Object dump complete.
The program ‘[10772] program1.exe: Native’ has exited with code 0 (0x0).

which is why I suspect delete []fileName; didn’t work.

also, this is what the int main() looks like

int main(){
     Program abc;
     abc.ReadFile();
}

Oh, and Program.h can not be changed. Only .cpp can be change it’s part of my requirements.

  • 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-13T02:44:05+00:00Added an answer on June 13, 2026 at 2:44 am

    Are you sure your main is exactly as posted here? If you just define abc globally, it will be freed after memory dump report memory leaks and you may see invalid reports! You can insert a break point in the destructor and see if memory leaks reported after or before destructor

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

Sidebar

Related Questions

For a project, I have to ask the user for a file name, and
I have written the following very simple Java program to ask user enter a
I need to write a program, that will ask a user to enter a
I need to write a javascript program that will ask the user to enter
I want my C program to ask the user to type the name of
I have a program which creates JButtons which are then added to a JPanel
I have a program that saves an image in a local directory and then
I have a program that load data from a file using std::ifstream and store
I have to write an address book program in C# 2008. It is supposed
I have been trying to write some code that asks the user for several

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.