Answers for "malloc cpp"

C++
1

malloc() in c and c++

/* malloc example: random string generator*/
#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='';

  printf ("Random string: %sn",buffer);
  free (buffer);

  return 0;
}
Posted by: Guest on September-19-2020
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