更新时间:2023-02-11 17:24:55
" jimjim" <是ne ***** @ blueyonder.co.uk> schrieb im Newsbeitrag新闻:0N ***************** @ text.news.blueyonder.co.u k ...你好,>
我来自C背景,以下动态分配指针阵列对我来说很有意义:
#define SIZE 2
int ** p;
p = malloc(SIZE * sizeof(int *));
for(int j = 0; j< SIZE; j ++)
p [j] = malloc(SIZE * sizeof(int)) ;在C ++中,我发现指针数组的动态分配如下所示:
int ** a;
* a = new (int *)[SIZE];
for(int j = 0; j< SIZE; j ++)
a [SIZE] = new int [SIZE];
cout< ;< & a<< " " << a<< " " << & a [0]<< " " << a [0]<< " " << & a [0] [0]
<< endl<< endl;
1.为什么它是* a = new(int *)[SIZE];"而不是a = new(int *)[SIZE];如在C? C ++编译器在
函数main()中表示:无法将'int *''转换为''int * *''
实际上它是a = new int * [SIZE];。 a = new(int *)[SIZE]"也应该编译,至少它与Comeau的在线编译器一样,但是其他几个编译器似乎被那些额外的括号混淆了,我猜这看起来太类似了。
2。 cout输出:0012FF88 008521D4 008521D4 00000000 00000000。我无法理解最后两个零地址。
当你看到你的C背景时你会有什么期望
p = calloc(SIZE,sizeof(int *));
p [SIZE] = calloc(SITE,sizeof(int));
?
你问过未定义的行为而且你得到了它。
问候
Heinz
jimjim写道:你好,
我来自C背景和以下动态分配
数组指针对我来说很有意义:
#define SIZE 2
int ** p;
p = malloc(SIZE * sizeof(int *));
for(int j = 0; j< SIZE; j ++)
p [j] = malloc(SIZE * sizeof(int));
在C ++中,我发现了动态分配指针数组如下所示:
int ** a;
* a = new(int *)[SIZE];
for(int j = 0 ; j< SIZE; j ++)
a [SIZE] = new int [SIZE];
cout<< & a<< " " << a<< " " << & a [0]<< " " << a [0]<< " " << & a [0] [0]
<< endl<< ENDL;
我想,这应该是:
#include< iostream>
const unsigned SIZE = 50;
int main(void){
int ** a;
a = new int * [SIZE ]。 //注意:int周围没有括号*
for(int j = 0; j< SIZE; j ++)
a [SIZE] = new int [SIZE];
std :: cout<< & a<< " " << a<< "
<< & a [0]<< " " << a [0]<< " " << & a [0] [0]
<< std :: endl<< std :: endl;
}
1.为什么它是* a = new(int *)[SIZE];" ;而不是a = new(int *)[SIZE];作为
尝试:a = new int * [SIZE];"
in C? C ++编译器在>函数main()"
2. cout输出:0012FF88 008521D4 008521D4 00000000 00000000中包含:'无法转换''int *''到''int * *''。我无法理解最后两个零地址。
您期望看到什么?
Best
Kai-Uwe Bux
Heinz Ozwirk写道:" jimjim" ; <是ne ***** @ blueyonder.co.uk> schrieb im Newsbeitrag
新闻:0N ***************** @ text.news.blueyonder.co.u k ...你好,
[snip]在C ++中,我发现一个
指针数组的动态分配如下所示:
int ** a;
* a = new( int *)[SIZE];
for(int j = 0; j< SIZE; j ++)
a [SIZE] = new int [SIZE];
cout< < & a<< " " << a<< " " << & a [0]<< " " << a [0]<< " " <<
& a [0] [0]<< endl<< endl;
1.为什么它是* a = new(int *)[SIZE];"而不是a = new(int *)[SIZE];在C中作为
? C ++编译器在>函数main()"
中实现了无法转换'int *''到'int *'''实际上它是& ; a = new int * [SIZE];。 a = new(int *)[SIZE]"也应该编译,至少它与Comeau的在线编译器一样,但其他几个
编译器似乎被那些额外的括号混淆了,这些括号看起来太类似了演员,我猜测。
实际上,a = new(int *)[SIZE]" *不*使用Comeau进行编译:
" ComeauTest.c",第7行:错误:类型为int *的值无法分配给
a
类型为int **的实体
a = new(int *)[SIZE];
^
[snip]
Best
Kai-Uwe Bux
Hello,
I am coming from a C background and the below dynamic allocation of an array
of pointers makes sense to me:
#define SIZE 2
int **p;
p = malloc ( SIZE * sizeof ( int * ));
for(int j=0; j<SIZE; j++)
p[j] = malloc( SIZE * sizeof ( int ) );
In C++, I have found out that the dynamic allocation of an array of pointers
looks like below:
int ** a ;
*a = new (int*)[SIZE];
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
<< endl << endl;
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as in
C? The C++ compiler compains: "Cannot convert ''int *'' to ''int * *'' in
function main()"
2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
understand the last two zero addresses.
TIA
jimjim
"jimjim" <ne*****@blueyonder.co.uk> schrieb im Newsbeitrag news:0N*****************@text.news.blueyonder.co.u k...Hello,
I am coming from a C background and the below dynamic allocation of an array
of pointers makes sense to me:
#define SIZE 2
int **p;
p = malloc ( SIZE * sizeof ( int * ));
for(int j=0; j<SIZE; j++)
p[j] = malloc( SIZE * sizeof ( int ) );
In C++, I have found out that the dynamic allocation of an array of pointers
looks like below:
int ** a ;
*a = new (int*)[SIZE];
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
<< endl << endl;
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as in
C? The C++ compiler compains: "Cannot convert ''int *'' to ''int * *'' in
function main()"
Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also compile, at least it does with Comeau''s online compiler, but several other compilers seem to be confused by those extra parentheses, which looks too similiar to a cast, I guess.
2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
understand the last two zero addresses.
What would you expect with your C background when you see
p = calloc(SIZE, sizeof(int*));
p[SIZE] = calloc(SITE, sizeof(int));
?
You asked for undefined behaviour and you got it.
Regards
Heinz
jimjim wrote:Hello,
I am coming from a C background and the below dynamic allocation of an
array
of pointers makes sense to me:
#define SIZE 2
int **p;
p = malloc ( SIZE * sizeof ( int * ));
for(int j=0; j<SIZE; j++)
p[j] = malloc( SIZE * sizeof ( int ) );
In C++, I have found out that the dynamic allocation of an array of
pointers looks like below:
int ** a ;
*a = new (int*)[SIZE];
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
<< endl << endl;
I think, this should be:
#include <iostream>
const unsigned SIZE = 50;
int main ( void ) {
int ** a ;
a = new int* [SIZE]; // note: there are no parentheses around int*
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
std::cout << &a << " " << a << " "
<< &a[0] << " " << a[0] << " " << &a[0][0]
<< std::endl << std::endl;
}
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as
Try: "a = new int* [SIZE];"
in C? The C++ compiler compains: "Cannot convert ''int *'' to ''int * *'' in
function main()"
2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
understand the last two zero addresses.
What do you expect to see instead?
Best
Kai-Uwe Bux
Heinz Ozwirk wrote:"jimjim" <ne*****@blueyonder.co.uk> schrieb im Newsbeitrag
news:0N*****************@text.news.blueyonder.co.u k...Hello, [snip] In C++, I have found out that the dynamic allocation of an array of
pointers looks like below:
int ** a ;
*a = new (int*)[SIZE];
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " <<
&a[0][0] << endl << endl;
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as
in C? The C++ compiler compains: "Cannot convert ''int *'' to ''int * *'' in
function main()"
Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
compile, at least it does with Comeau''s online compiler, but several other
compilers seem to be confused by those extra parentheses, which looks too
similiar to a cast, I guess.
Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:
"ComeauTest.c", line 7: error: a value of type "int *" cannot be assigned to
an
entity of type "int **"
a = new (int*) [SIZE];
^
[snip]
Best
Kai-Uwe Bux