Question

A) Write a command that will execute the file named main and report memory management errors...

A) Write a command that will execute the file named main and report memory management errors (memory leaks) when the program has finished.

B) Write a command that will generate an executable file named main from the C++14 source code file main.cpp that includes all available debugging information. After this command, write a second command to start the C++ debugger in text user interface.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Memory Leakage occurs in C++ when programmers allocate memory by using new keyword and forgets to deallocate the memory by using delete() function or delete[] operator .One of the most memory leakage occurs in C++ by using wrong delete operator .

The delete operator should be used to free a single allocated memory space ,whereas the delete[] operator should be used to free an array of data values.

DISADVANTAGE WITH MEMORY LEAKAGE:

if a program has memory leaks, then its memory usage is satirically increasing since all systems have limited amount of memory and memory is costly.Hence it will create problems .

EXAMPLE OF MEMORY LEAKAGE IN C++:

#include <bits/stdc++.h>

using namespace std;

void func_to_show_mem_leak()

{

int * ptr = new int(5);

//body

return;

}

//driver code

int main()

{

func_to_show_mem_leak();

return 0;

}

HOW TO AVOID MEMORY LEAK:

-instead of managing memory manually,try to use smart pointers where applicable .

-use std::string instead of char* .The std::string class handles all memory management internally,and it's fast and well optimized.

-Never use a raw pointer unless it's to interface with an older lib.

-The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible -ideally NONE. Anything that requires dynamic memory should be buried inside an RAII object that releases the memory when it goes out of scope. RAII allocate memory in constructor and release it in destructor,so that memory is guaranteed to be deallocated when the variable leaves the current scope.

-Allocate memory by new keyword and deallocate memory by delete keyword and write all code between them.

EXAMPLE TO HANDLE MEMORY LEAKS:

#include <bits/stdc++.h>

using namespace std;

void func_to_handle_mem_leak()

{

int *ptr = new int(5);

//body

delete(ptr);

}

//Driver code

int main()

{

func_to_handle_mem_leak()

return 0;

}

Therefore Always write delete pointer for matching of new pointer in C++ and always write code between these new and delete as explained in above example. In above example ,no memory is wasted because when we are coming out from the function we are deallocating the memory by using delete function..

COMMAND FOR EXECUTABLE FILE AND DEBUGGER:

Let us first understand what happens under the hood while executing a C program In linux system, how main() is called and how to execute a program without main().

Following step is considered:

-UBUNTU 16.4 LTS OPERATING SYSTEM

-GCC 5.4.0 COMPILER

-objdump utility

From C/C++ programming perspective ,the program entry point is main() function .From the perspective of program execution,However it is not.Prior to the point when the execution flow reaches to the main(),calls to few other functions are made,which set up arguments,prepare environment variables for program execution.

The executable files created after compiling a C source code is a EXECUTABLE AND LINKABLE FORMAT (ELF) file.

Every ELF file have a ELF Header where there is a e_entry field which contains the program memory address from which the execution of executable will start . This memory address point to the _start() function.

After loading the program ,Loader looks for the e_entry field from the ELF file header.

EXECUTABLE AND LINKABLE FORMAT(ELF) is a common standard file format used in UNIX system for executable files,object code ,shared libraries and core dumps.

let's c this using an example:

int main()

{

return(0);

}

Now compiling this using following  commands

gcc -o example example.c

now an example executable is created :

objdump -f example

This outputs following critical information of executable on my machine . Have a look at start address below, this is the address pointing to _start() function.

example : file format elf64-x86-64

architecture : i386:x86 -64,flags 0 *000000112:

EXEC_P.HAS_SYMS,D_PAGES

start address 0*00000000004003e0

We can cross check this address by deassembling the executable,the output is long so I'm just pasting the output which shows where this address 0x00000000000004003e0 is pointing

objdump --disassemble example

OUTPUT:

Hey !! i had completely wrote the full answer and explained every step for it along with the on -screen output so if any query then comment and tell me and uprate my ratings if possible as it is the most important thing for me at the moment..

Add a comment
Know the answer?
Add Answer to:
A) Write a command that will execute the file named main and report memory management errors...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using PuTTY Linux Server Task Compiling:             1) Download the two files from blackboard, driver.cpp, and...

    Using PuTTY Linux Server Task Compiling:             1) Download the two files from blackboard, driver.cpp, and circle.h             2) Create a new directory to store the files in             3) Compile the code a) Run the command g++ driver.cpp -o executable_name, this will compile the code both for driver.cpp and the referenced file circle.h note: -o parameter specifies a new name for the executable, if you do not specify the “-o” parameter the default name of the executable is “a.out”...

  • Write the source codes in the command line interface that is named after your name to...

    Write the source codes in the command line interface that is named after your name to generate the random code sequence like in the movie “Matrix”. Record the screenshot of your source code that includes all the required operations and results. You will get zero point if no screenshot is provided. [14 points] name is zehua

  • For the efficient memory management and to avoid errors modern systems uses secondary memory as a...

    For the efficient memory management and to avoid errors modern systems uses secondary memory as a part of the main memory known as a virtual memory. When a computer system is running serval programs at the same time, the main memory often becomes exhausted. In such circumstances instead of closing some programs the operating system use the secondary memory as part of the main memory to store some data. In short virtual memory is an elegant interaction of hardware exceptions,...

  • (In Linux) 1. Create a file to write to from your script and save it as...

    (In Linux) 1. Create a file to write to from your script and save it as “.txt”. 2. Write the following information to the file that you created in Step 1: Today's date Your full name Your student ID The name of your course What directory you are in File permissions of the file that you created that allow everyone to read, write, and execute The long list of files in your current directory, providing all items and their process...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

  • OPERATING SYSTEM... C , LINUX Modify the mycat.c program to write to stderr the number of...

    OPERATING SYSTEM... C , LINUX Modify the mycat.c program to write to stderr the number of bytes that were read from stdin each time. You may use cLion inside VM, or any other text editor of your choice, such as vi or emacs. There is also a neat text editor named Kate available in Ubuntu (you can find it in the menu). In cLion you can open a single.cfile without having to create a whole new project and modify the...

  • Write a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".

    Project DescriptionWrite a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".Project SpecificationsInput for this project:the user must enter the number of employees in the company.the user must enter as integers for each employee:the employee number (ID)the number of days that employee missed during the past year.Input Validation:Do not accept a number less than 1 for the number of employees.Do not accept a negative...

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • Linux & Unix Write a bash program to indent the code in a bash source file....

    Linux & Unix Write a bash program to indent the code in a bash source file. Conditions:     The source file will contain only printing characters, spaces, and newlines. It is not necessary to check for invalid input.     The source file will not contain comments (words beginning with #). Requirements:     Read from standard input, write to standard output.     Code inside while statements should be indented 2 spaces. Be sure your program includes all of the following:    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT