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.

>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.
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?

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.

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?

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.

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.

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 <N
When (if ever) this actually blows up is entirely random.

* Segmentaion Falut
Accessing memory which doesn't exist (or dereferencing NULL).

* Bus Error
Accessing memory with the wrong alignment.
Usually triggered by doing dangerous pointer casts (say char* to int*), then dereferencing it.

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??

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.

Dream2Code,

Sure you didn't mean: a=strst[B]r[/B](b,"name"); instead of: a=strste(b,"name"); ?

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

Dream2Code,

Sure you didn't mean: a=strst[B]r[/B](b,"name"); instead of: a=strste(b,"name"); ?

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

sory it's a typo.

its strstr().thanks.

This question has already been answered. Start a new discussion instead.