Question

for c language What function is used to deallocate dynamic memory when it is no longer...

for c language

What function is used to deallocate dynamic memory when it is no longer required by your program? In your answer give the name of the function and its syntax, and how the C runtime system knows how much memory to deallocate. Give an appropriate example showing typical usage of this function

Homework Answers

Answer #1

free function is used to deallocate dynamic memory when it is no longer required.

Syntax of free function

free(pointer name)

example void(*ptr)

pointername is the name of the pointer which pointsto a memory block previously allocated with malloc, calloc or realloc to be deallocated.

Example of usage of free function

#include <stdio.h>
#include <string.h>
int main () {
char *str;
/* Initial memory allocation */
str = (char *) malloc(15);
strcpy(str, "hello");
  

/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, "world");

/* Deallocate allocated memory */
free(str);

return(0);
}

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++ language 1. Programs with memory leaks a should be executed in a secure environment to...
C++ language 1. Programs with memory leaks a should be executed in a secure environment to prevent theft of data by hackers. b will eventually crash if allowed to execute for long periods of time. c should be compiled with a special compiler that detects and flags the memory leaks. d should be executed under an operating system that can dynamically plug the leaks. e none of these. 2. An  lvalue is a a value of type long. b a memory...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
Describe the General Sinusoidal function. When was the term “sinusoid” first used? Sinusoidal functions occur in...
Describe the General Sinusoidal function. When was the term “sinusoid” first used? Sinusoidal functions occur in other fields of study and nature. Give an example of an application of sinusoid modeling in real life. How might this be useful in your field of study? Be sure to cite any sources you use.
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
1. In 1974, Loftus and Palmer conducted a classic study demonstrating how the language used to...
1. In 1974, Loftus and Palmer conducted a classic study demonstrating how the language used to ask question can influence eyewitness memory. In the study, college students watched a film of an automobile accident and then were asked questions about what they say. One group was asked, “About how fast were the cars going when they smashed into each other?” Another group was asked the same question except the verb was changed to “hit” instead of “smashed into.” The “smashed...
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas,...
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas, the following les for each of your programs: (1) the client and the server source les each (2) the client and the serve executable les each (3) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identiers suitable, to enable enhanced readability of the code. Problems 1. Write an ftp client that...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Need in C language also need full documentation/explanation of each line A student has established the...
Need in C language also need full documentation/explanation of each line A student has established the following monthly budget: Budget Categories Budgeted amount ----------------------------------------------------- Housing $ 580.00 Utilities $ 150.00 Household Expenses     $ 65.00 Transportation $ 50.00 Food $ 250.00 Medical $ 30.00 Insurance $ 100.00 Entertainment $ 150.00 Clothing $ 75.00 Miscellaneous $ 50.00 ---------------------------------------------------- Write a program that declares a MonthlyBudget structure designed with member variables to hold each of these expense categories. The program should define...
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...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...