Question

suppose you have allocated an array of twenty doubles, called values, on the heap. Write the...

suppose you have allocated an array of twenty doubles, called values, on the heap. Write the line of code (including the semicolon) to free up the memory when the array is no longer needed.

c code

Homework Answers

Answer #1

Dynamic memory is allocted in heap in the program.

So code to allocate dynamic memory in the heap as mentioned in question is like:

double *values;

values = malloc(20 * sizeof(double));

So now one can put values in it by using simply values[0], values[1]....values[19]

To free the dynamically allocated memory in heap one can use free() function.

Code to free the memory dynamically allocated in the above code is like:

free(values);

values=NULL;

Assigning Null to the pointer after the deallocation is not necessary but it is a good practice.

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
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
(a) Write a function in C++ called readNumbers() to read data into an array from a...
(a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of doubles The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows is exceeded. (b) Write a program with the...
Write a program in Java that creates an array of doubles. The array must have size...
Write a program in Java that creates an array of doubles. The array must have size 1000. Fill this array with random numbers between 0 and 1. Then compute and print the total value of all the elements of the array, the mean, the minimum, and the maximum. Filling the array with random numbers must be done by a method that you define and implement. Computing and printing the statistics must be done by another method, which again you must...
Write a testing program (in C) that contains a heap buffer overflow vulnerability. (Provide an example...
Write a testing program (in C) that contains a heap buffer overflow vulnerability. (Provide an example in the project. Copy/paste is fine. No Screenshot). Again, you do not have to exploit it. Show what the heap layout looks like and explain how to exploit it. (Include a diagram) Include the following items: Each chunk of memory allocated by malloc() and their metadata. Their sizes in bytes. The overflow direction in the heap. The size of the overflowing buffer to reach...
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
Write a Java part code to do the following   : ** Suppose a file called infile stored...
Write a Java part code to do the following   : ** Suppose a file called infile stored in drive D: ,filled with five integers(1000,200,3030,40 and 500) Read  the integers from infile then store them in array called ar[] and print it elements. Method called search( ) to print the location of   value 500. Method called sort() to sort array elements. Build another file called outfile in drive D: to save the sorted array elements . Note : Use ArrayIndexOutOfBoundsException when you use array...
[Multiple correct answers] Write a line of Java code to create an array of integers called...
[Multiple correct answers] Write a line of Java code to create an array of integers called studentIDs that can hold 1200 values.    int [] studentIDs = new int[1200];     int studentIDs [] = new int[1200];     int studentIDs = new int[1200];     int [] studentIDs = new int(1200);     int studentIDs [] = new int(1200);     int [] studentIDs = int[1200];     int studentIDs [] = int[1200];
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
Write a function called read floats(int n) that reads n float values from the user into...
Write a function called read floats(int n) that reads n float values from the user into an array and return the array back to the caller. Recall that a regular array will get deallocated when the function returns. (C++ the simplier the better)
Assume we have an array int* arr = {1,21,32,43,54}. The values in arr store in the...
Assume we have an array int* arr = {1,21,32,43,54}. The values in arr store in the memory of 0(x27), 8(x27), …, 32(x27). Convert the following RISC-V code into a C program. ld x5, 24(x27) addi x8, x0, 0(x27) srl x5, x5, x6 sd x5, 0(x27)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT