free()会发生什么? [英] what happens to free()?

查看:97
本文介绍了free()会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

我写一个函数将一个内存缓冲区复制到另一个内存缓冲区

当目标不够大时动态分配。 />
代码如下:

void DSM_CC :: fillBuf(unsigned char ** pdstBuf,int * pdstBufBase,int *

pdstTolalLen,char * pbufToWrite ,int lenToWrite)

{

unsigned char * tmp;

int step = 256;

short allocateCnt = 10;

if(* pdstBufBase + lenToWrite> * pdstTolalLen)

{

if(!(* pdstBuf))// pdstBuf = = null

{

while(allocateCnt--)

{

* pdstBuf =(unsigned char *) malloc(lenToWrite + step);

if(* pdstBuf)

break;

}

* pdstTolalLen = lenToWrite + step;

}

else

{

allocateCnt = 10;

while(allocateCnt--)

{

tmp =(unsigned char *)malloc(* pdstBufBase);

if(tmp)

休息;

}

memcpy(tmp,* pdstBuf,* pdstBufBase);

// \\!realloc会使原始数据丢失吗?

realloc (* pdstBuf,* pdstBufBase + lenToWrite +

step); memcpy(* pdstBuf,tmp,* pdstBufBase);

free(tmp);

* pdstTolalLen = * pdstBufBase + lenToWrite + step;

}

}

memcpy(* pdstBuf + * pdstBufBase,pbufToWrite,lenToWrite);

* pdstBufBase + = lenToWrite;

}

当第三次调用该函数时
。异常发生

at linefree(tmp)


Anther:有没有办法确定指针是否有效?

例如,我使用if(!(* pdstBuf))来检测它。但我们知道当

a指针声明时,初始化值可能不是NULL(即'b
0).SO if(!(* pdstBuf))无效时无效带有值的指针

0xccccccc(VC 6.0)

:: Envir0nment:vc6.0 sp6


请为什么? Thx!

Hi,all

I write a function to copy one memory buffer to anther with
dynamic allocationg when the destination is not large enough.
The code is below:
void DSM_CC::fillBuf(unsigned char** pdstBuf,int* pdstBufBase,int*
pdstTolalLen,char* pbufToWrite,int lenToWrite)
{
unsigned char* tmp;
int step = 256;
short allocateCnt = 10;
if(*pdstBufBase + lenToWrite > *pdstTolalLen)
{
if(!(*pdstBuf))//pdstBuf == null
{
while(allocateCnt--)
{
*pdstBuf = (unsigned char*)malloc(lenToWrite + step);
if(*pdstBuf)
break;
}
*pdstTolalLen = lenToWrite + step;
}
else
{
allocateCnt = 10;
while(allocateCnt--)
{
tmp = (unsigned char*)malloc(*pdstBufBase);
if(tmp)
break;
}
memcpy(tmp,*pdstBuf,*pdstBufBase);
//\\!realloc will make the original data lost??
realloc(*pdstBuf,*pdstBufBase + lenToWrite +
step); memcpy(*pdstBuf,tmp,*pdstBufBase);
free(tmp);
*pdstTolalLen = *pdstBufBase + lenToWrite + step;
}
}
memcpy(*pdstBuf+ *pdstBufBase,pbufToWrite,lenToWrite);
*pdstBufBase += lenToWrite;
}

when the function was invoked the third time.An exception happens
at line"free(tmp)"

Anther:Is there any way to deside if a pointer is valid or not?
For example,I use if(!(*pdstBuf)) to detect it.But we know when
a pointer declaration,the initialization value may not be NULL(that''s
0).SO if(!(*pdstBuf)) will not work when an invalid pointer with value
0xccccccc(VC 6.0)

::Envir0nment: vc6.0 sp6

please why? Thx!

推荐答案

-----开始PGP签名消息-----

哈希:SHA1


Jerry写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry wrote:
所有

