This challenge from hackerRank that I was working on successfully compiles with the test run and gives out correct answers with all sorts of input. But when I submit and the code is run with enormous amount of digits like so, I get a segmentation fault.
My best guess is that I am making some sort of mistake while allocating memory to the dynamic 2D array.
Since all my test runs have successfully compiled and given out a correct result I have no idea why it would not work.
#include <stdio.h>
#include <stdlib.h>
/*
* This stores the total number of books in each shelf.
*/
int* total_number_of_books;
/*
* This stores the total number of pages in each book of each shelf.
* The rows represent the shelves and the columns represent the books.
*/
int** total_number_of_pages;
int main()
{
int total_number_of_shelves;
scanf("%d", &total_number_of_shelves);
int total_number_of_queries;
scanf("%d", &total_number_of_queries);
//_______________________________________________________________________//
// All malloc() declarations writen by me are here : //
// My guess is, one of this statement is causing the bug. //
//_______________________________________________________________________//
total_number_of_books=(int *)malloc(sizeof(int)*(total_number_of_shelves*1100));
total_number_of_pages=(int **)malloc(sizeof(int*)*total_number_of_shelves);
for (int tnos=0; tnos<total_number_of_shelves; tnos )
{
total_number_of_pages[tnos]=(int *)malloc(sizeof(int)*1100);
}
while (total_number_of_queries--) {
int type_of_query;
scanf("%d", &type_of_query);
if (type_of_query == 1) {
//___My code starts here.___//
int x, y, index;
scanf("%d %d", &x, &y);
index=0;
while( total_number_of_pages[x][index]!=0 )
{
index ;
}
total_number_of_pages[x][index] = y;
total_number_of_books[x] ;
//_______________________________________________________________//
//All code below is a template which was provided by the website.//
} else if (type_of_query == 2) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", *(*(total_number_of_pages x) y));
} else {
int x;
scanf("%d", &x);
printf("%d\n", *(total_number_of_books x));
}
}
if (total_number_of_books) {
free(total_number_of_books);
}
for (int i = 0; i < total_number_of_shelves; i ) {
if (*(total_number_of_pages i)) {
free(*(total_number_of_pages i));
}
}
if (total_number_of_pages) {
free(total_number_of_pages);
}
return 0;
}
CodePudding user response:
You should use realloc()
to grow the dynamic arrays when you need to put a new element in them. In your code you're doing just one allocation at the start.
For instance, write this at the start (after you scanf()
to get total_number_of_shelves
and total_number_of_queries
):
total_number_of_books = malloc(total_number_of_shelves * sizeof(int));
total_number_of_pages = malloc(total_number_of_shelves * sizeof(int*));
// TODO: you should initialize every element of the first array to `0`,
// and every element of the second array to `NULL`
And when you need to put a new element inside, you use realloc()
:
total_number_of_books[x] = 1;
total_number_of_pages[x] = realloc(total_number_of_pages[x], total_number_of_books[x] * sizeof(int));
...