帮助将malloc转换为新的 [英] Help converting malloc to new
问题描述
我是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
推荐答案
2006年2月2日14:51:44 -0800,ppateel < PP ***** @ gmail.com>写道:
On 2 Feb 2006 14:51:44 -0800, "ppateel" <pp*****@gmail.com> wrote:
我是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 ++ style ====
pColumnData = new PBYTE [nCols * sizeof(PBYTE)];
" new"不需要告诉被新物体的大小。
因此,这应该是:
pColumnData = new PBYTE [nCols];
您正在分配PBYTE类型的nCols元素数组......
大小是给定的。
pIndicators = new SQLINTEGER [nCols * sizeof(SQLINTEGER)];
pIndicators = new SQLINTEGER [nCols];
pColumnData [nCol] = *(new PBYTE [cbColDataLength])
哎呀......你只是取消引用一个未初始化的指针!此外,你在这里有一个内存泄漏,因为你丢失了由
new []返回的指针。此外,nCol在哪里?界定?如果您的意思是nCols,那么您只需要在数组末尾写一个元素就可以了。
分配。
否则,如果介于0和nCols-1之间,你可以写:
pColumnData [nCol] = new PBYTE [cbColDataLength];
"新"无论如何都返回一个PBYTE *,这就是pColumnData包含的内容,所以
没有必要去掉它(更不用说取消引用了...... ???)。
我尝试使用我对指针的逻辑理解,但编译器不断抛弃我。因此,看看编译器的碎片,我使用上面的语法编译它。请帮我弄清楚这是否正确。
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)];
"new" doesn''t need to be told the size of the object being newed.
Therefore, this should be:
pColumnData = new PBYTE [nCols];
You are allocating an array of nCols elements of type PBYTE ... the
size is a given.
pIndicators = new SQLINTEGER [nCols * sizeof(SQLINTEGER)]; pIndicators = new SQLINTEGER [nCols];
pColumnData[nCol] = *(new PBYTE [cbColDataLength])
Oops ... you just dereferenced an uninitialized pointer! Besides, you
have a memory leak here because you lose the pointer returned by
new[]. Also, where is "nCol" defined? If you meant "nCols", you are
writing to the element one past the end of the array you just
allocated.
Otherwise, if it is between 0 and nCols-1, you can write:
pColumnData[nCol] = new PBYTE [cbColDataLength];
"new" returns a PBYTE* anyway, which is what pColumnData contains, so
there is no need to cast it (much less dereference it...???).
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.
请务必致电delete []当你需要释放数组时,而不是普通删除或free()
。实际上,使用std :: vector< PBYTE>会更好。
。或者std :: vector< SQLINTEGER>对于
您的缓冲区。
-
Bob Hairgrove
没有********** @ Home.com
[回复在comp.lang.c ++ ...中
ppateel写道:
[Replying in comp.lang.c++...]
ppateel wrote:
我是c +的新手,我正在将ac程序转换为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 ++ style ====
pColumnData = new PBYTE [nCols * sizeof(PBYTE)];
在此处删除sizeof。应该只是
pColumnData =新的PBYTE [nCols];
pIndicators = new SQLINTEGER [nCols * sizeof(SQLINTEGER)];
也可以在这里删除sizeof。应该只是
pIndicators = new SQLINTEGER [nCols];
pColumnData [nCol] = *(new PBYTE [cbColDataLength])
这个应该是
for(int col = 0; col< nCol; ++ col)
pColumnData [col ] = new BYTE [cbColDataLength];
我尝试使用我对指针的逻辑理解,但编译器一直让我失望。因此,看看编译器的碎片,我使用上面的语法编译它。请帮我弄清楚这是否正确。
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)];
Drop the sizeof here. Should simply be
pColumnData = new PBYTE[nCols];
pIndicators = new SQLINTEGER [nCols * sizeof(SQLINTEGER)];
Drop the sizeof here as well. Should simply be
pIndicators = new SQLINTEGER[nCols];
pColumnData[nCol] = *(new PBYTE [cbColDataLength])
This one should probably be
for (int col = 0; col < nCol; ++col)
pColumnData[col] = new BYTE[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.
不是。
没有''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
ppateelaécrit:
ppateel a écrit :
IMO,这不是将其转换为C ++的正确方法。
你必须完全重新考虑它。
<编译器vc ++ 7.0(.Net 2003)
SQLINTEGER nCols;
SQLINTEGER cbColDataLength;
PBYTE * pColumnData = NULL;
所有大写类型名称都很棒。
=== C风格====
pColumnData =(PBYTE *)malloc(nCols *的sizeof(PBYTE));
这不是C风格。
你不应该在C中使用malloc()。
pColumnData [ nCol] =(PBYTE)malloc(cbColDataLength)
这很奇怪,你在这里向PBYTE投无效。
=== C ++风格====
pColumnData =新PBYTE [nCols * sizeof(PBYTE)];
* sizeof(PBYTE)表明你不知道新的运营商
是做什么的。它不是像malloc()这样的函数。
它足够聪明,知道如何自己获得PBYTE的大小。
pColumnData [ nCol] = *(新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.
IMO, this is not the correct way to convert it to C++.
You have to rethink it completely.
Compiler vc++ 7.0 (.Net 2003)
SQLINTEGER nCols;
SQLINTEGER cbColDataLength;
PBYTE* pColumnData = NULL;
What wonderful all uppercase type names.
=== C style ====
pColumnData = (PBYTE*) malloc(nCols * sizeof(PBYTE));
That''s not C style.
You shouldn''t cast malloc() in C.
pColumnData[nCol] = (PBYTE) malloc(cbColDataLength)
That''s pretty weird, you''re casting a void* to a PBYTE here.
=== C++ style ====
pColumnData = new PBYTE [nCols * sizeof(PBYTE)];
The "* sizeof(PBYTE)" shows that you don''t know what the new operator
does. It''s not a function like malloc().
It is smart enough to know how to get the size of PBYTE by itself.
pColumnData[nCol] = *(new PBYTE [cbColDataLength])
这对我来说很奇怪。
也许你应该指出你的''重新尝试。
That is strange to me.
Maybe you should point out what you''re trying to do.
这篇关于帮助将malloc转换为新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!