473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

new and delete usage

Hi.

I would like to know something about the code below. I know, that's not
a nice programming style, because before using the new-Operator a
second time, I should use delete first. But its working fine anyway. So
what is the worst that could happen to my program if I leave it like
that?

CODE:
int main()
{
int i,*val;

val = new int[5];

for (i=0;i<5;i++)
val[i]=i;

val = new int[10];

for (i=0;i<10;i++)
val[i]=i;

delete[] val;

return 0;
}

Dec 16 '06 #1
5 1771
Verbal Kint wrote:
Hi.

I would like to know something about the code below. I know, that's not
a nice programming style, because before using the new-Operator a
second time, I should use delete first. But its working fine anyway. So
what is the worst that could happen to my program if I leave it like
that?
If you let your code leak memory, it will eventually use up all the
available memory in your computer and can cause some operating systems
to do stupid unpleasant things.
Dec 16 '06 #2
thanks for the reply!

Dec 16 '06 #3

Verbal Kint wrote:
Hi.

I would like to know something about the code below. I know, that's not
a nice programming style, because before using the new-Operator a
second time, I should use delete first. But its working fine anyway. So
what is the worst that could happen to my program if I leave it like
that?

CODE:
int main()
{
int i,*val;

val = new int[5];

for (i=0;i<5;i++)
val[i]=i;

val = new int[10];

for (i=0;i<10;i++)
val[i]=i;

delete[] val;

return 0;
}
The first thing that might happen is that you develop a nasty habit. In
the case above, there is no reason to be allocating on the heap in the
first place. Whats disturbing with that code, is that you lose the
original pointer with which you reserved the original allocation.
Think of some coder thats comes along, scans the code quickly, assumes
that the delete[] statement above recovers the entire allocation and
proceeds to reuse your code/class with the assumption that no leaks
have occurred.

Using new/delete also makes code more difficult to write (and
maintain).

#include <iostream>
#include <ostream>
#include <vector>

void load(std::vector< int >& r_v)
{
for( size_t i = 0; i < r_v.size(); ++i)
{
r_v[i] = i;
std::cout << "v[" << i << "] ";
std::cout << r_v[i] << std::endl;
}
}

int main()
{
std::vector< int v(5); // a dynamic array of 5 elements
load( v );
v.clear();
v.resize(10);
load( v );
}

Dec 16 '06 #4
"Verbal Kint" <TE********@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
Hi.

I would like to know something about the code below. I know, that's not
a nice programming style, because before using the new-Operator a
second time, I should use delete first. But its working fine anyway. So
what is the worst that could happen to my program if I leave it like
that?
....the other users of the machine might come after you...I'm not sure if
it's the worst thing, but it's certainly unpleasant. Somebody did something
similar (only in bigger chunks) in the lab where I'm working and in the end
it slowed down the server where that program was running so dramatically due
to swapping, that even the sys-admin had a hard time to log in to kill the
process.

There are a lot of things that work - even "undefined behavior" might work
in the sense that it does what you would expect it to do. However, don't
count on it. In order to write clean and reliable code you should always use
delete whenever you write new!

Cheers
Chris
Dec 16 '06 #5
"Verbal Kint" <TE********@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
Hi.

I would like to know something about the code below. I know, that's not
a nice programming style, because before using the new-Operator a
second time, I should use delete first. But its working fine anyway. So
what is the worst that could happen to my program if I leave it like
that?
....the other users of the machine might come after you...I'm not sure if
it's the worst thing, but it's certainly unpleasant. Somebody did something
similar (only in bigger chunks) in the lab where I'm working and in the end
it slowed down the server where that program was running so dramatically due
to swapping, that even the sys-admin had a hard time to log in to kill the
process.

There are a lot of things that work - even "undefined behavior" might work
in the sense that it does what you would expect it to do. However, don't
count on it. In order to write clean and reliable code you should always use
delete whenever you write new!

Cheers
Chris

Dec 16 '06 #6

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

Similar topics

1
by: NotGiven | last post by:
I'd like to delete a record and all its children records at one time. How do I do that? Can you, in one SQL statement, delete from table 1 where id = 3 delete from table 2 where id = 12...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
6
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
5
by: John7000 | last post by:
Hello, I am amateur with VB database usage. I've written a little database that keeps track of names, address, phone numbers etc. It displays the data in a DataGrid and stores them in a...
9
by: Oliver Graeser | last post by:
Hi All, I do some ensemble averaging calculation that requires me to accumulate very many distributions which I store in arrays. Basically something like class AdaptiveNW{ public: int**...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.