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
1.when we create a dynamic array the area of memory used is called a. runtime area...
1.when we create a dynamic array the area of memory used is called a. runtime area b. usable memory area c. heap d. stack
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...
Please decrypt the following encrypted message using c++ coding language, using the CLASS method : Y...
Please decrypt the following encrypted message using c++ coding language, using the CLASS method : Y [ZU [ XZWZ \ XXZjZ [ [ WWVWU Ogaqkxpgbqbfiof! Cyw'ss bun! Zqksxl cywd lnepwvs uozneh. You are required to programmatically implement an automatic decryption tool, using only C++ as your programming language. Your program must show how all steps of the decryption process are resolved, for any marks to be awarded. This is similar to the decryption method being done manually, by hand on...
Write the algorithm for this program using the C++ language (not python or java). The explanation...
Write the algorithm for this program using the C++ language (not python or java). The explanation needs to be at least 250 words. Explain everything throughly step by step. While there is a built-in pop_back function in the vector class, there is no built-in pop_front function. Suppose a program needs a pop_front function that will remove the first element from the vector. For example, if the original vector is [1, 2, 3, 4, 5], then after passing in this vector...
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.
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”...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
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,...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT