Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deleting the Array.

Reply
Thread Tools

Deleting the Array.

 
 
Sabiyur
Guest
Posts: n/a
 
      11-30-2006
Hi All,
I am coding as below.

int *x = new int[10];
int * y= x;
............
.............

del [] y;
x=NULL;

When we are freeing the array, it should free the all memory locations
corresponding to all elements.
The compiler stores the number of elements of array, and releases the
memory accordingly.

So If we use del [] y; Does it knows how many locations to delete?
Becuae y is just copy of x.
I don't know how to test this case. Please help me.

Thanks
Sabiyur

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-30-2006
Sabiyur wrote:
> Hi All,
> I am coding as below.
>
> int *x = new int[10];
> int * y= x;
> ...........
> ............
>
> del [] y;
> x=NULL;
>
> When we are freeing the array, it should free the all memory locations
> corresponding to all elements.
> The compiler stores the number of elements of array, and releases the
> memory accordingly.
>
> So If we use del [] y; Does it knows how many locations to delete?
> Becuae y is just copy of x.
> I don't know how to test this case. Please help me.


Yes, it does. Both 'x' and 'y' are just values. The number of elements
in the array behind that pointer is the implementation business. How it
figures that number from the pointer value is up to it entirely. You can
copy the value as many times as you wish, provided that you eventually
use 'delete[]' to free the memory.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-01-2006

Sabiyur wrote:
> Hi All,
> I am coding as below.
>
> int *x = new int[10];
> int * y= x;
> ...........
> ............
>
> del [] y;
> x=NULL;
>
> When we are freeing the array, it should free the all memory locations
> corresponding to all elements.
> The compiler stores the number of elements of array, and releases the
> memory accordingly.
>
> So If we use del [] y; Does it knows how many locations to delete?


Yes it does, but don't take my word for it - proove it with a dummy
class.
If you add a copy ctor and op=, that dummy is no dummy anymore: a
usefull debugging technique.

#include <iostream>

class A
{
public:
A() { std::cout << "A()\n"; }
~A() { std::cout << "~A()\n"; }
};

int main()
{
A* p_a = new A[5];
A* p_b = p_a;
delete [] p_b;
}

Something else that might interest you:

#include <boost/shared_array.hpp>

int main()
{
boost::shared_array< A > sp_a(new A[5]);
}

 
Reply With Quote
 
t.lehmann@rtsgroup.net
Guest
Posts: n/a
 
      12-01-2006
You can avoid many problems relating to this kind of arrays by using
something like STL. Classes wrapping this kind of arrays are managing
all kind of "low level" memory managment so you needn't invest time to
think about it...

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      12-01-2006

Sabiyur skrev:
> Hi All,
> I am coding as below.
>
> int *x = new int[10];
> int * y= x;
> ...........
> ............
>
> del [] y;
> x=NULL;
>
> When we are freeing the array, it should free the all memory locations
> corresponding to all elements.
> The compiler stores the number of elements of array, and releases the
> memory accordingly.
>
> So If we use del [] y; Does it knows how many locations to delete?
> Becuae y is just copy of x.
> I don't know how to test this case. Please help me.
>

It is okay. But I recommend that you use std::vector instead - this
beast makes your life much, much easier.

/Peter
> Thanks
> Sabiyur


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2006
peter koch wrote:
> Sabiyur skrev:
>> Hi All,
>> I am coding as below.
>>
>> int *x = new int[10];
>> int * y= x;
>> ...........
>> ............
>>
>> del [] y;
>> x=NULL;
>>
>> When we are freeing the array, it should free the all memory
>> locations corresponding to all elements.
>> The compiler stores the number of elements of array, and releases the
>> memory accordingly.
>>
>> So If we use del [] y; Does it knows how many locations to delete?
>> Becuae y is just copy of x.
>> I don't know how to test this case. Please help me.
>>

> It is okay. But I recommend that you use std::vector instead - this
> beast makes your life much, much easier.


....and in some cases your code much much slower...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
t.lehmann@rtsgroup.net
Guest
Posts: n/a
 
      12-01-2006
>
> ...and in some cases your code much much slower...


If your code does need "soooo" much speed then write in assembler! For
all (most) other categories try using object oriented concepts!

You can also think about writing containers youself but STL people and
other libraries doing similar stuff have invested many time over years.
Decide yourself!

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      12-01-2006

