0

The following code worked fine for me (code blocks 10.05) and showed no compile-time/runtime errors for various test cases. But showed runtime error as I submitted it online on a programming website.

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

/*

 Here comes newPos()

*/
int main()

{
int t,i,n,k,j;
scanf("%d",&t);
int* a;

for(i=0;i<t;i++)
{
    scanf("%d",&n);
    free(a);

    a=(int*) malloc(n);

    for(j=0;j<n;j++)
        scanf("%d",&a[j]);
    scanf("%d",&k);

    printf("%d\n",newPos(a,n,k));

}


return 0;
}

And then I changed it into a .cpp file after making a few changes. i.e., instead of free(a) I used the statement, delete a; and instead of a=(int*) malloc(n), I used the statement, a=new int[n]; Then it executed successfully both on my compiler and online.

4 Answers4

6

First error:

You are not allocating enough memory to store n integer values. So you should change:

a=(int*) malloc(n);

to:

a=malloc(n * sizeof(int)); 

I have also removed the cast since it's useless and could hide a forgotten include.

Second error:

You must not free a before allocating memory. Free the memory only at the end of your loop.

C/C++ mix:

In the comments of this answer, people are talking about the need or not to cast, in particular in C++. In C, you should not cast.

If you are willing to do C++ code, you should use new and delete instead of malloc and free. Honestly, I don't know if a cast is needed in C++ when using malloc, because in C++, I always use new. But please, don't write C code with a C++ compiler. Choose between C and C++ depending on your needs.

Community
  • 1
  • 1
Maxime Chéramy
  • 16,409
  • 7
  • 51
  • 73
3

You are freeing before allocating:

free(a); // This can lead to Undefined Behavior because a is containing some junk value
a=(int*) malloc(n);

Also, no specific need to cast return type of malloc and check your malloc argument you are not specifying size in bytes correctly. But in C++ the case is required (Since you tagged both C and C++).

Do I cast the result of malloc?

 a=(int*) malloc(n*sizeof(int));
Community
  • 1
  • 1
Sadique
  • 22,181
  • 7
  • 62
  • 90
2

Aside from the mentioned allocation size problem, you can't free(a) unless you have either already allocated something, or have initialized a to have the value NULL.

Mats Petersson
  • 123,518
  • 13
  • 127
  • 216
0

This is because your argument to malloc() is wrong. The function has no idea what "unit" you're going to be using, so the unit for the argument is always "bytes". With C++'s new[] operator, that's operating a higher level in the language so it can take the type's size into account.

Thus, change your allocation to:

a = malloc(n * sizeof *a);

This removes the pointless and annoying cast, and also adds the missing sizeof to scale the argument by the number of bytes in each pointed-to object. This is a good form of malloc() usage to remember.

Also don't free() random pointers.

Community
  • 1
  • 1
unwind
  • 378,987
  • 63
  • 458
  • 590