-5
#include<stdio.h>
#include<stdlib.h>
int main() {
    int i, j, a[10], result = 0,p;
    int *m = malloc(sizeof(int)*8);
    for(i = 0; i < 10; i++){
        scanf("%d", &a[i]);
        result += a[i];
    }
    //printf("%d\n", result);
    //printf("\n");
    //for(i = 0; i < 8; i++) {
    for(j = 0; j < 9; j++) {
        scanf("%d", &m[j]);
        result = result - m[j];
        p = result / 2;
    }
    return p;
}

In this code I am getting a runtime error. Any help would be appreciated. Thanks!

Aditya R
  • 567
  • 4
  • 17
  • 2
    Can you provide more details on the error you are getting? – Jeremy Jul 15 '16 at 18:41
  • 1
    Because you are getting the error at runtime, it means that just looking at the code won't help. We need to see what the error is - and what happened at runtime to cause it. – durbnpoisn Jul 15 '16 at 18:42
  • 2
    Learn how to use a debugger. If you run in a debugger it will catch the crash "in action" and tell you where in your code it happens. Then you could use the debugger to examine values of variables to learn why the crash happens. At the very least please edit your question to tell us where in ***your*** code the crash happens and what the values of all involved variables are. – Some programmer dude Jul 15 '16 at 18:45
  • 1
    Please provide the `Input` and the `Error` for us to solve the question. – VatsalSura Jul 15 '16 at 18:45
  • 1
    My guess as to why you're getting the crash is because yo go out of bounds of the memory you allocate. Hints: Check that second loop again... – Some programmer dude Jul 15 '16 at 18:45
  • Lastly, your program can be simplified quite a bit. For example you don't really need the arrays, neither the compile-time array `a` nor the dynamically allocated `m`. That simplification will also fix your crash. – Some programmer dude Jul 15 '16 at 18:47
  • line 14: `j < 9` ?! – user3078414 Jul 15 '16 at 18:50

2 Answers2

3

Insufficient memory allocated.

int *m=malloc(sizeof(int)*8);  // 8 `int`
...
for(j=0;j<9;j++){
  scanf("%d",&m[j]);          // attempt to set the the 9th `int`, m[8]

Allocate sufficient memory.

#define JSIZE 9
int *m=malloc(sizeof *m * JSIZE);
if (m == NULL) Handle_OutOfMemory();
...
for(j=0;j<JSIZE;j++){
  if (scanf("%d",&m[j]) != 1) Handle_BadInput();
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Firstly, you can typecast the malloc allocated space to (int *) as by default malloc allocates space as (void *). SEcondly, you are running the loop j=0 to 8 i.e 9 times however u have allocated space for just 8.Hence you have a kindof array index out of bounds error

Oreo
  • 181
  • 1
  • 2
  • 12
  • compilers like dev c++ will give an error -"invalid conversion from (void*) to (int*)" – Oreo Jul 15 '16 at 19:00
  • Suggest reading [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) and [this answer](http://stackoverflow.com/a/605856/2410359) – chux - Reinstate Monica Jul 15 '16 at 19:11