-2

According to this question and this one, the operating system is freeing wathever you've malloced in your program as long as you close it, moreover in a simple program like mine it's not so important.

Sure in very short running simple programs, failing to free memory won't have a noticeable effect. jaredpar

According to man :

The malloc() function allocates size bytes [...]

So, I have used malloc to allocate an array of integer with 20 bytes of memory (sizeof int is 4 bytes on my system) :

array = malloc(5 * sizeof(int));

Therefore I should be able to write just 5 integers (5 * 4 bytes = 20 bytes = 5 integers). Now this program here should create a core dump, but this is not the case, for some reason :

#include <stdio.h>
#include <stdlib.h>

int main(void){

        int *array = NULL, i = 0;

        array = malloc(5 * sizeof(int));

        if(array == NULL)
        {
                printf("\nERROR : Out of memory.\n");
                return 1;
        }

        for(i = 0; i < 10; i++)
                scanf("%d", &array[i]);

return 0;

}

This program lets me input 10 integers without the creation of a coredump...Instead using free(array) at the end of the program will correctly result in the creation of a core dump. Isn't that strange? Shouldn't both programs create a core dump? Why doesn't using free() doesn't create a core dump? I know what a core dump is and how to avoid it, my question is purely curiosity as, for me, this is a strange behaviour.

Community
  • 1
  • 1
Claudio Cortese
  • 1,372
  • 2
  • 10
  • 21
  • 8
    Because *Undefined Behavior* is undefined. You can't expect a defined behavior from it. Also, use `if (array == NULL)` instead. – Iharob Al Asimi Jan 17 '16 at 12:13
  • 1
    Possible duplicate of [segfault with array](http://stackoverflow.com/questions/4678793/segfault-with-array) – Martin James Jan 17 '16 at 12:16
  • If the OS frees memory when the program is closed, why is it undefined? Should it give me a core dump as well? P.S = Fixed with NULL – Claudio Cortese Jan 17 '16 at 12:16
  • 3
    The behaviour is undefined by the C standard, which means anything is allowed to happen. One compiler/library may build the code with no problems, and another may simply give a crash. One possible result is behaving in a way that make it seem like nothing is wrong. Another possible result is behaving as if nothing is wrong, and then crashing at the next blue moon. – Peter Jan 17 '16 at 12:17
  • Then whoever says that using free() for simple program is useless is wrong, as using it will point out if you have done some error. – Claudio Cortese Jan 17 '16 at 12:20
  • 2
    It will not always point out anything. You may create undefined behavior and have `free` finish without apparent problems. – Ruslan Jan 17 '16 at 12:21
  • 5
    @Claudio No, you are very confused about how this works. There is no "*should* cause a core dump". There is only "*could* cause a core dump". If you have a memory error in your program, *anything* can happen. It *may* cause a core dump, or it *may* cause strange behavior, or it *may* not be detectable at all. All bets are off. Similarly with `free`. You cannot depend on `free` to catch your bugs for you. If you have a memory error, `free` *may* detect it, or it *may* cause a core dump, or it *may* do nothing special at all. – Tom Karzes Jan 17 '16 at 12:24
  • @MartinJames My question is more related to the use of free() that will lead to a core dump than the segfault itself – Claudio Cortese Jan 17 '16 at 12:24
  • 4
    Your call to `free` causes a core dump likely because you overwrite some of libc's data structures which control memory allocation, and which `free` relies on. You're just lucky that it caused immediate core dump, not just strange behavior. – Ruslan Jan 17 '16 at 12:31
  • 1
    Following @Ruslan comment, see this answer: http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work – terence hill Jan 17 '16 at 12:37
  • @Ruslan Thank you, I kind of understeand now. So free() doesn't necessarly give segfault, also if this is the case for me. – Claudio Cortese Jan 17 '16 at 12:39

1 Answers1

2

Shouldn't both programs create a core dump?

It could, but it doesn't have to.

Your program exercises undefined behavior, and the result of executing such program is undefined:

  • it could exit successfully,
  • it could abort,
  • it could crash with a segmentation fault,
  • it could melt your CPU,
  • it could launch nuclear missiles.

That's exactly what undefined behavior means: anything could happen.

Your expectation that a particular thing would happen is incorrect.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362