Jump to content
  • Advertisement
Sign in to follow this  

new() question

This topic is 4232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Visual C++ 2005, if I were to make an unlimited memory loop like this: while(1) { new int[1000]; } I would expect to eventually run out of memory. However, the program seems to be able to recycle memory, or reuse it somehow. Is it going into virtual memory? Or does VC++ 2005 have some form of rudimentary garbage collection?

Share this post


Link to post
Share on other sites
Advertisement
good question :)

i dont know the answer, so i ran your program in VC 2003
then viewed the program in the task manager.

The memory usage didn't increase a whole lot (in the Processes tab), but the page file (in the performance tab) size increased steadily until the application finished. So i guess the memory being allocated is being swapped out to virtual memory.

Share this post


Link to post
Share on other sites
The process will quickly eat up available RAM, and then move to paging. Once it starts doing that, things will really start to slow down. It would take quite a bit of time to exhaust a decently large page file at that rate.

Share this post


Link to post
Share on other sites
Nope, no garbage collection, unless you use .NET and gcnew.

This program will run out of memory and crash, but at that rate it will take a while. Try adding a few more zeros and see it more rapidly come to a screeching halt.

Share this post


Link to post
Share on other sites
As that code has no external side effects, it's not guaranteed to run at all.

To find out exactly what your compiler decided to do check the assembly produced.

Share this post


Link to post
Share on other sites
isn't the allocation a side effect because its allocating a dynamic object?
Here is the asm produced:


PUBLIC _main
EXTRN ??2@YAPAXI@Z:NEAR ; operator new
; COMDAT _main
_TEXT SEGMENT
_main PROC NEAR ; COMDAT

; 4 : {

$L272:

; 5 :
; 6 : while(1)
; 7 : { new int[1000]; }

push 4000 ; 00000fa0H
call ??2@YAPAXI@Z ; operator new
add esp, 4
jmp SHORT $L272
_main ENDP
_TEXT ENDS
END


Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
As that code has no external side effects, it's not guaranteed to run at all.

To find out exactly what your compiler decided to do check the assembly produced.


Isn't new() an external side effect?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
new implementation is OS dependant. Just calling new without touching the memory could very well just mark space (in the virtual address space) as reserved. The actual "allocation" as we normally know it happens on first read or write (having physical ram targeted for the specific allocation).

What is probably happening is virtual address space is consumed with each iteration. Eventually, new throws an exception, which is unhandled, and the process exits. Alternatively, the process may exit when virtual address space is exhausted (depending on the implementation of new).


Share this post


Link to post
Share on other sites
Quote:

new doesn't necessarily throw exceptions, and if that's the case the program could run forever.

A standards-compliant implementation of new always throws if allocation fails. The nothrow overload of new (which was not used here) will suppress the bad_alloc exception and instead return null.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!