Question

We discussed in the class that there are 3 major issues with pointers and dynamic memory...

We discussed in the class that there are 3 major issues with pointers and dynamic memory allocations .

  • Memory fault: Invalid memory access  
  • Memory leak
  • Memory corruption

Identify the line numbers in the code snippet below for each issue listed above with one line explaining your reason.

A sample answer:

- Memory fault: line number 6: because a for-loop can't iterate more than 1,000,000

- Memory leak: line number 9; because ....

- Memory corruption: line number 7; because ....

[code snippet]

1. int *intPtr = new int;
2. int *intPtr2 = intPtr;
3. int *intPtr3 = x00FF1234;  //Assume this is a valid address
3. *intPtr  = 2123;
4. *intPtr2 = *intPtr = 11;
4. delete(intPtr)
5. (*intPtr2)++;

6. for (int i = 0; i <= 1000000; i++) {
7.     int * intPtr = new int[1000];
8.     for (int j = 0; i <= 1000; j++) 
9.          intPtr[j] = *intPtr3++;
10. }

Homework Answers

Answer #1

Memory leak is caused by programmers when they create a memory in heap and does not delete it.

The line 8 with for loop has an error as the checking condition is with respect to 'i' and not 'j'.

In line 9 memory is allocated for intPtr[j] but is not deleted afterwards which leads to memory leak. So add these 3 lines of code after the 9th line to overcome memory leak :

for (int j = 0; j<= 1000; j++)

         delete Ptr[j];
delete Ptr;

The alteration of computer system memory without explicit assignment is known as memory corruption.

In line 7 an array is used in a loop, with wrong or incorrect terminating condition, so there is a chance of manipulating the memory beyond array bounds accidentally. When we use memory beyond memory that was allocated, it leads to memory corruption.

Know the answer?
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
Similar Questions
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V { public: IndexType partition(IndexType lo, IndexType hi); IndexType sort(int numvals); void qsRecursive(IndexType lo, IndexType hi); IndexType getHiIndex(); IndexType getLoIndex(); void setHiIndex(IndexType index); void setLoIndex(IndexType index); Array_V(IndexType lo, IndexType hi); //constructor Array_V(int size = 0); Array_V(const Array_V< IndexType, BaseData >& initArray); //copy constructor ~Array_V(); //destructor BaseData& operator [] (IndexType); Array_V< IndexType, BaseData >& operator = (const Array_V< IndexType, BaseData >& initArray); void assign(IndexType i, const...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer>...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer> moneyBox; //store the log of transactions for auditing StringBuilder log; /** * Constructs an empty register */ public SimpleRegister() { moneyBox = new TreeMap<Integer, Integer>(); moneyBox.put(1, 0); moneyBox.put(5, 0); moneyBox.put(10, 0); moneyBox.put(25, 0); moneyBox.put(100, 0); moneyBox.put(500, 0); moneyBox.put(1000, 0); log = new StringBuilder(); } @Override public void addPennies(int num) { moneyBox.put(1, moneyBox.get(1) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1 / 100.0f);...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT