开发者

c++ increase buffer size

im implementing a tcp/ip service in c++. what happen is that the server will receive data and each data may contain 1 or more messages. the server is only allow to receive a max of e.g 100 bytes of data per round. so in scenario 1, if a message is of 120 bytes, it will be split into 2 data. so i create a global static char tmpbuffer[100] temporary. upon receviving the data, i check the msg header for the total message size. if its more than 100 bytes, i store the first 100 bytes into tmpbuffer. it will wait for the 2nd loop where the next 100 bytes of data will come.this 100 bytes will contain the remaining 20 bytes which i wanna concat to the tmpbuffer.

however the problem here is that tmpbuffer size incre开发者_StackOverflowases dynamically. lets assume that this function will cater for messages much bigger than just 120 bytes. it could be 1000 bytes, thus the message will be split into 10 data each of 100 bytes. by using char only, can i increase the tmpbuffer size dynamically? that means gloablly i set, static tmpbuffer[100]. in the function, once i know the total message size from the msg header, how can i increase the tmpbuffer size.


Use a std::vector instead. Will save you soooo much hassle.


If you'd like to use raw arrays (for whatever reason), why not declare char* tmpbuffer, and then allocate arrays of chars as large as you need using new char[size]? Since you're obviously not in a multi-threaded environment, if the needed size increases, you should have no problem free[]ing tmpbuffer and creating it again as a larger array.


You cannot increase the size of a statically allocated array. You must use std::vector<char> and use it's resize() function to increase it's size to what you need.


You could also use a std:string here as well and just keep appending to that string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