Reputation: 2730
I have below C program. It asks for number of coordinates from user. Then uses a malloc to allocate memory, stores the coordinates (integers) in the allocated memory, and then later frees the memory.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]) /* Arguments to main() not necessary but used to keep with convention.*/
{
int num_of_coordinates;
printf("How many co-ordinates: ");
scanf("%d", &num_of_coordinates);
int *coordinate_array = malloc(num_of_coordinates * 2);
int i, j;
/* Below for loop takes the x and y coordinate of all points. */
/* These coordinates are stored in coordinate_aaray[]. */
for (i=0; i < num_of_coordinates*2; i++)
{
j = (i / 2) + 1 ;
if (i % 2 != 0)
{
printf("Enter x coordinate of point %d: ",j);
scanf("%d",&coordinate_array[i]);
}
else
{
printf("Enter y-coordinate of point %d: ",j);
scanf("%d",&coordinate_array[i]);
}
}
for (i=0; i < num_of_coordinates*2; i++)
{
printf("%d ",coordinate_array[i]);
}
printf("\n");
/* Free the allocated memory. */
free (coordinate_array);
return 0;
}
When I run these program I get no issues till number_of_coordinates
is equal or less than 3.
-bash-4.1$ ./a.out
How many co-ordinates: 3
Enter y-coordinate of point 1: 1
Enter x coordinate of point 1: 2
Enter y-coordinate of point 2: 3
Enter x coordinate of point 2: 4
Enter y-coordinate of point 3: 5
Enter x coordinate of point 3: 6
1 2 3 4 5 6
-bash-4.1$
However when I give num_of_coordinates
a value of 4 or more I get a runtime error (most probably because of free(coordinate_array)
).
-bash-4.1$ ./a.out
How many co-ordinates: 4
Enter y-coordinate of point 1: 1
Enter x coordinate of point 1: 2
Enter y-coordinate of point 2: 3
Enter x coordinate of point 2: 4
Enter y-coordinate of point 3: 5
Enter x coordinate of point 3: 6
Enter y-coordinate of point 4: 7
Enter x coordinate of point 4: 8
1 2 3 4 5 6 7 8
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x000000000185b010 ***
Actually the runtime error message is a long one, so I just showed the first line of that error.
Why is this error happening when num_of_coordinates
greater or equal to 4 in this case ?
Thanks.
Upvotes: 1
Views: 933
Reputation: 2398
Make sure to allocate the exact number of bytes required. As another user mentioned above you should use:
malloc(num_of_coordinates * sizeof(int) * 2)
Also, just to double check, how about you log values to test your code. For instance, log the num_of_coordinates to make sure that you scanned the right value.
To debug, you can try something like this: http://dmalloc.com/
Upvotes: 0
Reputation: 16540
there are a number of problems with this line:
int *coordinate_array = malloc(num_of_coordinates * 2);
1) the amount of allocated memory is 1byte * num_of_coordinates * 2
That is not large enough to hold num_of_coordinates*2 integers
use:
int *coordinate_array = malloc(num_of_coordinates * 2 * sizeof int);
2) always check the returned value from malloc (and family)
to assure the operation was successful
int *coordinate_array = NULL;
if( NULL == (coordinate_array = malloc(num_of_coordinates * 2 * sizeof int) ) )
{ // then, malloc failed
perror( "malloc for coordinate array failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
Upvotes: 1
Reputation: 2730
I had made a silly mistake.
I had allocated insufficient amount of memory using below statement.
int *coordinate_array = malloc(num_of_coordinates * 2);
However as each number is an int
I had to use below statement.
int *coordinate_array = malloc(num_of_coordinates * 2 * sizeof(int));
After making this change, the program works with no runtime errors.
-bash-4.1$ ./a.out
How many co-ordinates: 4
Enter y-coordinate of point 1: 1
Enter x coordinate of point 1: 2
Enter y-coordinate of point 2: 3
Enter x coordinate of point 2: 4
Enter y-coordinate of point 3: 5
Enter x coordinate of point 3: 6
Enter y-coordinate of point 4: 7
Enter x coordinate of point 4: 8
1 2 3 4 5 6 7 8
-bash-4.1$
Upvotes: 1