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?
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:
Get Answers For Free
Most questions answered within 1 hours.