Victor Bazarov skrev:
> peter koch wrote:
> > Sabiyur skrev:
> >> Hi All,
> >> I am coding as below.
> >>
> >> int *x = new int[10];
> >> int * y= x;
> >> ...........
> >> ............
> >>
> >> del [] y;
> >> x=NULL;
> >>
> >> When we are freeing the array, it should free the all memory
> >> locations corresponding to all elements.
> >> The compiler stores the number of elements of array, and releases the
> >> memory accordingly.
> >>
> >> So If we use del [] y; Does it knows how many locations to delete?
> >> Becuae y is just copy of x.
> >> I don't know how to test this case. Please help me.
> >>

> > It is okay. But I recommend that you use std::vector instead - this
> > beast makes your life much, much easier.

>
> ...and in some cases your code much much slower...


I believe you will be hard pressed to find modern compilers where
std::vector is "much much slower" than std::vector. I even believe you
will have problems finding a compiler where you will even notice the
difference.
But never mind that. Even if it was the case that std::vector was "much
much slower" (say a factor ten), I'd still recommend std::vector to the
OP and then - if profiling told you - reluctantly advice about using
new []. new [] is so much more errorprone and fragile and the OP
obviously not very experienced.

/Peter
>
> V


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2006
peter koch wrote:
> Victor Bazarov skrev:
>> peter koch wrote:
>>> Sabiyur skrev:
>>>> Hi All,
>>>> I am coding as below.
>>>>
>>>> int *x = new int[10];
>>>> int * y= x;
>>>> ...........
>>>> ............
>>>>
>>>> del [] y;
>>>> x=NULL;
>>>>
>>>> When we are freeing the array, it should free the all memory
>>>> locations corresponding to all elements.
>>>> The compiler stores the number of elements of array, and releases
>>>> the memory accordingly.
>>>>
>>>> So If we use del [] y; Does it knows how many locations to delete?
>>>> Becuae y is just copy of x.
>>>> I don't know how to test this case. Please help me.
>>>>
>>> It is okay. But I recommend that you use std::vector instead - this
>>> beast makes your life much, much easier.

>>
>> ...and in some cases your code much much slower...

>
> I believe you will be hard pressed to find modern compilers where
> std::vector is "much much slower" than std::vector. I even believe you
> will have problems finding a compiler where you will even notice the
> difference.


Visual Studio 2005, optimizing for size, does not inline calls to any
of 'std::vector' members, which in some cases causes too much overhead
for function calls when access to a simle array is sufficient. It is
especially noticeable when done millions of times in a loop.

> But never mind that. Even if it was the case that std::vector was
> "much much slower" (say a factor ten),


How did you guess [the factor] so well?

> I'd still recommend
> std::vector to the OP and then - if profiling told you - reluctantly
> advice about using new []. new [] is so much more errorprone and
> fragile and the OP obviously not very experienced.


Never mind the OP's experience. I was talking in general. And trust
me, I *have* profiled those things.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2006
wrote:
>> ...and in some cases your code much much slower...

>
> If your code does need "soooo" much speed then write in assembler! For
> all (most) other categories try using object oriented concepts!


No, thank you. I can live without assembler, since in most cases C++
is very close to it when using low-level constructs like pointers.

Do not underestimate the effects of calling functions unnecessarily.
The indexing operator is a function. It costs you.

Of course one should not downplay the cost of maintaining code. The
lower the level of constructs, the higher the cost of maintenance.
Every time a higher-level construct is replaced with a lower-level one,
the cost of maintenance needs to be incorporated into the decision
making process. But do not blindly dismiss constructs of the language
of which some people don't have a good grasp.

> You can also think about writing containers youself but STL people and
> other libraries doing similar stuff have invested many time over
> years. Decide yourself!


Yes, one always has to decide. And the decision has to be made based
on measuring the performance instead of some arbitrary investment some
arbitrary "STL people" have made.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
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
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
Deleting a wireless network =?Utf-8?B?TGlubmVjb3I=?= Wireless Networking 3 11-06-2005 05:59 PM
Deleting awireless network connection =?Utf-8?B?SGF3a2V5ZTA3Ng==?= Wireless Networking 0 10-23-2005 12:25 AM
Deleting old Network ? =?Utf-8?B?Q2Fyb2xlIFVL?= Wireless Networking 3 10-05-2005 07:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57