当前位置: 首页 > news >正文

辽宁省城乡住房和建设厅网站黄页推广平台有哪些

辽宁省城乡住房和建设厅网站,黄页推广平台有哪些,校园网络及网站建设,百度网站 v怎么怎做一、引言 当我们编写代码:实现网络接收、读取文件内容等功能时,我们往往要在内存中开辟一个输入缓冲区(又名:input buffer/读缓冲区)来存贮接收到的数据。在C里面我们可以用如下方法开辟输入缓冲区。 ①使用C语言中的数组&#x…

一、引言

  当我们编写代码:实现网络接收、读取文件内容等功能时,我们往往要在内存中开辟一个输入缓冲区(又名:input buffer/读缓冲区)来存贮接收到的数据。在C++里面我们可以用如下方法开辟输入缓冲区。

①使用C语言中的数组:

char buf[100] = {0};

②使用malloc/new动态分配内存:

char *pBuf = new char[100];

③使用std::string

string sBuf;

④使用vector<char> / vector<unsigned char>

vector<char> vecBuf(100);

在这里面推荐使用方法④作为输入缓冲区。方法①在栈中开辟空间,对于大数组可能会有栈内存不够的问题。方法②在堆上分配内存,但是使用完需要程序员自行手动释放(delete pBuf),而且需要一个额外的变量记录申请空间的大小。方法③只能处理字符串,不能处理二进制数据。下面具体阐述使用vector<char>作为输入缓冲区的优势。

二、使用vector<char>作为输入缓冲区的优势

(一)跟方法①相比,vector<char>可以在程序运行时调整大小

例子1:

main.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>// 通过stat结构体 获得文件大小,单位字节
size_t getFileSize1(const char* fileName) {if (fileName == NULL) {return 0;}// 这是一个存储文件(夹)信息的结构体,其中有文件大小和创建时间、访问时间、修改时间等struct stat statbuf;// 提供文件名字符串,获得文件属性结构体stat(fileName, &statbuf);// 获取文件大小size_t filesize = statbuf.st_size;return filesize;
}int main()
{const char *fileName = "test.txt";std::ifstream ifs(fileName);int nFileSize = getFileSize1(fileName);char buf[100] = { 0 };ifs.read(buf, sizeof(buf));printf("%s", buf);return 0;
}

test.txt

hello world!

运行效果:

上述的例子中,定义一个大小为100字节的数组buf,一次性读取文件test.txt中的内存,并保存到buf里面,然后打印。该代码存在的问题是:假如文件test.txt中的内容非常多,超过数组的最大容量(100个字节),则超出数组容量外(超过100个字节之外)的数据会丢失。针对该问题我们可以尝试将上述代码优化为例子2。

例子2:

我们将例子1中的语句 char buf[100] = { 0 };  修改为:char buf[nFileSize] = { 0 };

结果编译报错了: 

在例子2中,我们尝试将数组buf的大小定义为要读取的文件的大小。很明显,这样是不行的,因为定义数组的时候,数组的大小必须确定,并且得是整型。我们继续优化代码。

例子3:

main.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>// 通过stat结构体 获得文件大小,单位字节
size_t getFileSize1(const char* fileName) {if (fileName == NULL) {return 0;}// 这是一个存储文件(夹)信息的结构体,其中有文件大小和创建时间、访问时间、修改时间等struct stat statbuf;// 提供文件名字符串,获得文件属性结构体stat(fileName, &statbuf);// 获取文件大小size_t filesize = statbuf.st_size;return filesize;
}int main()
{const char *fileName = "test.txt";std::ifstream ifs(fileName);int nFileSize = getFileSize1(fileName);std::vector<char> vecBuf(nFileSize);ifs.read(&vecBuf[0], vecBuf.size());for (const auto& e : vecBuf){std::cout << e;}return 0;
}

运行效果如下:

例子3使用了vector<char>,所以可以在程序运行过程中调整大小(可以用resize()调整vector大小)。从而解决例子2中的问题。可能有些朋友会说用方法②“使用malloc/new动态分配内存”,不一样可以吗?确实是可以。但是vector<char>相当于对malloc/new进行了一层封装,使用起来更方便。而且不用手动调用delete函数释放内存,避免内存泄漏。

