dynamic memory allocation in c code example
Example 1: dynamic memory allocation
int *p = new int;
*p = 5;
cout << *p << endl;
delete p;
cout << *p << endl;
Example 2: malloc in c
#include <stdlib.h>
void *malloc(size_t size);
void exemple(void)
{
char *string;
string = malloc(sizeof(char) * 5);
if (string == NULL)
return;
string[0] = 'H';
string[1] = 'e';
string[2] = 'y';
string[3] = '!';
string[4] = '\0';
printf("%s\n", string);
free(string);
}
Example 3: allocate memory c
ptr = (castType*)calloc(n, size);
Example 4: dynamic memory allocation in c++
char* pvalue = NULL;
pvalue = new char[20];
Example 5: dynamic memory allocation in c++
#include <iostream>
using namespace std;
int main () {
double* pvalue = NULL;
pvalue = new double;
*pvalue = 29494.99;
cout << "Value of pvalue : " << *pvalue << endl;
delete pvalue;
return 0;
}