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

scope of dynamic variabe after program termination

hi
a variable is allocated in dynamic memory(using new and is not deleted) and the programs gets terminated .now does the memory allocated will become junk.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int *i = new int;
  4. }
if the above program is executed it just allocates 4 bytes in heap and the program gets terminated.

does this allocated memory will never be free even after program termination.
Feb 2 '08 #1
5 1707
sicarie
4,677 Expert Mod 4TB
Well think about that - if that were true, then every program ever run would keep its memory, and your disk would require ever-larger space, you could never "delete a file" - you would always need more and more space.

So the compiler is nice enough to take care of this for us. Part of the process the compiler goes through when the program terminates is the freeing of all memory that was used during the program.
Feb 2 '08 #2
JosAH
11,448 Expert 8TB
So the compiler is nice enough to take care of this for us. Part of the process the compiler goes through when the program terminates is the freeing of all memory that was used during the program.
A compiler has nothing to do with it; it just delivers compiled object files which
are linked by a linker. The linker produces a certain format file that is condsidered
an executable binary file ('elf' or 'coff' or what have you). When that image is
loaded the instructions are executed; when that process has finished the OS will
remove every trace of it including all memory used by that process. So, yes,
dynamic memory will be lost for the posterity if the process didn't take care of
it itself.

kind regards,

Jos
Feb 2 '08 #3
sicarie
4,677 Expert Mod 4TB
Hm, didn't know it was the OS freeing the memory - thanks Jos!
Feb 2 '08 #4
JosAH
11,448 Expert 8TB
Hm, didn't know it was the OS freeing the memory - thanks Jos!
Yep, the OS is the guilty one: it manages the 'text' space (where the instructions
are) as well as the stack space and the 'data' space (where your heap is stored
normally). When the process is dead the OS reclaims those spaces again and
simply ruins everything that was in there. All open files and other resourses are
closed as well. Consider the OS the reaper of the dead process.

kind regards,

Jos
Feb 2 '08 #5
thank u for u r replys

if a variable is newed twice then will the memory be allocated twice.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. int main()
  3. {
  4.     int *i;
  5.     i = new int; //newed first time
  6.     cout<<&*i<<"   ";
  7.     i = new int; //newed second time
  8.     cout<<&*i; 
  9. }

in the above code the variable i is newed twice and the address of newed location is different on both allocations.

how to find a variable is already newed or not.
Feb 4 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marek Baczek Baczyński | last post by:
:map ,p :!python % <cr> ,p #v+ C:\COMMAND.COM /c python items.py Fatal Python error: unknown scope for super in __init__(14) in items.py symbols: {'multirandrange': 264} locals: {} globals:...
2
by: Jim McGrail | last post by:
Background: I am investigating a problem involving a windows .NET application that is being developed in C# with Visual Studio 2003. It is a multi-threaded application that uses MSMQ to...
3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
7
by: Frank | last post by:
Hi, I have the following problem with dynamic memory: int main(){ for(){ int (**w)=new int *; for(m = 0; m < N1; m++) {
9
by: Adrian | last post by:
Is it defined in the standard what happend to dynamic storage that has not been deleted at program termination? I read through the Dynamic storage section and cannot find it. Adrian
3
by: globalrev | last post by:
i cant figure outif python has lexical or general scope. it seems functions have lexical scope but with some restrictions and some non-function scopes are dynamic?
1
by: D. Susman | last post by:
Hi, When the method "foo" ends, shouldn't the arr array be gone (due to scope termination) and the pointer in class A be invalid? However, when I run the program, I get "2". It seems to that I...
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...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.