473,383 Members | 1,829 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,383 software developers and data experts.

memeory related

I have allocated memory dynamically and I am not at all using delete[]. Now
suppose my application is demanding for more memory. And I allocate it again
without using delete[].
What all can happen eventually? Can anyone help.

Thanx
Dec 11 '06 #1
10 1158
If exception handling for std::bad_alloc isn't done around the memory
allocation statement, then the application would run out of memory
allocated to it and would be abnormally terminated by the Operation
System; else std::bad_alloc exception would be thrown.

Dec 11 '06 #2
Sorry for the typo "Operation"! Please read it as "Operating".

Thanks!

frame wrote:
If exception handling for std::bad_alloc isn't done around the memory
allocation statement, then the application would run out of memory
allocated to it and would be abnormally terminated by the Operation
System; else std::bad_alloc exception would be thrown.
Dec 11 '06 #3
"subrat" <su***********@in.bosch.comwrote in message
news:el**********@news4.fe.internet.bosch.com...
>I have allocated memory dynamically and I am not at all using delete[]. Now
suppose my application is demanding for more memory. And I allocate it
again
without using delete[].
What all can happen eventually? Can anyone help.

Thanx
Just run this program and you'll see. After you run it you might want to
reboot your computer.

int main()
{
int* p;
while (true)
{
p = new int[10000];
}
}

What will happen is that you will run out of memory. Since the memory is
newed and not deleted it's never released back to the program (or the OS) so
eventually you'll run out of memory.
Dec 11 '06 #4
subrat wrote:
I have allocated memory dynamically and I am not at all using delete[].
Now suppose my application is demanding for more memory. And I allocate it
again without using delete[].
What all can happen eventually? Can anyone help.
Eventually you will run out of memory. In that case, new() will fail and
throw (unless you use the no-trow version, in which case it will return 0).
How you handle that situation is up to you.

Some operating systems allow for overcommitment of memory. In this case,
your program may not notice that it is running out of memory and the OS
might kill the process. However, this is outside the scope of the C++
standard and outside the scope of this news group, as well.
Best

Kai-Uwe Bux
Dec 11 '06 #5

subrat wrote:
I have allocated memory dynamically and I am not at all using delete[].
Stop! Thats undefined behaviour. There is no need to read the rest of
your question.
Since you are both removing the compiler's responsability to recover
that allocation and you have chosen to ignore the discipline of
deallocating what you new, what happens from this point on could be
anything. It might crash your computer, format your hard drive and even
maybe succeed. Regardless: it is not defined and therefore the only
answer is that its UB.
Now
suppose my application is demanding for more memory. And I allocate it again
without using delete[].
What all can happen eventually? Can anyone help.

Thanx
It still puzzles me why a smart_ptr isn't at least considered in a case
like this. These are easier and safer to use than new/delete.
Example:
std::auto_ptr - if you don't need copies
boost::shared_ptr - if you do need the above with a ref count but its
not an array
boost::shared_array - if you need to allocate an array

Dec 11 '06 #6
Salt_Peter wrote:
>
subrat wrote:
>I have allocated memory dynamically and I am not at all using delete[].

Stop! Thats undefined behaviour.
No, it isn't. It may or may not be a memory leak, but there is, as far as I
know, no requirement in the standard that memory allocated has to be
released. What is undefined is releasing the memory using a non-matching
mechanism, e.g., using free for new()ed memory or delete for new[]ed
memory.
There is no need to read the rest of
your question.
Since you are both removing the compiler's responsability to recover
that allocation and you have chosen to ignore the discipline of
deallocating what you new, what happens from this point on could be
anything.
Now, at which line of the program would the undefined behavior start in the
following example:

#include <iostream>
int main ( void ) {
int * ip = new int ( 5 );
std::cout << *ip << '\n';
}
It might crash your computer, format your hard drive and even
maybe succeed. Regardless: it is not defined and therefore the only
answer is that its UB.
If you really think it's UB, please provide a quote from the standard.
Of course, none of this does addresses the question whether deallocating
memory is good programming practice.
Best

Kai-Uwe Bux
Dec 11 '06 #7

Kai-Uwe Bux wrote:
Salt_Peter wrote:

subrat wrote:
I have allocated memory dynamically and I am not at all using delete[].
Stop! Thats undefined behaviour.

No, it isn't. It may or may not be a memory leak, but there is, as far as I
know, no requirement in the standard that memory allocated has to be
released. What is undefined is releasing the memory using a non-matching
mechanism, e.g., using free for new()ed memory or delete for new[]ed
memory.
Frankly speaking, i couldn't care less what the standard says on this
subject.
If you allocate with new and fail to take the responsability to
deallocate, its UB. Period.

Surely, we aren't going to start telling newbies that newing
allocations and relying on the process to end for recovery is
accepteable or even pheaseable.
Sorry, i disagree with that doctrine emphatically.

If you feel that the standard is the ultimate rule-book that dictates
what is and what ain't, then get the standard changed. Ignoring the
discipline of newed allocations in Java is fine, but not C++ and i
don't need a standard to tell me otherwise.

