C/Memory/Memory Allocation

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Allocate array in memory: how to use calloc

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  int i, n;
  int *pointerData;
  printf ("Enter number of items to be stored: ");
  scanf ("%d", &i);
  pointerData = (int*) calloc (i, sizeof(int));
  
  if (pointerData==NULL) 
      exit (1);
      
  for (n = 0; n < i; n++)
  {
    printf ("Enter number #%d: ", n);
    scanf ("%d", &pointerData[ n ]);
  }
  printf ("You have entered: ");
  
  for (n = 0; n < i; n++) 
      printf ("%d ", pointerData[ n ]);
  free (pointerData);
  return 0;
}


Allocate memory

#include <stdlib.h>
#include <stdio.h>
int main()
{
  float *p;
  p = calloc(100, sizeof(float));
  if(!p) {
    printf("Allocation Error\n");
    exit(1);
  }
  return 0;
}


Allocate memory and reallocate

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
  char *p;
  p = malloc(17);
  if(!p) {
    printf("Allocation Error\n");
    exit(1);
  }
  strcpy(p, "This is 16 chars");
  p = realloc(p, 18);
  if(!p) {
    printf("Allocation Error\n");
    exit(1);
  }
  strcat(p, ".");
  printf(p);
  free(p);
  return 0;
}


Allocate memory block: how to use malloc

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  int i, n;
  char *str;
  printf ("String Length? ");
  scanf ("%d", &i);
  str = (char*) malloc (i+1);
  if (str == NULL) 
      exit (1);
  for ( n = 0; n < i; n++)
      str[n] = rand() % 26 + "a";
  
  str[i] = "\0";
  printf ("Random string: %s\n", str);
  free (str);
  return 0;
}


Allocate space for a string dynamically, request user input, and then print the string backwards

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
  char *s;
  register int i;
  s = malloc(80);
  if(!s) {
    printf("Memory request failed.\n");
    exit(1);
  }
  gets(s);
  for(i = strlen(s) - 1; i >= 0; i--) 
      putchar(s[ i ]);
  free(s);
  return 0;
}


Find out the address after malloc

#include <stdio.h>
#include <malloc.h>
main() {
    int  *base;
    int i,j;
    int cnt = 3;
    int sum = 0;

    base = (int *)malloc(cnt * sizeof(int));
    printf("the base of allocation is %16lu \n",base);
    if(!base)
        printf("unable to allocate size \n");
    else {
      for(j = 0;j < cnt; j++)
          *(base+j)=5;
    }
    for(j = 0;j < cnt; j++)
      sum = sum + *(base+j);
    printf("total sum is %d\n",sum);
    free(base);
    printf("the base of allocation is %16lu \n",base);
    base = (int *)malloc(cnt * sizeof(int));
    printf("the base of allocation is %16lu \n",base);
    free(base);
    base = (int *)calloc(10,2);
    printf("the base of allocation is %16lu \n",base);
    free(base);
}


Get the current system free memory

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  char *p;
  long l;
  l = 0;
  do {
    p = malloc(1000);
    if(p) 
        l += 1000;
  } while(p);
  printf("Approximately %ld bytes of free memory.", l);
  return 0;
}


Reallocate memory block: how to use realloc

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  int input, n;
  int count=0;
  int *numbers = NULL;
  do {
     printf ("Enter an integer value ( enter 0 to stop): ");
     scanf ("%d", &input);
     count++;
     numbers = (int*) realloc (numbers, count * sizeof(int));
     if (numbers == NULL) { 
         puts ("Error (re)allocating memory"); 
         exit ( 1 ); 
     }
     numbers[ count - 1 ] = input;
  } while (input!=0);
  printf ("Numbers entered: ");
  
  for ( n = 0; n < count; n++) 
      printf ("%d ",numbers[ n ]);
      
  free (numbers);
  return 0;
}


Store string in allocated memory

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
  char *str[100];
  int i;
  for(i = 0; i < 100; i++) {
    if((str[i] = malloc( 128 )) == NULL) {
      printf("Allocation Error\n");
      exit(1);
    }
    gets(str[i]);
  }
  /* now free the memory */
    
  for(i = 0; i < 100; i++) 
      free(str[i]);
  return 0;
}


Use malloc to allocate memory

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  char *p;
  p = malloc(80);
  if( !p ) {
    printf("Memory Allocation Failed");
    exit(1);
  }
  printf("Enter a string: ");
  gets( p );
  
  printf( p );
  free( p );
  return 0;
}