64,686
社区成员
发帖
与我相关
我的任务
分享
//class 定义
class CBuffer
{
public:
int pos;
char buf[0];
public:
CBuffer()
{
memset(this,0,sizeof(CBuffer));
}
void addBuf(char *data,int len)
{
memcpy(buf+pos,data,len);
pos+=len;
}
protected:
private:
};
//调用
CBuffer *buf = new CBuffer;
char *data="1234567890";
int len=strlen(data);
buf->addBuf(data,len);
printf("%s\n",buf->buf);
delete buf; //err - 1
buf = NULL;
//err -1
void __cdecl _unlock (
int locknum
)
{
/*
* leave the critical section.
*/
LeaveCriticalSection( _locktable[locknum].lock ); //定位
}
class CBuffer
{
public:
struct s{
int pos;
char buf[0];
};
struct s *ps;
public:
CBuffer()
{
ps = (struct s*)malloc(sizeof(s) + 128);
ps->pos = 0;
memset(ps->buf,0,128);
}
~CBuffer()
{
free(ps);
}
void addBuf(const char *data,int len)
{
memcpy(ps->buf + ps->pos,data,len);
ps->pos+=len;
}
};
int main()
{
CBuffer *buf = new CBuffer;
const char *data="1234567890";
int len=strlen(data);
buf->addBuf(data,len);
printf("%s\n",buf->ps->buf);
delete buf;
buf = NULL;
return 0;
}
#include<iostream>
using namespace std;
class CBuffer
{
public:
int pos;
char buf[0];
public:
CBuffer()
{
memset(this,0,sizeof(CBuffer));
}
void addBuf(char *data,int len)
{
memcpy(buf-4,data,len);
//pos+=len;
}
protected:
private:
};
int main()
{
cout<<sizeof(CBuffer)<<endl;
CBuffer *buf1 = new CBuffer;
char *data="123";
int len=strlen(data);
buf1->addBuf(data,len);
printf("%s\n",buf1->buf-4);
delete buf1; //err - 1
buf1 = NULL;
system("pause");
return 0;
}
给你一个版本