-6

The code is the following, I have doubts in the line that is inside the for, I do not know how to bring it to the C language, only that line, the rest I know, it is the NRU paging algorithm.

int numberofframes;
int references;
char *pages;
char *faults;
char **frametable;
        
cout << "\nEnter the number of references of pages you want to have: ";
cin >> references;
pages = new char[references];
faults = new char[references];
        
//The matrix for the frames is created
frametable= new char*[numberofframes];
for(i = 0; i < amountofframes; i++){
   frametable[i] = new char[references];//What would this line in C look like?
}
  • [Reading a text file into a 2D Dynamic Array using double pointers](https://stackoverflow.com/a/68052062/3422102) from earlier tonight may help. – David C. Rankin Jun 20 '21 at 03:28

1 Answers1

1

Memory allocation in C is usually done with malloc.

frametable[i] = malloc(references * sizeof *frametable);

Alternatively, you could use calloc:

frametable[i] = calloc(references, sizeof *frametable);

As you can see, the important part is determining the size of the memory allocation, and ideally you would use sizeof of the variable you want to put it into. In this case, frametable is of type char ** or a "pointer to pointers" an "array of pointers". The sizeof *frametable is used to determine the size of such a pointer.

Cheatah
  • 1,825
  • 2
  • 13
  • 21
  • 2
    Should be `frametable[i] = malloc(references * sizeof *frametable[i]);` or `frametable[i] = malloc(references * sizeof **frametable);` (you always use the size of the dereferenced pointer, in this case the pointer is `frametable[i]`) Given type `char` simply `frametable[i] = malloc(references);` would do. – David C. Rankin Jun 20 '21 at 03:42
  • If that is unclear, the current allocation you show over-allocates by a factor of `sizeof(a_pointer)`. Since `frametable[i]` is type `char*` you want to allocate characters, not pointers. – David C. Rankin Jun 20 '21 at 03:52