-7

What happen if I end the execution by passing return 0; after using a malloc and without freeing the part of memory allocated?

int * var;
var = (int *)malloc(sizeof(int)) ;
free(var) ;
return 0;
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
Facorazza
  • 317
  • 1
  • 15
  • 2
    if you allocate memory on the heap and don't free it you have a memory leak in your program – UnholySheep Jan 09 '16 at 15:26
  • 2
    If you mean ending the program without cleaning everything, in the majority of cases that wouldn't be a big problem. Don't rely on this though. It's still a memory leak. – Mr Lister Jan 09 '16 at 15:26
  • 1
    What's a memory leak? – Facorazza Jan 09 '16 at 15:28
  • @FedericoCorazza A memory leak is when you allocate memory but you don't release it after you're done. – Mr Lister Jan 09 '16 at 15:29
  • 1
    I'm a bit surprised about the many downvotes. In my opinion, it is a legitimate question, especially against the apparent backgound that the OP is unfamiliar with memory leaks. – Codor Jan 09 '16 at 15:33
  • 1
    Perhaps they suppose that everybody is born knowing everything. – Facorazza Jan 09 '16 at 15:35
  • 1
    No, they assume anyone asking questions to have research skills you obviously lack. By asking such a question with bazillions of duplicates on the Internet you waste everyone's (including yourself's) time. Such query Google with "what is free? c" or "memory leak." – cadaniluk Jan 09 '16 at 16:36

3 Answers3

2

The program contains a memory leak, as explained here. To answer the question specifically, the effect of the memory leak depends on the environment; in the best case, nothing happens, in the worst case, the machine might crash sooner or later. The existence of the memory leak is to be regarded as a bug in any case.

Codor
  • 17,447
  • 9
  • 29
  • 56
1

It is implementation specific.

On most operating systems (notably desktop or server OSes, e.g. Linux, MacOSX, Windows...) once a process is terminated, every used resources is released, and that includes its virtual address space. Hence even unfreed heap memory is released.

In particular, if you code a quickly running program (e.g. you know that it always will run in less than few seconds), it might be easier to accept some memory leak (but if you do, please comment that in your code and/or documentation). And many real life programs are doing that (in particular the GCC compiler, and probably several Unix shells).

On the contrary, if you are coding a server (e.g. a database or compute server), you should avoid any memory leaks which would make the server's process RSS grow indefinitely (till some crash). You usually should take great care that every allocated heap memory to deal with one request gets free-d after replying to that request.

On some embedded operating systems, if you don't release all the resources explicitly (free all heap allocated memory, fclose all opened streams) and properly, you have a resource leak.

See also this related question. On many OSes (including Linux) you can use valgrind to hunt memory leak bugs. With a recent gcc you might use debugging options like -g -fsanitize=address

Read also (at least for the concepts and the terminology) something about garbage collection and about fragmentation. If programming in C, you might consider using Boehm's garbage collector.

There is however a very good practical reason to systematically free all previously malloc-ed memory: it is a good programming discipline, and it helps a lot using tools like valgrind which helps debugging genuine memory bugs. Also it makes your code more clean, and you could reuse it in some different contexts (e.g. as part of some library, usable in long-running processes).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

If you don't free the memory, your application will have a memory leak. So if you don't free the memory and leave the application on for a couple of days, it will start to slow down, and eventually crash.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75