Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

anything wrong with this code?

 
 
Ian Collins
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.
>

What program? Please keep enough context for your reply to make sense.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 12:08 am, Ian Collins <(E-Mail Removed)> wrote:
> 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.

>
> What program? Please keep enough context for your reply to make sense.
>
> --
> Ian Collins.


same program i posted

 
Reply With Quote
 
 
 
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 12:07 am, John Harrison <(E-Mail Removed)> wrote:
> 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
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 12:07 am, John Harrison <(E-Mail Removed)> wrote:
> 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.


HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p[i] = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2007
hijkl wrote:
> On Mar 7, 12:07 am, John Harrison <(E-Mail Removed)> wrote:
>>
>>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.

>
> HI JOhn
> i understand that this code allocates only one intger.
> but
>
> for( int i = 0; i < 10; i++ ) {
> p[i] = 0;
> }
> this code will initialize next 9 integer to consecutive locations..
> if you will debug then u will notice that..
> let me know if you not agree with me.
>

No, it will write over what ever lives in memory after the single
integer you have allocated.

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2007
hijkl wrote:
> On Mar 7, 12:08 am, Ian Collins <(E-Mail Removed)> wrote:
>
>>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.

>>
>>What program? Please keep enough context for your reply to make sense.
>>

>
> same program i posted
>

On Usenet, each post should make sense on its own, so keep enough
context in your replies. You should also snip people's signatures (the
bit after "-- ").

--
Ian Collins.
 
Reply With Quote
 
hijkl
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 1:09 am, "hijkl" <(E-Mail Removed)> wrote:
> On Mar 7, 12:07 am, John Harrison <(E-Mail Removed)> wrote:
>
>
>
>
>
> > 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.

>
> HI JOhn
> i understand that this code allocates only one intger.
> but
>
> for( int i = 0; i < 10; i++ ) {
> p[i] = 0;
> }
> this code will initialize next 9 integer to consecutive locations..
> if you will debug then u will notice that..
> let me know if you not agree with me.- Hide quoted text -
>
> - Show quoted text -


John
try following code and you will understand how its working
....
for( int i = 0; i < 10; i++ ) {
p[i] = i;
}

printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
....
the output will be
0
1
2
..
..
..
9

so its cleary says that it allocated 10 integers.

thanks
sanjay

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2007
hijkl wrote:
> On Mar 7, 1:09 am, "hijkl" <(E-Mail Removed)> wrote:
>
>>On Mar 7, 12:07 am, John Harrison <(E-Mail Removed)> wrote:
>>
>>>Your main misunderstanding seem to be that you think this code

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

>>
>>>allocates n integers.

>>

<snip>
>
> so its cleary says that it allocated 10 integers.
>

No, it clearly shows that 10 unfortunate memory locations have been
written over.

--
Ian Collins.
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 10:26, "hijkl" <(E-Mail Removed)> wrote:
> On Mar 7, 1:09 am, "hijkl" <(E-Mail Removed)> wrote:
>
>
>
> > On Mar 7, 12:07 am, John Harrison <(E-Mail Removed)> wrote:

>
> > > 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.

>
> > HI JOhn
> > i understand that this code allocates only one intger.
> > but

>
> > for( int i = 0; i < 10; i++ ) {
> > p[i] = 0;
> > }
> > this code will initialize next 9 integer to consecutive locations..
> > if you will debug then u will notice that..
> > let me know if you not agree with me.- Hide quoted text -

>
> > - Show quoted text -

>
> John
> try following code and you will understand how its working
> ...
> for( int i = 0; i < 10; i++ ) {
> p[i] = i;
> }
>
> printf("%d", [0]);
> printf("%d", [1]);
> printf("%d", [2]);
> printf("%d", [3]);
> .
> .
> .
> printf("%d", [9]);
> ...
> the output will be
> 0
> 1
> 2
> .
> .
> .
> 9
>
> so its cleary says that it allocated 10 integers.


It's just luck that keeps this code from crashing, or doing something
worse. What you have is undefined behaviour, to see that just consider
what would happen if the integer you allocated were located on the
last addressable memory location?

--
Erik Wikström

 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 4:21 pm, "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);
> }
> 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;
> }


Seems you are trying to do the following
int *array(int n)
{
return new int[n];
}
int main()
{
UINT ARRAY_SIZE = 10;
int *p = array(ARRAY_SIZE);
memset( array, 0, ARRAY_SIZE))
printf( "%d\n", p[0] );
/* This statement will make the previous allocated memory orphan.
by doing this you are leaving handle the memory location where an
array of
integer pointers allocated. p will take any address which we are
assigning.
putting a delete []p; will avoid memory leak problems
*/
p = array(10);
return 0;
}


 
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