Answers for "malloc in c+"

C
1

malloc in c++

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

int main() {

  // allocate memory of int size to an int pointer
  int* ptr = (int*) malloc(sizeof(int));

  // assign the value 5 to allocated memory
  *ptr = 5;

  cout << *ptr;

  return 0;
}

// Output: 5
Posted by: Guest on March-06-2022
18

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);
}

/// output : "Hey!"
Posted by: Guest on March-03-2020

Code answers related to "C"

Browse Popular Code Answers by Language