Header file:
stdlib.h
Synopsis:
void free(void *ptr);
Description:
It deallocates the memory block pointed by ptr.
free function C example:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct db {
char name[50];
int age;
};
int main() {
struct db *data;
char *str;
str = (char *)malloc(20);
strncpy(str, "Student detail: ", 20);
data = (struct db*)malloc(sizeof (struct db));
strcpy(data->name, "Micheal Jackson");
data->age = 10;
printf("%s\nName: %s\n", str, data->name);
printf("Age: %d\n", data->age);
free(data);
free(str);
return 0;
}
#include<stdlib.h>
#include<string.h>
struct db {
char name[50];
int age;
};
int main() {
struct db *data;
char *str;
str = (char *)malloc(20);
strncpy(str, "Student detail: ", 20);
data = (struct db*)malloc(sizeof (struct db));
strcpy(data->name, "Micheal Jackson");
data->age = 10;
printf("%s\nName: %s\n", str, data->name);
printf("Age: %d\n", data->age);
free(data);
free(str);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Student detail:
Name: Micheal Jackson
Age: 10
Student detail:
Name: Micheal Jackson
Age: 10
No comments:
Post a Comment