內存緩沖區解析


一.緩沖區顧名思義即為:內存中開辟的一片緩沖區域
  按類型分為:全緩沖,行緩沖,不帶緩沖
  可以通過標准庫函數setvbuf(_Inout_ FILE * _File, _Inout_updates_opt_z_(_Size) char * _Buf, _In_ int _Mode, _In_ size_t _Size);來設置緩沖區的類型
1.全緩沖:
  當當填滿標准IO的緩存后才進行實際IO操作。
  windows和linux都可以通過給_Mode設為_IOFBF設置全緩沖。
  全緩沖的典型就是對磁盤文件的讀寫。
  緩沖區為空,一次讀滿數據,再寫出。

windows下測試代碼1:

 1 void main()
 2 {
 3     char buf[1024];//緩沖區大小為1024
 4     setvbuf(stdout, buf, _IOFBF, 1024);
 5     printf("中國女排很給力!\n\n");
 6     //while (1)
 7     //{
 8     //    printf("hello");
 9 
10     //    Sleep(10);
11     //}
12     //setvbuf(stdout, NULL, _IONBF, 0);
13     system("pause");
14 }

結果:不會輸出

測試代碼2:

 

 1 void main()
 2 {
 3     char buf[1024];//緩沖區大小為1024
 4     setvbuf(stdout, buf, _IOFBF, 1024);
 5     printf("中國女排很給力!\n\n");
 6     while (1)
 7     {
 8         printf("hello");
 9 
10         Sleep(10);
11     }
12     //setvbuf(stdout, NULL, _IONBF, 0);
13     system("pause");
14 }

 

 

結果:

 

 


2.行緩沖:
  在輸入輸出遇到換行符時,執行真正的IO操作。
  linux輸出默認是行緩沖,以回車結束。windows沒有行緩沖,不能設置,一旦設置變為全緩沖。
  設置行緩沖為:_IOLBF
  這時候輸入的字符先存放至緩沖區,等按下回車鍵時才進行實際IO操作。
  典型代表就是鍵盤輸入數據,每次讀取一行。

windows下測試代碼1:

 

 1 void main()
 2 {
 3     char buf[4096];
 4     setvbuf(stdout, buf, _IOLBF, 4096);
 5     printf("中國女排很給力!");
 6     //while (1)
 7     //{
 8     //    printf("hello");
 9     //}
10     //setvbuf(stdout, NULL, _IONBF, 0);
11     system("pause");
12 }

測試結果:不會直接打印出來,會輸入緩沖區

windows下測試代碼2:

 1 void main()
 2 {
 3     char buf[4096];
 4     setvbuf(stdout, buf, _IOLBF, 4096);
 5     printf("中國女排很給力!\n\n");
 6     //while (1)
 7     //{
 8     //    printf("hello");
 9     //}
10     //setvbuf(stdout, NULL, _IONBF, 0);
11     system("pause");
12 }

測試結果:可以看出設置行緩沖就是設置全緩沖,遇到換行符也不會輸出。

windows下測試代碼3:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<windows.h>
 4 
 5 void main()
 6 {
 7     char buf[1024];//緩沖區大小為1024
 8     setvbuf(stdout, buf, _IOLBF, 1024);
 9     printf("中國女排很給力!\n\n");
10     while (1)
11     {
12         printf("hello");
13 
14         Sleep(10);
15     }
16     setvbuf(stdout, NULL, _IONBF, 0);
17     system("pause");
18 }

測試結果:等緩沖區滿后才一次性輸出

 

 


3.不帶緩沖區:
  直接進行實際的輸入輸出操作。
  windows默認輸出不帶緩沖,linux可以設置setvbuf(stdout, NULL, _IONBF, 0);而不帶緩沖
  典型代表是標准錯誤stderr,這樣可以使錯誤信息盡快顯示出來

windows默認輸出測試代碼:

#include<stdio.h>
#include<stdlib.h>

void main()
{

    printf("中國女排很給力!");
    //setvbuf(stdout, NULL, _IONBF, 0);
    system("pause");
}

結果:直接輸出,不帶緩沖區。

 4.緩沖區的刷新

--所謂刷新對輸出而言是將緩沖區內容立即輸出

windows測試代碼:

 1 void main()
 2 {
 3     char buf[1024];//緩沖區大小為1024
 4     setvbuf(stdout, buf, _IOFBF, 1024);
 5     printf("中國女排很給力!");
 6     fflush(stdout);//刷新屏幕輸出緩沖區,使緩沖區的內容輸出
 7     //while (1)
 8     //{
 9     //    printf("hello");
10 
11     //    Sleep(10);
12     //}
13     //setvbuf(stdout, NULL, _IONBF, 0);
14     system("pause");
15 }

測試結果:對於滿緩沖輸出模式,可以立即輸出緩存內容

 

--對輸入而言是刷新后面的內容被拋棄

測試代碼:

 1 void main()
 2 {
 3     char c = getchar();
 4     printf("%c\n", c);
 5      c = getchar();
 6     printf("%c\n", c);
 7     fflush(stdin);//刷新屏幕輸出緩沖區,使緩沖區的內容輸出
 8      c = getchar();
 9     printf("%c\n", c);
10      c = getchar();
11     printf("%c\n", c);
12 
13     system("pause");
14 }

測試結果:可以看到fflush后面的輸入被丟掉了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM