Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > anything wrong with this code?

Reply
Thread Tools

anything wrong with this code?

 
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
hey guys
anything wrong with this code??
if it is then what?

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;
}

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 08:21, "hijkl" <(E-Mail Removed)> 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鰉

 
Reply With Quote
 
 
 
 
mimi
Guest
Posts: n/a
 
      03-07-2007
On 3月7日, 下午3时21分, "hijkl" <(E-Mail Removed)> wrote:
> hey guys
> anything wrong with this code??
> if it is then what?
>
> int *array(int n){
> return new int(n);
> }

This is equal to
int *array(int n)
{
int *i = new int;
*i = 10;
return i;
}
Surely, the functionality is not matching the function name.

> 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;
> }



 
Reply With Quote
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
sorry but u misunderstood.
array is a function name i think ..

 
Reply With Quote
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
or i am not about array()

 
Reply With Quote
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
mimi
but that doesnt matters..its same thing wat u mentioned/

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2007
hijkl wrote:
> sorry but u misunderstood.
> array is a function name i think ..
>

Misunderstood what? Leave some context and drop the silly abbreviations.

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2007
hijkl wrote:
> hey guys
> anything wrong with this code??
> if it is then what?
>

It won't compile and even if it did, it leeks memory.

> 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;
> }
>


--
Ian Collins.
 
Reply With Quote
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

thanks
sanjay

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-07-2007
hijkl wrote:
> ok i tried running this program..it will run fine only problem with
> this code is memory leak..
> there atleast 12 int pointers that needs to free at the end.
>
> thanks
> sanjay
>


Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers. It only allocates *one*. If you want to allocate n
integer you write it like this

int *array(int n){
return new int[n];
}

[n] not (n)

john
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there anything fundamentally wrong with this code? simon.stockton@baesystems.com VHDL 5 04-20-2006 01:09 PM
brut force eigrp over gre working, is there anything wrong? jcharth@hotmail.com Cisco 0 10-05-2005 09:57 PM
not receiving email,is anything wrong? =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 3 07-28-2004 10:32 PM
Is there anything wrong with my insert code? Mano kumar ASP .Net 2 10-17-2003 01:51 PM
Re: Is there anything wrong... rf HTML 1 07-06-2003 11:29 AM



Advertisments