I am trying to implement a simple sieve and to my help I found the following code:
int main(int argc, char *argv[])
{
    int *array, n=10;
    array =(int *)malloc(sizeof(int));
    sieve(array,n);
    return 0;
}
void sieve(int *a, int n)
{
    int i=0, j=0;
    for(i=2; i<=n; i++) {
        a[i] = 1;
    }
...
For some reason this works, but I think it should not! The space that is allocated for the variable array is only enough to support one integer, but a[i] for i = 2...10 are called in the function sieve. Shouldn't this cause problems?
I tried to change the implementation to
int array[10], n = 10; 
which caused "Abort trap: 6" on runtime. However, this I understand since array[10] will be outside of the space allocated. But shouldn't the same be true also for the code where malloc i used?
Truly confusing.
 
    