16,471
社区成员
发帖
与我相关
我的任务
分享
//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
//在处理的时候每次从新buffer中取两个字节打印
#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#include <windows.h>
#include <process.h>
#include <io.h>
#define MYVOID void
#define vsnprintf _vsnprintf
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define MYVOID void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef _MSC_VER
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt||0==pszFmt[0]) return;
vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
#define ASIZE 200
#define BSIZE 240
#define CSIZE 2
char Abuf[ASIZE];
char Cbuf[CSIZE];
CRITICAL_SECTION cs_HEX ;
CRITICAL_SECTION cs_BBB ;
struct FIFO_BUFFER {
int head;
int tail;
int size;
char data[BSIZE];
} BBB;
int No_Loop=0;
void HexDump(int cn,char *buf,int len) {
int i,j,k;
char binstr[80];
Lock(&cs_HEX);
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%03d %04x -",cn,i);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
Log("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
Log("%s\n",binstr);
}
Unlock(&cs_HEX);
}
int GetFromRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {
int lent,len1,len2;
lent=0;
Lock(cs);
if (fbuf->size>=len) {
lent=len;
if (fbuf->head+lent>BSIZE) {
len1=BSIZE-fbuf->head;
memcpy(buf ,fbuf->data+fbuf->head,len1);
len2=lent-len1;
memcpy(buf+len1,fbuf->data ,len2);
fbuf->head=len2;
} else {
memcpy(buf ,fbuf->data+fbuf->head,lent);
fbuf->head+=lent;
}
fbuf->size-=lent;
}
Unlock(cs);
return lent;
}
MYVOID thdB(void *pcn) {
char *recv_buf;
int recv_nbytes;
int cn;
int wc;
int pb;
cn=(int)pcn;
Log("%03d thdB thread begin...\n",cn);
while (1) {
sleep_ms(10);
recv_buf=(char *)Cbuf;
recv_nbytes=CSIZE;
wc=0;
while (1) {
pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
if (pb) {
Log("%03d recv %d bytes\n",cn,pb);
HexDump(cn,recv_buf,pb);
sleep_ms(1);
} else {
sleep_ms(1000);
}
if (No_Loop) break;//
wc++;
if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
}
if (No_Loop) break;//
}
#ifndef _MSC_VER
pthread_exit(NULL);
#endif
}
int PutToRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {
int lent,len1,len2;
Lock(cs);
lent=len;
if (fbuf->size+lent>BSIZE) {
lent=BSIZE-fbuf->size;
}
if (fbuf->tail+lent>BSIZE) {
len1=BSIZE-fbuf->tail;
memcpy(fbuf->data+fbuf->tail,buf ,len1);
len2=lent-len1;
memcpy(fbuf->data ,buf+len1,len2);
fbuf->tail=len2;
} else {
memcpy(fbuf->data+fbuf->tail,buf ,lent);
fbuf->tail+=lent;
}
fbuf->size+=lent;
Unlock(cs);
return lent;
}
MYVOID thdA(void *pcn) {
char *send_buf;
int send_nbytes;
int cn;
int wc;
int a;
int pa;
cn=(int)pcn;
Log("%03d thdA thread begin...\n",cn);
a=0;
while (1) {
sleep_ms(100);
memset(Abuf,a,ASIZE);
a=(a+1)%256;
if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
send_buf=(char *)Abuf;
send_nbytes=ASIZE;
Log("%03d sending %d bytes\n",cn,send_nbytes);
HexDump(cn,send_buf,send_nbytes);
wc=0;
while (1) {
pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
Log("%03d sent %d bytes\n",cn,pa);
HexDump(cn,send_buf,pa);
send_buf+=pa;
send_nbytes-=pa;
if (send_nbytes<=0) break;//
sleep_ms(1000);
if (No_Loop) break;//
wc++;
if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
}
if (No_Loop) break;//
}
#ifndef _MSC_VER
pthread_exit(NULL);
#endif
}
int main() {
#ifdef _MSC_VER
InitializeCriticalSection(&cs_log);
InitializeCriticalSection(&cs_HEX );
InitializeCriticalSection(&cs_BBB );
#else
pthread_t threads[2];
int threadsN;
int rc;
pthread_mutex_init(&cs_log,NULL);
pthread_mutex_init(&cs_HEX,NULL);
pthread_mutex_init(&cs_BBB,NULL);
#endif
Log("Start===========================================================\n");
BBB.head=0;
BBB.tail=0;
BBB.size=0;
#ifdef _MSC_VER
_beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
_beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);
#else
threadsN=0;
rc=pthread_create(&(threads[threadsN++]),NULL,thdA,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
rc=pthread_create(&(threads[threadsN++]),NULL,thdB,(void *)2);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif
if (!access("No_Loop",0)) {
remove("No_Loop");
if (!access("No_Loop",0)) {
No_Loop=1;
}
}
while (1) {
sleep_ms(1000);
if (No_Loop) break;//
if (!access("No_Loop",0)) {
No_Loop=1;
}
}
sleep_ms(3000);
Log("End=============================================================\n");
#ifdef _MSC_VER
DeleteCriticalSection(&cs_BBB );
DeleteCriticalSection(&cs_HEX );
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_BBB);
pthread_mutex_destroy(&cs_HEX);
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
for(lm=0;lm<count;lm++)
{
{
CString strText = getrowtexT(lm); //获取一行所有列的内容
USES_CONVERSION;
strcpy(bufPtr, W2A(strText.GetBuffer(0))); //转换为非unicode,确定这里不会越界
}
fwrite(bufPtr, 1, strlen(bufPtr), pfile);
}
中的strcpy()一行,我确定它是没有越界的,strText的长度最多100多字节,而bufPtr我分配了几k的空间
for(lm=0;lm<count;lm++)
{
{
CString strText = getrowtexT(lm); //获取一行所有列的内容
USES_CONVERSION;
strcpy(bufPtr, W2A(strText.GetBuffer(0))); //转换为非unicode,确定这里不会越界
}
fwrite(bufPtr, 1, strlen(bufPtr), pfile);
}
说明,我实际的getrowtext()函数是通过参数传出的内容:void getrowtext(int rowindex, CString &strText); 上面的那种写法只是为了方便说明流程
我屏蔽掉A部分后,我每次测试对strText附不同长度的字符串,发现每次的fwrite()里的retvalue地址变小的幅度是不同的,但相同长度的strText其变小的幅度是一致的。
我不屏蔽任何部分,但是我对B部分做个条件:当 lm%2==0 时才进行多字节的我,另外一半则不转换(此时bufPtr保持的是上一次转换的结果),我发现在不转换时,fwrite()中的retvalue地址就不会变小,如果转换就会变小。
下面是我的解决办法
我把这个多字节转换放到一个单独的函数中就行了,大致如下:
void GetChars(char *bufOut, CString strInfo)
{
USES_CONVERSION;
strcpy(bufOut, W2A(strInfo.GetBuffer(0)));
strInfo.ReleaseBuffer();
}
void ExportToFile( CString strFileName )
{
// 1. 变量定义, count为列表的总记录数
...
//
FILE *pFile = _tfopen(strFileName.GetBuffer(0), TEXT("a+b"));
// 3. 写文件
for(lm=0;lm<count;lm++)
{
strText = getrowtexT(lm); //获取一行所有列的内容
GetChars(bufPtr, strText); //转换为非unicode,确定这里不会越界
fwrite(bufPtr, 1, strlen(bufPtr), pfile);
}
}
fclose(pFile);
pFile =NULL;
delete[] bufPtr;
bufPtr = NULL;
这有点奇怪,放在代码段里难道还是会活在整个函数过程中?
void ExportToFile( CString strFileName )
{
// 1. 变量定义, count为列表的总记录数
...
//
FILE *pFile = _tfopen(strFileName.GetBuffer(0), TEXT("a+b"));
strText = TEXT("这里附值,主要包含中文字符... 长度大约字符"); // 这里实际是获取列标题的内容,为了测试,就把这个内容复制了一份加到strText后面,即strText的内容是两个列标题,总长度大约是178个字符,目的是使要写入的文件内容多一些,容易出现stack over flow
// 3. 写文件
for(lm=0;lm<count;lm++)
{
{
USES_CONVERSION;
strcpy(bufPtr, W2A(strText.GetBuffer(0))); //转换为非unicode,确定这里不会越界
strText.ReleaseBuffer(); // 这里加了释放的调用
}
fwrite(bufPtr, 1, strlen(bufPtr), pfile);
}
}
fclose(pFile);
pFile =NULL;
以上的这样修改还是不行,一样的错
void ExportToFile( CString strFileName )
{
// 1. 变量定义, count为列表的总记录数
...
//
FILE *pFile = _tfopen(strFileName.GetBuffer(0), TEXT("a+b"));
// 3. 写文件
for(lm=0;lm<count;lm++)
{
strText = getrowtexT(lm); //获取一行所有列的内容
{
USES_CONVERSION;
strcpy(bufPtr, W2A(strText.GetBuffer(0))); //转换为非unicode,确定这里不会越界
}
fwrite(bufPtr, 1, strlen(bufPtr), pfile);
}
}
fclose(pFile);
pFile =NULL;
以上为这个函数的主要流程,就是获取列表中的总行数,打开一个文件,然后循环获取每一行的内容后转换成非unicode然后写入到文件中。
把 USES_CONVERSION 放入到一个代码段中,是为了保证在出了这一块区域后其定义的变量自动销毁。
在实际的运行过程中,通过断点发现,USES_CONVERSION 中定义的变量 _convert 的地址每一次循环都是一样的,也就是说栈并没有不断增大的情况,但是跟到 fwrite() 中发现,这个函数中定义的一个变量
size_t retval = 0;
在每次进来时,发现这个变量的地址总是要减小240个字节。这个就有点奇怪了,不知道是咋回事
void ExportToFile( CString strFileName )
{
// 1. 变量定义, count为列表的总记录数
...
//
FILE *pFile = _tfopen(strFileName.GetBuffer(0), TEXT("a+b"));
// 3. 写文件
for(lm=0;lm<count;lm++)
{
strText = getrowtexT(lm);
{
USES_CONVERSION;
}
}
}