malloc()失败后立即realloc()/ free()BYTE * [英] realloc() / free() BYTE* immediately after malloc() fails

查看:143
本文介绍了malloc()失败后立即realloc()/ free()BYTE *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在使用VisualStudio 2005教授的项目中遇到了一个奇怪的问题


当我执行程序时下面我收到此消息:


调试断言失败

程序:...

文件:dbgheap.c

行:1252

表达式:_CrtIsValidHeapPointer(pUserData)


---------- START ----- -----

#include" stdafx.h"


#include< iostream>

using namespace std ;


int main(int argc,char * argv [])

{

unsigned long lU_tile_size = 2956;

//将磁贴数据(jpeg文件)读入缓冲区

unsigned char * byP_jpeg_file =(unsigned char *)

malloc(lU_tile_size);

if(byP_jpeg_file == NULL)

{

wcout<< L" eek - 分配内存失败" ;;

返回1;

}


// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M M M M M M M M M M M b $ b realloc(byP_jpeg_file,0);


返回0;

}

-------- - 结束----------


VS2005设置:

win32命令行可执行文件

ATL是静态链接

多线程调试(/ MTd)

运行时检查:两者

优化已关闭(/ Od)


请帮忙!


~Attila

Hi,

I''m having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

~ Attila

推荐答案

另外:


这个

---------------------------- --------------------------------------

int i_error;

errno = 0; //重置

免费(byP_jpeg_file);

if(errno =!0)

{

wchar_t wct_error [256];

i_error = _wcserror_s(wct_error,256,errno);

if(i_error!= 0)

{

wcout<< L" _wcserror_s()失败" ;;

返回3;

}

wcout<< wct_error;

返回2;

}

-------------------- ----------------------------------------------

而不是realloc()使程序打印


不允许操作。

表示:errno == 1 == EPERM

addition:

this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )
{
wchar_t wct_error[256];
i_error = _wcserror_s( wct_error , 256 , errno );
if( i_error != 0 )
{
wcout << L"_wcserror_s() failed";
return 3;
}
wcout << wct_error;
return 2;
}
------------------------------------------------------------------
instead of realloc() makes the program print

Operation not permitted.
means: errno == 1 == EPERM


attibln写道:
attibln wrote:




我'使用VisualStudio 2005教授的项目有一个奇怪的问题。


当我执行下面的程序时,我收到以下消息:


Debug断言失败

程序:...

文件:dbgheap.c

行:1252

表达式:_CrtIsValidHeapPointer (pUserData)
Hi,

I''m having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)



考虑在新闻组中询问有关VC ++调试的问题

专用于VC ++。看看microsoft.public.vc。*和microsoft.public.vc。*。层次结构。

Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.


>

---------- START ----------

#include" stdafx.h"
>
---------- START ----------
#include "stdafx.h"



这是一个非标准的标题。考虑删除它(至少在这里发布代码的时候是

)。

That''s a non-standard header. Consider removing it (at least when
posting the code here).


>

#include< ; iostream>

使用命名空间std;


int main(int argc,char * argv [])
>
#include <iostream>
using namespace std;

int main( int argc, char * argv[] )



你似乎没有使用''argc''或''argv''。为什么声明它们?

You don''t seem to be using ''argc'' or ''argv''. Why declare them?


{

unsigned long lU_tile_size = 2956;

//读取磁贴数据( jpeg文件)到一个缓冲区

unsigned char * byP_jpeg_file =(unsigned char *)

malloc(lU_tile_size);
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );



''malloc''未定义。考虑包括< cstdlib> ;.

''malloc'' is undefined. Consider including <cstdlib>.


if(byP_jpeg_file == NULL)

{

wcout< < L" eek - 分配内存失败" ;;

返回1;

}


// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M M M M M M M M M M M b $ b realloc(byP_jpeg_file,0);
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );



''realloc''未定义。考虑包括< cstdlib> ;.


''realloc''的行为''size''传递为0是实现

定义。您应该考虑在新闻组中询问您的C ++

编译器(见上文)。

''realloc'' is undefined. Consider including <cstdlib>.

The behaviour of ''realloc'' with ''size'' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).


>

返回0;

}

----------结束----------


VS2005设置:

win32命令行可执行文件

ATL静态链接

多线程调试(/ MTd)

运行时检查:两个

优化已关闭(/ od)


请帮忙!


~Attila
>
return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

~ Attila



V

-

请在回复e时删除资金'A' -mail

我没有回复热门帖子回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On 2007-06-06 16:03 ,attibln写道:
On 2007-06-06 16:03, attibln wrote:




我在使用VisualStudio 2005教授的项目中遇到了一个奇怪的问题。


当我执行下面的程序时,我收到以下消息:


Debug As sertion失败

程序:...

文件:dbgheap.c

行:1252

表达式:_CrtIsValidHeapPointer (pUserData)


---------- START ----------

#include" stdafx。 h"


#include< iostream>

使用命名空间std;


int main(int argc, char * argv [])

{

unsigned long lU_tile_size = 2956;

//将磁贴数据(jpeg文件)读入缓冲区

unsigned char * byP_jpeg_file =(unsigned char *)

malloc(lU_tile_size);

if(byP_jpeg_file == NULL)

{

wcout<< L" eek - 分配内存失败" ;;

返回1;

}


// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M M M M M M M M M M M b $ b realloc(byP_jpeg_file,0);


返回0;

}

-------- - 结束----------


VS2005设置:

win32命令行可执行文件

ATL是静态链接

多线程调试(/ MTd)

运行时检查:两者

优化已关闭(/ Od)


请帮忙!
Hi,

I''m having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!



要么你在其他地方有一些奇怪的设置(当你不使用它时,为什么要静态链接ATL

?)或者你没有向我们展示所有的

代码。我猜测它在相同版本的VS上编译并运行完美

这个事实。


-

Erik Wikstr?m

Either you have some weird settings somewhere else (why do you link ATL
statically when you don''t use it?) or you have not shown us all the
code. This I surmise from the fact that it compiles and runs perfectly
fine for me on the same version of VS.

--
Erik Wikstr?m


这篇关于malloc()失败后立即realloc()/ free()BYTE *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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