本文介绍了帮助将malloc转换为新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是c ++的新手,我正在将c程序转换为c ++。我更改了

malloc对new的调用,我收到异常违规。这是

相关的一段代码。


编译器vc ++ 7.0(.Net 2003)

SQLINTEGER nCols ;

SQLINTEGER cbColDataLength;

PBYTE * pColumnData = NULL;


=== C style ====

pColumnData =(PBYTE *)malloc(nCols * sizeof(PBYTE));

pIndicators = malloc(nCols * sizeof(SQLINTEGER));

pColumnData [nCol] =(PBYTE)malloc(cbColDataLength)


=== C ++风格====

pColumnData =新PBYTE [nCols * sizeof(PBYTE) )];

pIndicators = new SQLINTEGER [nCols * sizeof(SQLINTEGER)];

pColumnData [nCol] = *(new PBYTE [cbColDataLength])


我尝试使用我对指针的逻辑理解,但编译器

一直让我失望。因此,看看编译器的消息,我得到了使用上面的语法编译的
。请帮我弄清楚

这是否正确。


谢谢

普拉哈拉德

Hi,
I am new to c++ and I am converting a c program to c++. I changed
malloc call to new and I am getting an exception violation. Here is the
relevant piece of code.

Compiler vc++ 7.0 (.Net 2003)

SQLINTEGER nCols;
SQLINTEGER cbColDataLength;
PBYTE* pColumnData = NULL;

=== C style ====
pColumnData = (PBYTE*) malloc(nCols * sizeof(PBYTE));
pIndicators = malloc(nCols * sizeof(SQLINTEGER));
pColumnData[nCol] = (PBYTE) malloc(cbColDataLength)

=== C++ style ====
pColumnData = new PBYTE [nCols * sizeof(PBYTE)];
pIndicators = new SQLINTEGER [nCols * sizeof(SQLINTEGER)];
pColumnData[nCol] = *(new PBYTE [cbColDataLength])

I tried using my logical understanding of pointers but the compiler
kept throwing me out. So looking at the compiler messsages I got it to
compile by using the above syntax. Please help me in figuring out if
this is correct.

Thanks
Prahalad

推荐答案




请务必致电delete []当你需要释放数组时,而不是普通删除或free()

。实际上,使用std :: vector< PBYTE>会更好。

。或者std :: vector< SQLINTEGER>对于

您的缓冲区。


-

Bob Hairgrove





不是。


没有''SQLINTEGER'或

''PBYTE''的定义,原文不清楚(至少在''comp.lang.c ++''中,不要交叉发布到两个组

具有不同的定义区域)。并且

分配的意图是模糊的,但我想我猜它很接近...


V

-

请在邮寄回复时从我的地址删除资金



It''s not.

The original is unclear without the definitions of ''SQLINTEGER'' or
''PBYTE'' (at least in ''comp.lang.c++'', do not cross-post to two groups
that have different areas of definitions). And the intention of the
allocations is murky, but I think I guessed it close...

V
--
Please remove capital As from my address when replying by mail





这对我来说很奇怪。

也许你应该指出你的''重新尝试。



That is strange to me.
Maybe you should point out what you''re trying to do.


这篇关于帮助将malloc转换为新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 02:33