Re: anything wrong with this code?
On 7 Mar, 08:21, "hijkl" <s_mah...@yahoo.com> wrote:
hey guys
anything wrong with this code??
if it is then what?
First of all you are using pointers, which is generally no a good
idea. And second, you use an array, we generally prefer that you use a
vector.
int *array(int n){
return new int(n);
return new int[n];
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
It is often preferred to use ++i instead, which is at least as fast as
i++ but can potentially be faster, especially when working with
iterators instead of integers.
p[i] = 0;
}
printf( "%d\n", p[0] );
Memory leak, you forgot to free the memory you allocated on the first
call to array(), insert delete[] p;
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
I fail to see the point of your code, is it to show that a variable
allocated with new is uninitialized?
--
Erik Wikstr=F6m