array

closed account (jGAShbRD)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

int main(){
int row,question;
  scanf("%i%i",&row,&question);
  int a[row][100000];
  for(int i=0;i<row;i++){
    int x;
    scanf("%i",&x);
    for(int j=0;j<x;j++){
      scanf("%i",&a[i][j]);
    }
  }
  int q[question][2];
  for(int i=0;i<question;i++){
    for(int j=0;j<2;j++)
      scanf("%i",&q[i][j]);
  }
  for(int i=0;i<question;i++)
        printf("%i\n",a[q[i][0]][q[i][1]]);  
    return 0;
}


im getting segemented fault error here when i enter 100000 for row and question. would i have to create this array in heap or am i doing something else wrong?
so the code is gets 2 inputs first tells how many rows are created and second is how many questions are asked
then each row gets a number telling that row to contain that much element.
then a question is asked where it gets the row and index of that row's element.
Last edited on
It's not surprising that a 40 gigabyte array won't fit on the stack, or in memory at all, really.

What the fuck are you filling it with?

At any rate, since you're obviously writing a C program (scanf, printf, VLA), you should use C headers (i.e., <stdio.h>) and ensure that you compile it as C.
Last edited on
closed account (jGAShbRD)
im writing a c++ program. i heard many companies still used printf and scanf instead of std::cin and std::cout so im getting used to writing both.
and im filling the array with random ints.
The stack's memory space is in the magnitude of a few MB. You're trying to allocate 40 GB, as dutch said. Won't work.

In addition, array sizes must be known at compile-time. Your lines 6 and 14 are Variable-Length Arrays (VLA), and are not legal C++. If you're just practicing, just make a sanely-sized array, like 100x100, and don't let the user enter a number above that.

Also,it'sokaytousethespacebar.
Last edited on
Topic archived. No new replies allowed.