new and delete usage

V

Verbal Kint

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;

val = new int[10];

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

delete[] val;

return 0;
}
 
G

Gianni Mariani

Verbal said:
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.
 
S

Salt_Peter

Verbal said:
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;

val = new int[10];

for (i=0;i<10;i++)
val=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;
std::cout << "v[" << i << "] ";
std::cout << r_v << std::endl;
}
}

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

Chris Theis

Verbal Kint said:
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
 
C

Chris Theis

Verbal Kint said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top