Question

Create a program that allocates an array of integers, frees them, and then tries to print...

Create a program that allocates an array of integers, frees them, and then tries to print the value of one of the elements of the array. Does the program run? What happens when you use Valgrind on it? Now pass a funny value to free (e.g., a pointer in the middle of the array you allocated above). What happens? Do you need tools to find this type of problem?

Homework Answers

Answer #1

Solution:-

No it will not run as memory to that pointer is freed and we are trying to print element of that pointer.

Example program:

#include<iostream>
using namespace std;

int main(){
   int arr[] = {1,23,3,4,5,6};
   free(arr);
   cout<<"Attempting to read element from deleted array. Element is : "<<arr[1];
}

OUTPUT:

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
C++ 1. An array of integers is said to be a palindrome if it is read...
C++ 1. An array of integers is said to be a palindrome if it is read the same from right to left and from left to right such as 1 2 3 2 1. Implement a program that: a. Prompts the user for an integer value n which represents the number of integers to be read next. b. Create a one dimensional dynamic array with size equal to n. c. Create two pointers front and rear and make them point...
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
C++ Create a program that will use pointers to determine the average (to 1 decimal place)...
C++ Create a program that will use pointers to determine the average (to 1 decimal place) of a series of grades entered into an array. You can assume a maximum of 15 students and the user will end input with a sentinel value. Make sure to use pointer increment and a pointer comparison while loop, and not array notation or array index numbers or offsets. You will use a function called getgrades. Send the array name (a pointer constant) to...
Follow the instructions below to create a full running Assembly program: Create a ​WORD ​sized array...
Follow the instructions below to create a full running Assembly program: Create a ​WORD ​sized array with ​5​ elements of ​A2D5h ​using the ​DUP​ operator. Move the ​OFFSET ​of this array into register ​ESI​. Move the number of elements in the array initialized using the ​LENGTHOF​ instruction and move it into ECX as your loop counter. Create a ​LOOP​ that adds the value 1F33h to each element. Traverse through the array by adding the size of each element to the...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Prelab: Week of September 9th You’ve created a general-purpose function for allocating an array with elements...
Prelab: Week of September 9th You’ve created a general-purpose function for allocating an array with elements of any data type: array = createArray(numElems, elemSize); Now we want to generalize the function so that the result secretly stores the size (number of elements) with the array so that we can implement the following function size = getArraySize(array); which allows the user to determine the number of elements in a given array whenever its needed. You’ll want to declare the parameter to...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...
The local taqueria has decided they need to raise their prices. In order to soften the...
The local taqueria has decided they need to raise their prices. In order to soften the blow to their customers, they also want to rename all their burritos to make them sound more desirable. Your C++ program should create two arrays in main() - one string array with 3 burrito types and one float array with 3 associated prices, defined below: string names[] = {"Carnitas", "Pollo", "Veggie"}; float prices[] = {6.95, 6.25, 5.95}; Now, main should declare a string pointer...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT