-1

I have a ram of 8GB and I have Hard disk of 80GB. I have three programs as shown in below.

1)

main()
{

  while(1)
     x();
}

x()
{
  printf("hello\n");
}

Will there be a memory leak here in the above program? I suppose not. In case if it does not happen then what will happen? When will this stop?

2)

main()
{
   int *x;
   while(1)
      x=malloc(100*size(int));//there is a memory leak here
}

It's obvious that this program will crash after some time but I want to know WHEN, looking at the resources I mentioned above.

3)

main()
{
   x();
}
x()
{
   x();
}

Is this above program in infinite recursion? What will happen in the end? When will this program crashes/dies considering the resources I have?

user5756014
  • 301
  • 4
  • 16
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 1
    I have a question for you. What happens when you try these programs? How long does it take? – Greg Hewgill Mar 23 '11 at 10:03
  • These are three different (related questions), and some of them unanswerable (in particular 2 depends on your configuration, OS bitness, and even compiler options). And even more, there is actually little to no value in the answers... – David Rodríguez - dribeas Mar 23 '11 at 10:04
  • 2
    The code is bordering on "hard to read" without proper indentation. – Vatine Mar 23 '11 at 10:04
  • 1
    I'd suggest that you post your own theory as to the answers, then we have some basis for helping you. At present it's not clear to me what you don't understand. – djna Mar 23 '11 at 10:06
  • Memory leaks happen when someone fails to search for a memory leak definition http://stackoverflow.com/questions/312069/the-best-memory-leak-definition – sharptooth Mar 23 '11 at 10:08
  • Why do you think that the second one will crash? – Šimon Tóth Mar 23 '11 at 10:09
  • actually the question was asked in the interview!regarding the second program..considering that i have a ram of 8GB! when will it crash? – Vijay Mar 23 '11 at 10:12
  • @zombie There is no reason for it to crash. – Šimon Tóth Mar 23 '11 at 10:13
  • 1
    why is this closed? why is this not a real question?i just wanted to know when the program will crash and it might practically take a lot of time which sometimes in never possible to find when it will crash? – Vijay Mar 23 '11 at 10:16
  • @zombie It was most likely closed for the low quality (mentioned in the comments). It will probably be reopened. Please try to provide a better formulated question (and formated code) next time. – Šimon Tóth Mar 23 '11 at 10:20

3 Answers3

3

A memory leak occurs when you request memory with malloc() and don't free() the memory.

Bad example

#include <stdlib.h>

int main(int argc, char *argv[]) {
    int *nums = malloc(10 * sizeof(int)); // create array with memory for 10 nums
    nums[0] = 2;
    nums[4] = 8;

    return 0; // main() ended but nums wasn't free'd. Memory leak/
}

Good example

#include <stdlib.h>

int main(int argc, char *argv[]) {
    int *nums = malloc(10 * sizeof(int)); // create array with memory for 10 nums
    nums[0] = 2;
    nums[4] = 8;

    free(nums); // free memory allocated for nums

    return 0;
}

Now about your programs.

1 - reformatted

This program will never end and forever call x (and thus print Hello World! till the end of times)

#include <stdio.h>

void x(void) {
    printf("Hello World!\n");
}

int main(int argc, char *argv[]) {
    while (1) x();
    return 0;
}

2 - reformatted

This program allocates memory for 100 integers every loop but never free's this memory. Huge memory leak that keeps going until the OS is out of memory.

#include <stdlib.h>

int main(int argc, char *argv[]) {
    int *x;
    while (1) {
        x = malloc(100 * size(int)); // allocate memory for an array of 100 integers
    }

    return 0;
}

3 - reformatted

This program keeps recursing until the stack is exhausted (stack trace) and the program will crash (stack overflow).

void x(void) {
    x();
}

int main(int argc, char *argv[]) {
    x();

    return 0;
}
orlp
  • 112,504
  • 36
  • 218
  • 315
  • Actually your bad example isn't that bad. When the program ends those bytes will be freed regardless of whether or not the program explicitly frees them. Memory leaks are only an issue while the program continues to run. When it terminates, the OS reclaims all the memory that was allocated to it. – aroth Mar 23 '11 at 10:17
  • @aroth: Depends on the OS and is not guaranteed by the C standard. – orlp Mar 23 '11 at 10:18
  • 1
    It shouldn't matter what the C standard says (and the C standard shouldn't be making guarantees about anything the OS does when a program terminates anyways). The OS deals in executable code, and has no way of knowing that program X was written in C. If the OS leaves anything resident in memory after a program terminates then that is a bug in the OS, and has nothing to do with anything the program did or did not do. – aroth Mar 23 '11 at 10:22
  • "The OS deals in executable code, and has no way of knowing that program X was written in C." The same goes for C: The programmer deals in C code, and has no way of knowing whether OS Y (X was too confusing) cleans up __his__ mess. – orlp Mar 23 '11 at 10:28
  • If this program is compiled as 32 bit program it will most likely run out of address space long before the OS runs out of memory. – CodesInChaos Mar 23 '11 at 10:29
3

There is no memory leak in the first program, it will run forever (or until it's killed from outside), happily printing that string.

There is a memory leak in the second but it won't crash. You'll just start getting NULL back from the malloc. Otherwise, same effect as number 1 above.

Number three will eventually overflow the stack, unless you have some particularly good tail end recursion optimisation in your compiler, in which case it too will run forever.

By the way, that code is particularly bad. It's been a long time since default return types and main functions without the two canonical argument lists were acceptable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • this point that you mentioned is interesting!"There is a memory leak in the second but it won't crash".is it true?will the second program never crash? – Vijay Mar 23 '11 at 10:14
  • +1 for the only fully correct answer – Šimon Tóth Mar 23 '11 at 10:15
  • @zombie, that's correct, it won't crash. All that will happen is that you'll exhaust the heap and start getting back NULL. Now, if you were to try and _use_ that memory, that would be another matter. – paxdiablo Mar 23 '11 at 11:35
2
  1. No memory leak. The program will never stop.
  2. Here is memory leak. Beacuse during program work you are allocating memory but after next step you don't have any pointer to this memory to delete. When the memory is finished this will throw "bad alloc" error.
  3. Here you don't have memory leak, but program will crash when the memory of stack is finished.
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111