You might need a standard to tell you when Monday is Monday, but i
don't.
>
There is no need to read the rest of
your question.
Since you are both removing the compiler's responsability to recover
that allocation and you have chosen to ignore the discipline of
deallocating what you new, what happens from this point on could be
anything.

Now, at which line of the program would the undefined behavior start in the
following example:

#include <iostream>
int main ( void ) {
int * ip = new int ( 5 );
std::cout << *ip << '\n';
}
It might crash your computer, format your hard drive and even
maybe succeed. Regardless: it is not defined and therefore the only
answer is that its UB.

If you really think it's UB, please provide a quote from the standard.
Of course, none of this does addresses the question whether deallocating
memory is good programming practice.
Best

Kai-Uwe Bux
Dec 11 '06 #8
Salt_Peter wrote:
Kai-Uwe Bux wrote:
>Salt_Peter wrote:
>>>
subrat wrote:
I have allocated memory dynamically and I am not at all using
delete[].

Stop! Thats undefined behaviour.

No, it isn't. It may or may not be a memory leak, but there is, as
far as I know, no requirement in the standard that memory allocated
has to be released. What is undefined is releasing the memory using
a non-matching mechanism, e.g., using free for new()ed memory or
delete for new[]ed memory.

Frankly speaking, i couldn't care less what the standard says on this
subject.
If you allocate with new and fail to take the responsability to
deallocate, its UB. Period.
The term "undefined behavior" is used in this newsgroup with a very
specific meaning: the behavior of a program, which not defined by the
*Standard*. You cannot use "UB" in the same paragraph where you say
that you don't care what Standard says. That's plain nonsense.

If you meant to say that not deallocating is "bad", or "frowned upon",
or "foolish", or "dangerous", then use those terms. Do not use the
term "undefined behavior". Period.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 11 '06 #9

Salt_Peter wrote:
Frankly speaking, i couldn't care less what the standard says on this
subject.
Then you're off-topic.

Gavin Deane

Dec 11 '06 #10
Salt_Peter wrote:
>
Kai-Uwe Bux wrote:
>Salt_Peter wrote:
>
subrat wrote:
I have allocated memory dynamically and I am not at all using
delete[].

Stop! Thats undefined behaviour.

No, it isn't. It may or may not be a memory leak, but there is, as far as
I know, no requirement in the standard that memory allocated has to be
released. What is undefined is releasing the memory using a non-matching
mechanism, e.g., using free for new()ed memory or delete for new[]ed
memory.

Frankly speaking, i couldn't care less what the standard says on this
subject.
If you allocate with new and fail to take the responsability to
deallocate, its UB. Period.
Nope.
Surely, we aren't going to start telling newbies that newing
allocations and relying on the process to end for recovery is
accepteable or even pheaseable.
Sorry, i disagree with that doctrine emphatically.
Me too. But that is not the claim at issue. The claim at issue is not
whether such code is acceptable. The question is whether it is UB.
If you feel that the standard is the ultimate rule-book that dictates
what is and what ain't, then get the standard changed. Ignoring the
discipline of newed allocations in Java is fine, but not C++ and i
don't need a standard to tell me otherwise.

You might need a standard to tell you when Monday is Monday, but i
don't.
[snip]

You are missing the point entirely. You do need the standard to tell when
something is undefined behavior: this term is defined by and only by the
standard. You do not need the standard to tell that not deleting what you
new()ed is _bad_. But that is an entirely different matter (as I already
indicated in my first response).
Best

Kai-Uwe Bux
Dec 11 '06 #11

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

Similar topics

3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
17
by: Sharon | last post by:
I Have a PC with dual XEON CPU’s and 4 Giga Byte RAM win Windows XP Pro. I have 2 problems with it: (1) Windows is showing only 3.25 Giga byte on the System Properties ? General tab. While the...
2
by: Danial Juka | last post by:
Hi All, I am making a content management system in asp.net. I want to show max 5 related links to other articles that somehow relate to the current article. An example of what I am looking...
2
by: Jim in Arizona | last post by:
Usually, If i need special formatting, I don't use the datagrid control and use a loop that processes a table for each record read from the database (as in classic asp) like so: ...
9
by: clintonG | last post by:
The Barnes & Noble stores in and around Milwaukee, Wisconsin have removed or are removing all of the Microsoft platform programming and support magazines from their shelves. Not one single magazine...
3
by: ss | last post by:
hI, Where do I created the Session Related things in ASP.NET 2.0? How do I do that? Could anybody give some sample code? bye
1
by: David | last post by:
Hi, I have an MS Access app which lets me create a new product by copying data from another selected product. All works fine, except one of the copy updates which I cannot fathom. The code...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
3
by: stair | last post by:
I have 2 tables, 1 table containing a list of items and a second with list of documents related to the items and the status of those documents. It is valid to have no documents related to a...
11
misscrf
by: misscrf | last post by:
I have received data which contains similar records, where the customers have similar records. A customer, i.e. Jane Doe may have moved, or something, or a change to her account. When that happened...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.