Answers for "malloc c++ example"

C++
2

malloc c++ example

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int *ptr;
	ptr = (int*) malloc(5*sizeof(int));

	if(!ptr)
	{
		cout << "Memory Allocation Failed";
		exit(1);
	}
	cout << "Initializing values..." << endl << endl;

	for (int i=0; i<5; i++)
	{
		ptr[i] = i*2+1;
	}
	cout << "Initialized values" << endl;

	for (int i=0; i<5; i++)
	{
		/* ptr[i] and *(ptr+i) can be used interchangeably */
		cout << *(ptr+i) << endl;
	}

	free(ptr);
	return 0;
}
Posted by: Guest on July-03-2021
0

cpp malloc

int alloc_size = 10;
int* buffer = (int*) malloc (alloc_size);
//Allocate memory block which can fit 10 integers
Posted by: Guest on March-25-2021
0

malloc c++ program

void* malloc(size_t size);
Posted by: Guest on November-05-2020

Browse Popular Code Answers by Language