这会有用吗? [英] will this work??
问题描述
嘿
这个程序有什么问题???
int * array(int n){
return new int(n);
}
int main(){
int * p = array(10);
for(int i = 0; i< 10; i ++){
p [i] = 0;
}
printf ("%d \ n",p [0]);
p = array(10);
printf("%d \ n",p [0]);
返回0;
}
hey
is anything wrong with this program???
int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
推荐答案
hijkl写道:
hijkl wrote:
嘿
这个程序有什么问题???
hey
is anything wrong with this program???
是的。它不是C.
Yes. It''s not C.
int * array(int n){
return new int(n);
int *array(int n){
return new int(n);
这里是非C结构。
And here''s the non-C construct.
}
int main(){
int * p = array(10);
for(int i = 0; i< 10; i ++){
p [i] = 0;
}
printf("%d \ n",p [0]);
p = array(10);
printf("%d \ n",p [0]);
返回0;
}
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
>这个程序有什么问题???
是的。 C中没有新。
>is anything wrong with this program???
Yes. There is no ''new'' in C.
int * array(int n){
返回新的int(n);
}
int main(){
int * p = array(10);
for(int i = 0; i< 10; i ++){
p [i] = 0;
}
printf("%d \ n",p [0]);
p = array(10);
int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
printf( "%d\n", p[0] );
p = array(10);
上面的行会导致内存泄漏(除了它首先不应该编译的问题)。你已经分配了内存
,然后扔掉了它的最后一个指针。
The above line causes a memory leak (except for the problem that it
shouldn''t compile in the first place). You have allocated memory
and then throw away the last pointer to it.
> printf("%d \ n",p [0]);
返回0;
}
> printf( "%d\n", p[0] );
return 0;
}
是否重要,如果它的c或c ++ ??
可以认为它是c ++ .. :)
does it matters if its c or c++??
ok consider it as c++..:)
这篇关于这会有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!