【发布时间】:2021-10-31 05:00:25
【问题描述】:
我尝试了这种方法,但是当我cout <<vararr[i][j]<<endl; 时出现错误
我要解决的问题-hackerrank
我的代码-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int* variablesizedarr(int size){
int* arr= new int [size];
for(int i=1;i<size;i++){
cin >>arr[i];
}
/*I believe the problem is when I return the arr array*/
return arr;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q,size;
cin >>n >>q;
int** vararr= new int* [n];
for(int i=0;i<n;i++){
cin >>size;
vararr[i]=variablesizedarr(size);
}
/*now printing the values to the console*/
for(int k=0;k<q;k++){
int i,j;
cin >>i>>j;
cout <<vararr[i][j]<<endl; /* difinetly an error in this line because that's what the compiler say*/
}
// deallocate the array
for(int i=0;i<n;i++){
delete [] vararr[i];
}
delete [] vararr;
return 0;
}
edit1:我遇到的错误-
Compiler Message
Segmentation Fault
Error (stderr)
Reading symbols from Solution...done.
[New LWP 2655054]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00000000004011f2 in main () at Solution.cpp:32
32 cout <<vararr[i][j]<<endl;
To enable execution of this file add
add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual. E.g., run from the shell:
info "(gdb)Auto-loading safe path"
我知道我可以像其他人一样做到这一点,但我不会那样学习。任何帮助表示赞赏。
【问题讨论】:
-
请在问题中包含错误。
-
在
variablesizedarr你为什么不写信给arr[0]? -
另外,现在很难重现这一点,因为我们不知道您提供的是什么输入。你能用硬编码值替换所有
cin >> n >> q;类型的东西,并且仍然遇到你描述的问题吗?如果不能,你至少能告诉我们你正在尝试什么输入,你期望什么输出,以及你得到什么输出? “我遇到错误”非常模糊。 -
我已经对其进行了编辑,还提到了我遇到的错误。
-
@NathanPierson 哎呀。我怀疑这会解决错误。让我试试