2021年4月9日星期五

How should I make a dynamic array of 1

I am trying to solve a problem on a competitive programming page, and it had occurred to me that a good way to solve it would be by making a dynamic array, right now I don't care about the problem and what I want to know is how something like this could be implemented.

The idea is first they give us the number of cases (the size of the array) and then we make certain tours of that array, I would also have to apply it to a matrix of characters (an array of strings), this code that I have here works well when the input of cases is 1 <n <110, but of course, when that range is passed (it should be up to 300000 ) gives me a memory access error Process finished with exit code -1073741819 (0xC0000005), which is normal considering what I'm trying to do, I don't even know if it can, thank you very much in advance!

This is my code:

int main() {      int cases, i, j, max = 0;      while ((scanf("%d", &cases)) != EOF) {            int *victims;          victims = (int *) malloc(cases * sizeof(int));            const char **date;                    date = (const char **) malloc(cases * sizeof(char));          for (i = 0; i < cases; i++) {              date[i] = (char *) malloc(10 * sizeof(char));//String max length is 10.          }          for (i = 0; i < cases; i++) {              scanf("%s", date[i]);              scanf("%d", &victims[i]);          }      }  }  
https://stackoverflow.com/questions/67029595/how-should-i-make-a-dynamic-array-of-1n300000 April 10, 2021 at 07:57AM

没有评论:

发表评论