大家都知道用new分配内存最后要用delete删除,以避免内存泄露.
现在有几种情况请问该怎么删除;
1.char buf=new[size];
original-size=size;
SomeFunction(buf,&size); //返回时,buf的size发生了变化;buf内容也变化了
delete [] buf ;//此时的大小是original-size,还是size
2.以下delete为什么出错?
const char hello[] = "hello, hello!";
long len = strlen(hello)+1;
char *compr=new char[len]; //分配内存
long comprLen=100; int (*compress)(Byte *,long *,const Byte *,long);
HINSTANCE ZlibHandle=LoadLibrary("zlib.dll");//调用dll
compress=(int(*)(Byte *,long *,const Byte *,long)) GetProcAddress (ZlibHandle,"compress");
int err;
err = compress(compr, &comprLen, (const Byte *)hello, len);
//comprLen,compr都发生了变化
FreeLibrary(ZlibHandle);
delete[] compr;///此处出错
现在有几种情况请问该怎么删除;
1.char buf=new[size];
original-size=size;
SomeFunction(buf,&size); //返回时,buf的size发生了变化;buf内容也变化了
delete [] buf ;//此时的大小是original-size,还是size
2.以下delete为什么出错?
const char hello[] = "hello, hello!";
long len = strlen(hello)+1;
char *compr=new char[len]; //分配内存
long comprLen=100; int (*compress)(Byte *,long *,const Byte *,long);
HINSTANCE ZlibHandle=LoadLibrary("zlib.dll");//调用dll
compress=(int(*)(Byte *,long *,const Byte *,long)) GetProcAddress (ZlibHandle,"compress");
int err;
err = compress(compr, &comprLen, (const Byte *)hello, len);
//comprLen,compr都发生了变化
FreeLibrary(ZlibHandle);
delete[] compr;///此处出错
解决方案 »
- typedef TOOLS_W64 Size32 Size;出错,高手进!!
- sql server 数据库,打开时用_ConnectionPtr::Open(),为什么会卡好几秒钟
- || 一个关于 Windows Media Player 9.0 控件的问题 ||
- MFC的组合框控件问题
- 求助,SDI程序,不会写。
- 请问视图怎么延时刷新,就是必须两次刷新间隔在一定时间以上,谢谢
- 如果用vc编写,安装和 卸载程序,谢谢
- ACE初始环境的设置
- 做为服务器端,如何主动连接客户端!!!急不可待!!!!
- MFC编写小程序,发布后,安装运行,保存到EXCEL时候,出现未指定错误?
- DialogBox(thishins,MAKEINTRESOURCE(105),hwnd,(DLGPROC)Dlgfun)
- 像红警,如何做单机多个player,computer player也是direct player吗?我是想知道这种程序结构
用new分配,怎么使buf的size变化?2. 还是这句话,用new分配,怎么使compr发生变化(不是指内容)?要是变化了,再去删除,当然出错。
~~~~~~~~~~~~~~~~~~~何以见得?
难道莫非是这样的变化么???
void SomeFunction(buf,&size) {
size++; ... size++;
}请你好好研究一下指针参数的传递,即使你在SomeFunction()中有:
void SomeFunction(pBUF,&size) {
...
pBUF = new char[new_size];
...
}
也不会影响到buf的"size"的! delete [] buf ;//此时的大小是original-size
original-size=size;
SomeFunction(buf,&size); //返回时,buf的size发生了变化;buf内容也变化了
delete [] buf ;//此时的大小是original-size,还是size
---it's orginal size.
2.以下delete为什么出错?
const char hello[] = "hello, hello!";
long len = strlen(hello)+1;
char *compr=new char[len]; //分配内存
long comprLen=100; int (*compress)(Byte *,long *,const Byte *,long);
HINSTANCE ZlibHandle=LoadLibrary("zlib.dll");//调用dll
compress=(int(*)(Byte *,long *,const Byte *,long)) GetProcAddress (ZlibHandle,"compress");
int err;
err = compress(compr, &comprLen, (const Byte *)hello, len);
//comprLen,compr都发生了变化
FreeLibrary(ZlibHandle);
delete[] compr;///此处出错----- you function broken the stack.if you had make sure there is no problem in you compress fun.
try int (WINAPI *compress)(Byte *,long *,const Byte *,long);
compress=(int(WINAPI *)(Byte *,long *,const Byte *,long)) GetProcAddress (ZlibHandle,"compress");