954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Memory Allocation and Deallocation

While we allocate memory using malloc/calloc.That much bytes is reserved by the program.When the program terminates and we didnot free it what happens exactly?

How to check memory usage by a program by writeing a C program.

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

>How to check memory usage by a program by writeing a C program.
A profiler is used for such purposes.

>When the program terminates and we didnot free it what happens exactly?
Most OSes (this means: not all!) clean up all the memory assigned to a program when the program shuts down, but it's dangerous to rely upon this when writing software.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>How to check memory usage by a program by writeing a C program. A profiler is used for such purposes.

>When the program terminates and we didnot free it what happens exactly? Most OSes (this means: not all!) clean up all the memory assigned to a program when the program shuts down, but it's dangerous to rely upon this when writing software.


How to check memory usage by a program by writeing a C program. can we do it using C?

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

I need to write a program which will use as memory checker for c programs while running.

i mean to say a function which i can call in between inside a program to check memory usage.

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

You mean like BoundsChecker ?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Sorry I misunderstood you.

>can we do it using C?
Using standard C: no.
Using the OS's API: yes.

>i mean to say a function which i can call in between inside a program to check memory usage.
What exactly must this function check then?
Only memory occupied within the program, or also memory occupied by other programs running?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
You mean like BoundsChecker ?

I can say YES.
and apart from this,My concern is like, while programming how to take care about the following things:Memory leak
Buffer OverFlow
Segmentaion Falut
Bus Error

Can some one illustrute these things with examples?These are most crucial thing while software devlopment.

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

Sorry I misunderstood you.

>can we do it using C? Using standard C: no. Using the OS's API: yes.

>i mean to say a function which i can call in between inside a program to check memory usage. What exactly must this function check then? Only memory occupied within the program, or also memory occupied by other programs running?

memory occupied by the variables in that program only.

e.g
After malloc is called how much memory assigned to a char*
then after calling free it is freed or not.

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

http://c-faq.com/malloc/index.html

* Memory leak
Calling malloc() without calling free().

* Buffer OverFlow
Accesses outside the bounds of your memory. Can equally apply to arrays as well as allocated memory.
A typical example is a for loop which runs to <=N rather than

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

when i write code like below:

char *a;
a=(char*)malloc(100);
strcpy(a,"xyz");   //will not free memory here
a=(char*)malloc(100);
strcpy(a,"abc");

What is problem with memory occurs here?because the code will run fine>

Another case:
in the below code let me know we should allocate memory using malloc or its not required.

char *a;
cahr *b="my name is Dream2code";
a=(char*)malloc(100); /is ir required coz it refreshes in the next line
a=strste(b,"name");
//if memory allocated in this case so how to free it??
Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 
What is problem with memory occurs here?because the code will run fine>


It is called a memory or resource leak. The memory you allocated from the first call to malloc is never freed, and for the rest of the program it will stay allocated and cannot be used anymore because you do not have a pointer to it.

The problem is that memory is finite, and fast memory is even more finite. The more you leak memory, the more often your program will dip into virtual memory pages. This slows the program down and if too much memory is leaked, can crash or freeze the whole machine.
a=(char*)malloc(100); /is ir required coz it refreshes in the next line
a=strste(b,"name");
//if memory allocated in this case so how to free it??

/* memory is allocated and the address is stored in a */
a=(char*)malloc(100);

/* memory leak. a now points somewhere inside b 
and the address returned by malloc is forgotten */
a=strste(b,"name");
//if memory allocated in this case so how to free it??


If you do not have at least one pointer to the address returned by malloc, freeing the memory is impossible. You need to wait until the program ends and the OS reclaims memory for the process. Or if the OS does not reclaim memory for processes, you need to wait until a reboot.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

Dream2Code,

Sure you didn't mean:
a=strst<strong>r</strong>(b,"name");
instead of: a=strste(b,"name");
?

I assume this was a typo because probably ther-key comes just after the e-key on your keyboard.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Dream2Code,

Sure you didn't mean: a=strst<strong>r</strong>(b,"name"); instead of: a=strste(b,"name"); ?

I assume this was a typo because probably ther-key comes just after the e-key on your keyboard.

sory it's a typo.

its strstr().thanks.

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You