-1

I need to write a program that will allocate the memory for multiplication table. The problem is that single call of malloc, calloc and realloc is limited to 80 bytes and I don't know how to allocate memory stage by stage. If anyone could help me I would be grateful.

Here is what I have tried already. It works if I allocate the memory for 400 bytes in one call.

int main()
{
    int row = 10;
    int col = 10;

    int w=0;
    int k=0;

    int *tab = (int *)malloc(row*col*sizeof(int));

    if(tab == NULL)
    {
        printf("Failed to allocate memory");
        return 8;
    }

    int i=0;

    for (w=0; w<row; w++)
    {
        for(k=0; k<col; k++)
        {
            *(tab+w*col+k) = ++i;
        }
     }       

    for (w=0; w<row; w++){
            for(k=0; k<col; k++){
                printf("%3d ", *(tab+w*col+k) );
         }
         printf("\n");
        }   

    free(tab);

return 0;
}

KAER
  • 5
  • 6
  • 3
    Why do you say that a "single call of malloc, calloc and realloc is limited to 80 bytes"? Is it really a limitation of the assignment/exercise that you're not allowed that? In that case there's plenty of example how to make "dynamic multi-dimensional arrays" using pointer to pointers (e.g. `int **tab` in your case). – Some programmer dude Jul 02 '19 at 11:59
  • 1
    On common systems there should not be a small limit (like 80 0r 400 bytes) for `malloc` etc. except if you are out of memory. What exactly happens when you try to allocate more memory? I suggest to replace `printf("Failed to allocate memory");` with `perror("malloc failed");` or similar to show the corresponding error information. Please show the values that lead to an error. On what system do you have the problem? Please [edit] your question to add the missing information. – Bodo Jul 02 '19 at 12:02
  • 1
    If you allocate 80 bytes, then you will not be able to fit 100 4-byte elements. You need to describe your problem better. Please read [ask] – klutt Jul 02 '19 at 12:19
  • Yea, I know it is confusing. I will try to explain it better. You can allocate up to 80 bytes with malloc, calloc or realloc in a single call, but the overall limit is 1000 bytes. You need to allocate 480 bytes with several malloc, calloc or realloc calls. – KAER Jul 02 '19 at 12:29
  • Then use realloc 5 times? – klutt Jul 02 '19 at 12:32
  • Yes that is a good call I thought of it aswell, but I have no idea how to implement that. Don't get me wrong, but I'm a newbie in C. If you could show me how to cast realloc to allocate +80 bytes in every call, I would be grateful. – KAER Jul 02 '19 at 12:36
  • Note that real-world systems with severely restricted RAM won't be using `malloc` in the first place - it would be banned entirely. – Lundin Jul 02 '19 at 13:08
  • @ROKA Have you read the documentation for realloc? I don't see an attempt of that in your code. – klutt Jul 02 '19 at 13:51

1 Answers1

0

Nested mallocs() are what you'll need to use. But, be careful, because you'll also need to use several frees()!


// 80 bytes
int** table = malloc(row * sizeof(int*));

for (int i = 0; i < row; i++) {
    // 80 bytes
    table[i] = malloc(col * sizeof(int));
}

// The 6th element in the first row is set to 5.
table[0][5] = 5;
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
  • The sad thing is that it would actually be faster to call realloc on the same memory segment 10 times in a row, than to use this code. Simply _don't_ invent senseless, artificial allocation schemes. – Lundin Jul 02 '19 at 13:11
  • @Lundin This is a pretty standard implementation of a 2D array. It's not exactly senseless and artificial. – Michael Bianconi Jul 02 '19 at 16:14
  • The senseless scheme was invented by the OP. As for this code, it is not even a 2D array. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Jul 03 '19 at 06:23