调试有段错误的程序
debugging a program with segmentation fault
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, 我遇到了分段错误。
我最好的猜测是我在为动态二维数组分配内存时犯了某种错误。
由于我所有的测试运行都已成功编译并给出了正确的结果,我不知道为什么它不起作用。
#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;
}
当您需要在动态数组中放入新元素时,您应该使用 realloc()
来增加动态数组。在您的代码中,您一开始只进行了一次分配。
例如,在开头写这个(在你 scanf()
得到 total_number_of_shelves
和 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`
当你需要在里面放一个新元素时,你使用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));
...
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, 我遇到了分段错误。
我最好的猜测是我在为动态二维数组分配内存时犯了某种错误。
由于我所有的测试运行都已成功编译并给出了正确的结果,我不知道为什么它不起作用。
#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;
}
当您需要在动态数组中放入新元素时,您应该使用 realloc()
来增加动态数组。在您的代码中,您一开始只进行了一次分配。
例如,在开头写这个(在你 scanf()
得到 total_number_of_shelves
和 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`
当你需要在里面放一个新元素时,你使用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));
...