#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int max_b(int,int);
int max(int[],int);
int main(){
int num_tests;
scanf("%d",&num_tests);
int array_n[num_tests];
int array_b[num_tests];
int i,j;
for (i=0;i<num_tests;i++){
scanf("%d %d",&array_n[i],&array_b[i]);
}
for (j=0;j<num_tests;j++){
int A = 1;
int N = array_n[j];
int B = array_b[j];
int max_num_b;
max_num_b = max_b(N,B);
int array2[max_num_b];
int k;
for (k=0;k<max_num_b;k++){
int num_a,num_b;
num_a = N-(k+1)*B;
num_b = k+1;
array2[k] = num_a*num_b;
}
printf("%d\n",max(array2,max_num_b));
}
}
int max(int array[],int a){
int max_num = 0,i;
for (i=0;i<a;i++){
if (array[i] > max_num){
max_num = array[i];
}
}
return max_num;
}
int max_b(int n,int b){
return n/b;
}
my first input is num of test cases T (say 1),and second input is 1,000,000,000 1 . So then the code tries to form an array of 10^9 size and the program ends up showing segmentation fault. However the code runs well upto 1,000,000 1. how can I store upto 10^9 elements. If its not possible then how can I store so many numbers. Should I use malloc, if yes then how. any help would be appreciated.
You should NOT use variable length array declarations when the size is determined at runtime.
I would suggest you use
malloc()
instead. Replace the following linesWith
As other comments pointed out, 1,000,000,000 elements need roughly 4GB of memory (double that as you have two arrays). So, you may run out of memory allocating this many integers.
For a billion
int
s in two arrays, you will need nearly 8GiB of storage, assuming you have 32bitint
s.This is a really huge amount of memory and your best bet with standard C function is to request them via
malloc()
-- if your address space is large enough (oni386
, it would be too small, but onamd64
it should be fine) and if the system can provide that much memory, the following should work:Note I changed the
scanf()
to read anunsigned long
, just to be sure. It's still not bullet-proof, though.You don't need to store this data at all. Just process it on the fly.
As far as I can tell, the following code produces the same results as the code you posted, but uses no arrays whatsoever.
First of all, when you initialize an array the size must be constant. So use
#define NUM_TESTS 1000000000
instead. Second, since an integers size is 4 bytes 10^9 integers would need 4 000 000 000 B which equals 4 GB. And its pretty 'hard' to 4 GB on the stack. You should use files or, if all your values are smaller than, lets say 256, you can usechar
s instead. Its still 1 GB, but thats way 'easier' to get.