I am trying to solve the 1D Arrays in C problem on Hacker Rank: here
We have to print the sum of the integers in the array.
My code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() 
{
    int n;
    int sum = 0;
    int *a = malloc(n * sizeof(int));
    scanf("%d", &n);
    getchar();
    for (int i = 0; i < n; i++)
    {
       scanf("%d ", &a[i]);
       sum += a[i];
    }
    printf("%d", sum);
    free(a);   
    return 0;
}
But the compiler gives error for some select test cases. The compiler message (error):
Compiler Message:
Abort Called
Error (stderr):
1.corrupted size vs. prev_size
2.Reading symbols from Solution...done.
3.[New LWP 129788]
4.[Thread debugging using libthread_db enabled]
5.Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
6.Core was generated by `./Solution'.
7.Program terminated with signal SIGABRT, Aborted.
8.#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
Some observations:
- This program is running properly in VS Code.
- Custom inputs (i.e. Custom test cases) given by me compiles successfully (using the "Test against custom input" feature of Hacker Rank).
- But it is only some select test cases which are giving this error.
Kindly point out the possible error in my code which is causing this problem.
 
     
    