0

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]);
        }
    }
}
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
shacke
  • 592
  • 4
  • 11

1 Answers1

4

The lines

const char **date;

date = (const char **) malloc(cases * sizeof(char));

are wrong. The element is const char*, so you have to allocate for that. In other words, you have to allocate size of a pointer, not size of a char, for each elements.

Moreover, the objects pointed at by the elements of the array date will be modified via scanf() later, so they should be char*, not const char*.

it should be

char **date;

date = malloc(cases * sizeof(char*));

or

char **date;

date = malloc(cases * sizeof(*date));

Also the line

date[i] = (char *) malloc(10 * sizeof(char));//String max length is 10.

is wrong. You have to allocate one more element for the terminating null-character to allow 10-character strings to be stored there.

It should be:

date[i] = malloc(11 * sizeof(char));//String max length is 10.

Also note that:

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks, i forgot it, but the problem still the same, my problem is in the first array, when i say for example, 2345 cases, then i got the error of denied access – shacke Apr 10 '21 at 00:03
  • Ok nvm, the problem is related with how i allocate memory for the matrix, im trying to get it work, thanks in advice buddy – shacke Apr 10 '21 at 00:05
  • It seems you are not freeing what are allocated, causing memory leak. – MikeCAT Apr 10 '21 at 00:07
  • I applied your advices and now it works smoothly, i was freeing but the script of the answer was a part of all the script, thank you so much buddy, and thanks for the malloc casting advice <3 – shacke Apr 10 '21 at 00:10