如何清除C中的缓冲区? [英] How to clear the buffer in C?

查看:87
本文介绍了如何清除C中的缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接收器缓冲区,我在其中接收来自recv()的数据;打电话给我想从recv()接收数据后清除缓冲区;调用缓冲区.


I have a receiver buffer in which i am receiving data from the recv(); call i want to clear the buffer after receiving data from the recv(); call in to the buffer.


#define DEFAULT_BUFLEN 512
char recvbuf[DEFAULT_BUFLEN];

int recvbuflen = DEFAULT_BUFLEN;
char common[276];
int main()
{

//After connect and send call...
iResult = recv(ThreadSocket, recvbuf, recvbuflen, 0);
            if ( iResult>0 )
              {
for(int j=0;j<=iResult;j++)
common[j]=recvbuf[j];

//NOW I WANT TO CLEAR recvbuf.......
//fflush can't be used,how can i do so?

}



请用伪代码告诉



Please tell by pseudo code

推荐答案

为什么要清除缓冲区?它会包含敏感信息吗?

我建议您查找memcpy()和memset()函数.

另外,您的"recvbuf"比"common"长,因此从recvbuf复制到common可能会溢出,这很不好.
Why do you want to clear the buffer? Will it contain sensitive information?

I suggest you look up the memcpy() and memset() functions.

Also, your "recvbuf" is longer than "common", so copying from recvbuf to common could overflow, which is Very Bad.


您可能不需要清除此类缓冲区,反正:
You probably don''t need to clear such buffer, anyway:
memset(recvbuf, 0, sizeof(recvbuf));


会做的.




will do the job.



Tarun Batra写道:
Tarun Batra wrote:

for(int j=0;j<=iResult;j++)
common[j]=recvbuf[j];


正如 Graham Breach 所指出的那样,您将使用此类代码来覆盖common缓冲区.

As already noted by Graham Breach you are going to overrun the common buffer with such code.


您想清除缓冲区的所有内容?如果正确,则可以使用for循环将其元素设置为零.
You want to clear all contents of buffer? If that is right you can use a for loop to set elements of it to zero.


这篇关于如何清除C中的缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