A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FMemory%2FMemory_Allocation</id>
		<title>C/Memory/Memory Allocation - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FMemory%2FMemory_Allocation"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/Memory/Memory_Allocation&amp;action=history"/>
		<updated>2026-04-07T05:00:59Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C/Memory/Memory_Allocation&amp;diff=306&amp;oldid=prev</id>
		<title> в 14:20, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/Memory/Memory_Allocation&amp;diff=306&amp;oldid=prev"/>
				<updated>2010-05-25T14:20:56Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:20, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C/Memory/Memory_Allocation&amp;diff=307&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/Memory/Memory_Allocation&amp;diff=307&amp;oldid=prev"/>
				<updated>2010-05-25T10:22:35Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Allocate array in memory: how to use calloc==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  int i, n;&lt;br /&gt;
  int *pointerData;&lt;br /&gt;
  printf (&amp;quot;Enter number of items to be stored: &amp;quot;);&lt;br /&gt;
  scanf (&amp;quot;%d&amp;quot;, &amp;amp;i);&lt;br /&gt;
  pointerData = (int*) calloc (i, sizeof(int));&lt;br /&gt;
  &lt;br /&gt;
  if (pointerData==NULL) &lt;br /&gt;
      exit (1);&lt;br /&gt;
      &lt;br /&gt;
  for (n = 0; n &amp;lt; i; n++)&lt;br /&gt;
  {&lt;br /&gt;
    printf (&amp;quot;Enter number #%d: &amp;quot;, n);&lt;br /&gt;
    scanf (&amp;quot;%d&amp;quot;, &amp;amp;pointerData[ n ]);&lt;br /&gt;
  }&lt;br /&gt;
  printf (&amp;quot;You have entered: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  for (n = 0; n &amp;lt; i; n++) &lt;br /&gt;
      printf (&amp;quot;%d &amp;quot;, pointerData[ n ]);&lt;br /&gt;
  free (pointerData);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Allocate memory==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  float *p;&lt;br /&gt;
  p = calloc(100, sizeof(float));&lt;br /&gt;
  if(!p) {&lt;br /&gt;
    printf(&amp;quot;Allocation Error\n&amp;quot;);&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Allocate memory and reallocate==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char *p;&lt;br /&gt;
  p = malloc(17);&lt;br /&gt;
  if(!p) {&lt;br /&gt;
    printf(&amp;quot;Allocation Error\n&amp;quot;);&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  strcpy(p, &amp;quot;This is 16 chars&amp;quot;);&lt;br /&gt;
  p = realloc(p, 18);&lt;br /&gt;
  if(!p) {&lt;br /&gt;
    printf(&amp;quot;Allocation Error\n&amp;quot;);&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  strcat(p, &amp;quot;.&amp;quot;);&lt;br /&gt;
  printf(p);&lt;br /&gt;
  free(p);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Allocate memory block: how to use malloc==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  int i, n;&lt;br /&gt;
  char *str;&lt;br /&gt;
  printf (&amp;quot;String Length? &amp;quot;);&lt;br /&gt;
  scanf (&amp;quot;%d&amp;quot;, &amp;amp;i);&lt;br /&gt;
  str = (char*) malloc (i+1);&lt;br /&gt;
  if (str == NULL) &lt;br /&gt;
      exit (1);&lt;br /&gt;
  for ( n = 0; n &amp;lt; i; n++)&lt;br /&gt;
      str[n] = rand() % 26 + &amp;quot;a&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  str[i] = &amp;quot;\0&amp;quot;;&lt;br /&gt;
  printf (&amp;quot;Random string: %s\n&amp;quot;, str);&lt;br /&gt;
  free (str);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Allocate space for a string dynamically, request user    input, and then print the string backwards==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char *s;&lt;br /&gt;
  register int i;&lt;br /&gt;
  s = malloc(80);&lt;br /&gt;
  if(!s) {&lt;br /&gt;
    printf(&amp;quot;Memory request failed.\n&amp;quot;);&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  gets(s);&lt;br /&gt;
  for(i = strlen(s) - 1; i &amp;gt;= 0; i--) &lt;br /&gt;
      putchar(s[ i ]);&lt;br /&gt;
  free(s);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Find out the address after malloc==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;malloc.h&amp;gt;&lt;br /&gt;
main() {&lt;br /&gt;
    int  *base;&lt;br /&gt;
    int i,j;&lt;br /&gt;
    int cnt = 3;&lt;br /&gt;
    int sum = 0;&lt;br /&gt;
&lt;br /&gt;
    base = (int *)malloc(cnt * sizeof(int));&lt;br /&gt;
    printf(&amp;quot;the base of allocation is %16lu \n&amp;quot;,base);&lt;br /&gt;
    if(!base)&lt;br /&gt;
        printf(&amp;quot;unable to allocate size \n&amp;quot;);&lt;br /&gt;
    else {&lt;br /&gt;
      for(j = 0;j &amp;lt; cnt; j++)&lt;br /&gt;
          *(base+j)=5;&lt;br /&gt;
    }&lt;br /&gt;
    for(j = 0;j &amp;lt; cnt; j++)&lt;br /&gt;
      sum = sum + *(base+j);&lt;br /&gt;
    printf(&amp;quot;total sum is %d\n&amp;quot;,sum);&lt;br /&gt;
    free(base);&lt;br /&gt;
    printf(&amp;quot;the base of allocation is %16lu \n&amp;quot;,base);&lt;br /&gt;
    base = (int *)malloc(cnt * sizeof(int));&lt;br /&gt;
    printf(&amp;quot;the base of allocation is %16lu \n&amp;quot;,base);&lt;br /&gt;
    free(base);&lt;br /&gt;
    base = (int *)calloc(10,2);&lt;br /&gt;
    printf(&amp;quot;the base of allocation is %16lu \n&amp;quot;,base);&lt;br /&gt;
    free(base);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Get the current system free memory ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char *p;&lt;br /&gt;
  long l;&lt;br /&gt;
  l = 0;&lt;br /&gt;
  do {&lt;br /&gt;
    p = malloc(1000);&lt;br /&gt;
    if(p) &lt;br /&gt;
        l += 1000;&lt;br /&gt;
  } while(p);&lt;br /&gt;
  printf(&amp;quot;Approximately %ld bytes of free memory.&amp;quot;, l);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reallocate memory block: how to use realloc==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
  int input, n;&lt;br /&gt;
  int count=0;&lt;br /&gt;
  int *numbers = NULL;&lt;br /&gt;
  do {&lt;br /&gt;
     printf (&amp;quot;Enter an integer value ( enter 0 to stop): &amp;quot;);&lt;br /&gt;
     scanf (&amp;quot;%d&amp;quot;, &amp;amp;input);&lt;br /&gt;
     count++;&lt;br /&gt;
     numbers = (int*) realloc (numbers, count * sizeof(int));&lt;br /&gt;
     if (numbers == NULL) { &lt;br /&gt;
         puts (&amp;quot;Error (re)allocating memory&amp;quot;); &lt;br /&gt;
         exit ( 1 ); &lt;br /&gt;
     }&lt;br /&gt;
     numbers[ count - 1 ] = input;&lt;br /&gt;
  } while (input!=0);&lt;br /&gt;
  printf (&amp;quot;Numbers entered: &amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  for ( n = 0; n &amp;lt; count; n++) &lt;br /&gt;
      printf (&amp;quot;%d &amp;quot;,numbers[ n ]);&lt;br /&gt;
      &lt;br /&gt;
  free (numbers);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Store string in allocated memory==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char *str[100];&lt;br /&gt;
  int i;&lt;br /&gt;
  for(i = 0; i &amp;lt; 100; i++) {&lt;br /&gt;
    if((str[i] = malloc( 128 )) == NULL) {&lt;br /&gt;
      printf(&amp;quot;Allocation Error\n&amp;quot;);&lt;br /&gt;
      exit(1);&lt;br /&gt;
    }&lt;br /&gt;
    gets(str[i]);&lt;br /&gt;
  }&lt;br /&gt;
  /* now free the memory */&lt;br /&gt;
    &lt;br /&gt;
  for(i = 0; i &amp;lt; 100; i++) &lt;br /&gt;
      free(str[i]);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use malloc to allocate memory==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char *p;&lt;br /&gt;
  p = malloc(80);&lt;br /&gt;
  if( !p ) {&lt;br /&gt;
    printf(&amp;quot;Memory Allocation Failed&amp;quot;);&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  printf(&amp;quot;Enter a string: &amp;quot;);&lt;br /&gt;
  gets( p );&lt;br /&gt;
  &lt;br /&gt;
  printf( p );&lt;br /&gt;
  free( p );&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>