473,804 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting the Array.

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

Nov 30 '06 #1
10 1579
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
Nov 30 '06 #2

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

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

Dec 1 '06 #3
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...

Dec 1 '06 #4

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
Dec 1 '06 #5
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
Dec 1 '06 #6
>
...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!

Dec 1 '06 #7

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
Dec 1 '06 #8
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
Dec 1 '06 #9
t.*******@rtsgr oup.net 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
Dec 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2412
by: georgios zakitraxis | last post by:
Hi all, I have a folder with many pictures in it. All have the name like: HHMMDDMMYYYY.jpg H-hour M-minute D-day M-month Y-year
18
2485
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks for ( j =0; j<MAX ; j++) { for ( i =0; i<MAX ; i++)
3
11727
by: ms_chika | last post by:
Hi again to all, I just want to know how to delete array or array object in javascript? I have this problem, i have an html table that contains textboxes and delete button for each row, the contents of textboxes for each row are stored in an array. When i click the delete button it should delete the row from the table and also delete the row from the array that contains the data, but the problem is it is not deleted from the array...
4
14035
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
4
1654
by: Jim Michaels | last post by:
Is it safe to do what is below? deleting from a resultset while you are processing it? I don't know how dynamic the SQL database is. I assume you get a cursor to live dataset. Even if it is a cursor as opposed to a table, will the cursor's integrity be ruined if the current record is deleted? $q2=mysql_query("SELECT * FROM table1 WHERE id1=5", $link); while ($row2=mysql_fetch_array($q2)) { //do some stuff with resultset...
7
1836
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I have an application that pulls files, folders and registry keys of installed programs. I'm wanting to with a context menu selection of "Delete Selected", delete "ALL" of the checked selected files, folders, registry keys. Can someone show me some code to do this please?
1
3106
by: Pat | last post by:
Hi all, I have a really awkward situation that is causing memory leak problems. I'm passing data to a driver, and unfortunately, the driver code is not something I can change, and it is written in C, so it deals with the data as a big BYTE array. Basically, the driver expects a struct, followed immediately in memory by a big chunk of raw BYTE data. The size of the array of BYTEs is determined by certain members of the struct. So...
13
2467
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or logic would help me accomplish this?
3
3758
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the string array finally runs out of memory. I'd like to set some line limit and once that limit is reached, start removing the first line each time a new line is appended onto the end. I think this is probably more of an array manipulation question than...
9
1686
by: n00b | last post by:
Hello everyone, I just had a question about deleting objects in C#. For example if I have a string array and I wanted to make it grow by one size adding the string "dd" to it. string array = new string {"aa", "bb", "cc"}; string temp = array; array = new string; array = "dd";
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.