我写一个函数将一个内存缓冲区复制到另一个内存缓冲区当目的地不够大时动态分配。
代码如下:
void DSM_CC :: fillBuf(unsigned char ** pdstBuf,int * pdstBufBase,int *


我讨厌说出来,但这不是C代码。这就是'这就是为什么你有问题

用它。我的评论如下:如果/你发布了C代码。

pdstTolalLen,char * pbufToWrite,int lenToWrite)
{
unsigned char * tmp;
int step = 256;
short allocateCnt = 10;
if(* pdstBufBase + lenToWrite> * pdstTolalLen)
{
if(!(* pdstBuf))// pdstBuf == null
{
while(allocateCnt--)
{pd / = * pdstBuf =(unsigned char *)malloc(lenToWrite + step);
if(* pdstBuf)
break;
}
* pdstTolalLen = lenToWrite + step;
}

{
allocateCnt = 10;
while(allocateCnt--)
{
tmp =(unsigned char *)malloc(* pdstBufBase);


可疑 - 施放malloc()的结果隐藏了问题。

如果(tmp)
休息;
} memcpy(tmp,* pdstBuf,* pdstBufBase);
// \\!realloc会使原始数据丢失吗?
realloc(* pdstBuf,* pdstBufBase + lenToWrite +
步);的memcpy(* pdstBuf,TMP,* pdstBufBase);


你不能realloc()首先你没有malloc()ed。是

* pdstBuf一个指针malloc()ed存储?

free(tmp);
* pdstTolalLen = * pdstBufBase + lenToWrite + step;
调用函数时,memcpy(* pdstBuf + * pdstBufBase,pbufToWrite,lenToWrite);
* pdstBufBase + = lenToWrite;
}
第三次。例外情况发生在线路free(tmp)


如果你的realloc()正在摆弄第一个

地方没有malloc()编辑的内存,那么它就足够了导致malloc()或free()在

某些点上失败。

Anther:有没有办法确定指针是否有效?
Hi,all

I write a function to copy one memory buffer to anther with
dynamic allocationg when the destination is not large enough.
The code is below:
void DSM_CC::fillBuf(unsigned char** pdstBuf,int* pdstBufBase,int*
I hate to say it, but this isn''t C code. P''haps that''s why you have problems
with it. My comments below will be /as if/ you had posted C code.
pdstTolalLen,char* pbufToWrite,int lenToWrite)
{
unsigned char* tmp;
int step = 256;
short allocateCnt = 10;
if(*pdstBufBase + lenToWrite > *pdstTolalLen)
{
if(!(*pdstBuf))//pdstBuf == null
{
while(allocateCnt--)
{
*pdstBuf = (unsigned char*)malloc(lenToWrite + step);
if(*pdstBuf)
break;
}
*pdstTolalLen = lenToWrite + step;
}
else
{
allocateCnt = 10;
while(allocateCnt--)
{
tmp = (unsigned char*)malloc(*pdstBufBase);
Suspicious - casting the results of malloc() hides problems.
if(tmp)
break;
}
memcpy(tmp,*pdstBuf,*pdstBufBase);
//\\!realloc will make the original data lost??
realloc(*pdstBuf,*pdstBufBase + lenToWrite +
step); memcpy(*pdstBuf,tmp,*pdstBufBase);
You can''t realloc() what you haven''t malloc()ed in the first place. Is
*pdstBuf a pointer malloc()ed storage?
free(tmp);
*pdstTolalLen = *pdstBufBase + lenToWrite + step;
}
}
memcpy(*pdstBuf+ *pdstBufBase,pbufToWrite,lenToWrite);
*pdstBufBase += lenToWrite;
}

when the function was invoked the third time.An exception happens
at line"free(tmp)"
If your realloc() is fiddling with memory that wasn''t malloc()ed in the first
place, it can bollix things up enough to cause malloc() or free() to fail at
some point or another.
Anther:Is there any way to deside if a pointer is valid or not?




编号不在标准C中

[snip]

- -

Lew Pitcher


Master Code Code& JOAT-in-training |可根据要求提供GPG公钥

注册Linux用户#112576( http:/ /counter.li.org/

Slackware - 因为我知道我在做什么。

----- BEGIN PGP SIGNATURE-- ---

版本:GnuPG v1.2.7(GNU / Linux)

iD8DBQFDVbWSagVFX4UWr64RApDzAJwKyIOAwulFjcePq8lWwJ 2Ucwb5HQCg3zks

fRL1oyc53RvnWt2eFRnbSXE =

= Zypu <​​br />
----- END PGP SIGNATURE -----



No. Not in standard C
[snip]
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I''m doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDVbWSagVFX4UWr64RApDzAJwKyIOAwulFjcePq8lWwJ 2Ucwb5HQCg3zks
fRL1oyc53RvnWt2eFRnbSXE=
=Zypu
-----END PGP SIGNATURE-----




Lew Pitcher写道:

Lew Pitcher wrote:
----- BEGIN PGP签名消息-----
哈希:SHA1

Jerry写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry wrote:
memcpy(* pdstBuf,tmp,* pdstBufBase);
你不能realloc()首先你没有malloc()ed。是
* pdstBuf指针malloc()ed存储?
memcpy(*pdstBuf,tmp,*pdstBufBase);
You can''t realloc() what you haven''t malloc()ed in the first place. Is
*pdstBuf a pointer malloc()ed storage?



来自realloc()的手册页


From the man pages for realloc()




realloc()将

ptr指向的内存块大小更改为

size字节。内容将保持最低值



和新尺寸;新分配的内存将是未初始化的。如果

ptr是

NULL,则调用等同于malloc(size);如果大小相等

为零,

该调用相当于free(ptr)。除非ptr为NULL,否则

必须有早期调用malloc(),calloc()或

realloc()返回


如果指向的区域被移动,则自由(ptr)完成。



realloc() changes the size of the memory block pointed to by
ptr to
size bytes. The contents will be unchanged to the minimum of
the old
and new sizes; newly allocated memory will be uninitialized. If
ptr is
NULL, the call is equivalent to malloc(size); if size is equal
to zero,
the call is equivalent to free(ptr). Unless ptr is NULL, it
must have
been returned by an earlier call to malloc(), calloc() or
realloc().
If the area pointed to was moved, a free(ptr) is done.


ncf写道:
Lew Pitcher写道:>
Jerry写道:
Lew Pitcher wrote:>
Jerry wrote:
memcpy(* pdstBuf ,tmp,* pdstBufBase);
memcpy(*pdstBuf,tmp,*pdstBufBase);



你不能realloc()首先你没有malloc()ed。是
* pdstBuf一个指针malloc()ed存储?



You can''t realloc() what you haven''t malloc()ed in the first place. Is
*pdstBuf a pointer malloc()ed storage?



来自realloc()的手册页



From the man pages for realloc()




<以下文本重新格式化>

realloc()将
ptr指向的内存块的大小更改为size字节。内容将保持最小的新旧尺寸;新分配的内存将是未初始化的。
如果ptr为NULL,则调用等同于malloc(size);如果大小等于零,则该调用等同于free(ptr)。除非ptr为NULL,否则它必须由之前调用malloc(),
calloc()或realloc()返回。如果指向的区域被移动,则完成免费(ptr)



<following text reformatted>
realloc() changes the size of the memory block pointed to by
ptr to size bytes. The contents will be unchanged to the minimum of
the old and new sizes; newly allocated memory will be uninitialized.
If ptr is NULL, the call is equivalent to malloc(size); if size is
equal to zero, the call is equivalent to free(ptr). Unless ptr is
NULL, it must have been returned by an earlier call to malloc(),
calloc() or realloc(). If the area pointed to was moved, a free(ptr)
is done.



那么什么?

-

Nick Keighley


so what?
--
Nick Keighley


这篇关于free()会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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