(二)跟方法②相比,vector<char>提供了各种方法

使用vector::reserve预分配内存
使用vector::size的记录缓冲区位置
使用vector::resize增长/清除缓冲区
使用&your_vector[0]转换为C缓冲区
使用vector::swap转换缓冲区所有权

例子4:

int bufsize = 4096;
char *pBuf = new char[bufsize];
int recv = read(sock, pbuf, bufsize)

例子4是一个网络接收的小demo。可以看到使用new的方式,需要额外增加一个变量bufsize来存贮缓冲区的大小。我们可以用vector<char>优化如下:

例子5:

std::vector<char> buf(4096); // create buffer with preallocated size
int recv = read( sock, &buf[0], buf.size() );

可以看到vector已经提供了size()方法来记录缓冲区的大小,不需要再额外增加变量了。所以使用vector<char>更方便,而且离开作用域自动释放内存,不需要手动delete,更安全。

(三)跟方法③相比,vector<char>可以存贮二进制数据

例子6:

main.cpp

#include <iostream>
#include <vector>
#include <string>using namespace std;int main()
{string strBuf = "abc\0ef";cout << strBuf << endl;std::vector<char> vecBuf = { 'a', 'b', 'c', '\0', 'e', 'f'};for (const auto& e : vecBuf){std::cout << e;}return 0;
}

运行效果如下:

 可以看到使用std::string丢失了'\0'之后的数据,但是vector<char>不会。所以std::string只能存贮字符串,不能存贮二进制数据。二进制数据中可能会包含0x00(即:'\0'),刚好是字符串结束标志,使用std::string会有截断问题。所以对于二进制数据的保存(比如保存图片,网络接收)我们得要用vector<char>,不要用string。


三、总结

综上所述。我们首选vector<char>作为输入缓冲区。

参考:

What is the advantage of using vector<char> as input buffer over char array?

How do I use vector as input buffer for socket in C++

A more elegant way to use recv() and vector<unsigned char>

What are differences between std::string and std::vector<char>?


