日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C语言增量内存申请 realloc

發布時間:2025/3/18 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言增量内存申请 realloc 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C語言增量內存申請 realloc

void* realloc (void* ptr, size_t size); Reallocate memory block

Changes the size of the memory block pointed to by?ptr.
The function may move the memory block to a new location (whose address is returned by the function).

The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. If the new?size?is larger, the value of the newly allocated portion is indeterminate.

【如果申請長度小于原來長度,原來內容被截斷;等于則內容不變,如果申請長度大于原來長度,多出部分的內存,其內容是不確定的,原來內容不變】

【但不管哪種情況,內存地址都可能改變,相等情況下可能不變】

In case that?ptr?is a null pointer, the function behaves like?malloc, assigning a new block of?size?bytes and returning a pointer to its beginning.

【如果ptr是空,則此時函數相當于malloc的功能】

?代碼如下:

1 /* realloc example: rememb-o-matic */ 2 #include <stdio.h> /* printf, scanf, puts */ 3 #include <stdlib.h> /* realloc, free, exit, NULL */ 4 5 int main () 6 { 7 int input,n; 8 int count = 0; 9 int* numbers = NULL; 10 int* more_numbers = NULL; 11 12 do { 13 printf ("Enter an integer value (0 to end): "); 14 scanf ("%d", &input); 15 count++; 16 17 more_numbers = (int*) realloc (numbers, count * sizeof(int)); 18 19 if (more_numbers!=NULL) { 20 numbers=more_numbers; 21 numbers[count-1]=input; 22 } 23 else { 24 free (numbers); 25 puts ("Error (re)allocating memory"); 26 exit (1); 27 } 28 } while (input!=0); 29 30 printf ("Numbers entered: "); 31 for (n=0;n<count;n++) printf ("%d ",numbers[n]); 32 free (numbers); 33 34 return 0; View Code

?

posted on 2018-02-09 18:01 時空觀察者9號 閱讀(...) 評論(...) 編輯 收藏

總結

以上是生活随笔為你收集整理的C语言增量内存申请 realloc的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。