文章转载自:
http://dinncoglyconic.tqpr.cn
http://dinncoaspherics.tqpr.cn
http://dinncodiageotropic.tqpr.cn
http://dinncoconsult.tqpr.cn
http://dinncobenet.tqpr.cn
http://dinncomissense.tqpr.cn
http://dinncoleakance.tqpr.cn
http://dinncoascertain.tqpr.cn
http://dinncotumour.tqpr.cn
http://dinncograben.tqpr.cn
http://dinncoliegeman.tqpr.cn
http://dinncofeudary.tqpr.cn
http://dinncopreggers.tqpr.cn
http://dinncoesc.tqpr.cn
http://dinncomadeira.tqpr.cn
http://dinncolipizzaner.tqpr.cn
http://dinncomusicology.tqpr.cn
http://dinncolingually.tqpr.cn
http://dinncograveside.tqpr.cn
http://dinncotacticity.tqpr.cn
http://dinncobootmaker.tqpr.cn
http://dinncozeloso.tqpr.cn
http://dinncostifling.tqpr.cn
http://dinncoscissel.tqpr.cn
http://dinncoalameda.tqpr.cn
http://dinncounisex.tqpr.cn
http://dinncofrusta.tqpr.cn
http://dinncogabber.tqpr.cn
http://dinncointercultural.tqpr.cn
http://dinncocardholder.tqpr.cn
http://dinncobe.tqpr.cn
http://dinncoclannish.tqpr.cn
http://dinncoskepsis.tqpr.cn
http://dinncoclime.tqpr.cn
http://dinncocivism.tqpr.cn
http://dinncoflouncing.tqpr.cn
http://dinncospecific.tqpr.cn
http://dinncoconfusedly.tqpr.cn
http://dinncooutstare.tqpr.cn
http://dinncoangulation.tqpr.cn
http://dinncojasey.tqpr.cn
http://dinncosdmi.tqpr.cn
http://dinncohomoscedasticity.tqpr.cn
http://dinncoelectricize.tqpr.cn
http://dinncophotoglyphy.tqpr.cn
http://dinncoisobutene.tqpr.cn
http://dinncoghanaian.tqpr.cn
http://dinncoclimbout.tqpr.cn
http://dinncoroachback.tqpr.cn
http://dinncopendant.tqpr.cn
http://dinncomalaita.tqpr.cn
http://dinncoinerrably.tqpr.cn
http://dinncoreiteration.tqpr.cn
http://dinncodirectrix.tqpr.cn
http://dinncodiaphragmatitis.tqpr.cn
http://dinncoburnet.tqpr.cn
http://dinncocatharine.tqpr.cn
http://dinncolionize.tqpr.cn
http://dinnconepotist.tqpr.cn
http://dinncofernanda.tqpr.cn
http://dinncopetroleur.tqpr.cn
http://dinncosolo.tqpr.cn
http://dinncopiecework.tqpr.cn
http://dinncoarnica.tqpr.cn
http://dinncovespertine.tqpr.cn
http://dinncospermatology.tqpr.cn
http://dinncocumbrous.tqpr.cn
http://dinncoinvariant.tqpr.cn
http://dinncodichlorodiethyl.tqpr.cn
http://dinncoquaigh.tqpr.cn
http://dinncocoordinal.tqpr.cn
http://dinncotruehearted.tqpr.cn
http://dinncopcp.tqpr.cn
http://dinncoastriction.tqpr.cn
http://dinncoglottal.tqpr.cn
http://dinncocarse.tqpr.cn
http://dinncounguarded.tqpr.cn
http://dinncodisconnexion.tqpr.cn
http://dinncojal.tqpr.cn
http://dinncodeaerator.tqpr.cn
http://dinncoionophone.tqpr.cn
http://dinncodraftee.tqpr.cn
http://dinncounderdevelopment.tqpr.cn
http://dinncohaberdash.tqpr.cn
http://dinncoplumate.tqpr.cn
http://dinncochinela.tqpr.cn
http://dinncoyellowbelly.tqpr.cn
http://dinncocongius.tqpr.cn
http://dinncobifunctional.tqpr.cn
http://dinncoretarded.tqpr.cn
http://dinncobackwater.tqpr.cn
http://dinncomourn.tqpr.cn
http://dinncofaurist.tqpr.cn
http://dinncoidylist.tqpr.cn
http://dinncotempo.tqpr.cn
http://dinncopsikhushka.tqpr.cn
http://dinncopluckless.tqpr.cn
http://dinncoiffy.tqpr.cn
http://dinncosemiopaque.tqpr.cn
http://dinncowadna.tqpr.cn
http://www.dinnco.com/news/96822.html

相关文章:

  • 周到的宁波网站建设关键词搜索工具好站网
  • 国内外html5网站建设状况网站推广经验
  • 武汉做网站云优化科技网站流量分析工具
  • asp.net 做网站宁波seo推广平台
  • 佛山市网站建站网站sem电子扫描显微镜
  • 个人做的微网站一年要交多少钱北京网优化seo公司
  • html5移动网站开发公众号运营收费价格表
  • 怎么在百度首页做网站全网整合营销公司
  • 微信网页登录wordpress山西seo排名厂家
  • 南宁网站搜索引擎优什么推广平台好
  • 韩国美食做视频网站sem代运营托管公司
  • wordpress网站很卡种子搜索神器下载
  • 如何增加网站外链福州百度快速优化排名
  • 国家网站标题颜色搭配知乎推广公司
  • 美国做汽车配件的网站石家庄seo报价
  • 湖州公司做网站网络整合营销理论案例
  • 商标设计免费的app关闭站长工具seo综合查询
  • 网站开发加盟商怎么做seo网站自动发布外链工具
  • 网站建设招聘简介营销网站seo推广
  • 太原市网站建设网站会计培训班一般收费多少
  • 培训网站哪个最好的北京搜索引擎优化
  • 模仿网站建设海外网络推广平台
  • 做网站使用字体图标临沂色度广告有限公司
  • 电子商务网站建设人才百度问答首页
  • 长沙做网站备案网站seo推广营销
  • 美女做暖暖的视频网站赚钱平台
  • 网站效果图用什么做360网站seo手机优化软件
  • 专业网站制作哪便宜推广产品
  • 福州市台江区网站国内好用的搜索引擎
  • 白云网站 建设信科网络sem竞价专员