1

I have a simulated 2D array using pointers with malloc and I want to get the size of that array, since sizeof() returns me the size of the pointer. My code (ANSI C):

void main(){
    int s1=10;
    int s2=15;
    int ** rack=(int**)malloc(s1*sizeof(int**));
    for(int i=0; i<s1; i++) rack[i]=(int*)malloc(s2*sizeof(int*));
}

I want a function (may by system, may do it myself) this way:

size(rack)    //returns 10
size(rack[0]) //returns 15

Thanks in advance! (For more info I use GNU/Linux)

  • 2
    Note that [pointer to pointer array is not really 2D array](https://stackoverflow.com/q/42094465/694733). Also, ANSI C refers to old, outdated version of C; if you are on Linux it's quite possible that you could upgrade to C11. – user694733 Oct 10 '17 at 08:07
  • 3
    A good habit is not to do the `sizeof` in `malloc` with the the type but with the object that the pointer is supposed to point to `malloc(s1*sizeof *rack)`. Also in C you shouldn't cast the return of `malloc` and friends. – Jens Gustedt Oct 10 '17 at 08:09
  • 1
    You allocate the wrong amount of space in the mallocs – M.M Oct 10 '17 at 08:16
  • If you are given a pointer that was assigned/initialized with the value returned by `malloc` there is no (portable) way to find out how much memory was requested originally. These links may be of interest: https://stackoverflow.com/a/28854641/4386427 or http://man7.org/linux/man-pages/man3/malloc_usable_size.3.html or https://stackoverflow.com/questions/44581168/malloc-usable-size-returns-the-wrong-size – Support Ukraine Oct 10 '17 at 08:20
  • I'm not going to say you can't, but in C, you allocate the memory and you are responsible for tracking the allocation. There isn't a simple library function that will tell you how much memory you allocated for variable `X` earlier in your code. (and with multiple functions, and scopes, that information may not be available at runtime) – David C. Rankin Oct 10 '17 at 08:20
  • Possible duplicate of [How to get size of 2D array pointed by a double pointer?](https://stackoverflow.com/questions/12965359/how-to-get-size-of-2d-array-pointed-by-a-double-pointer) – Bo Persson Oct 10 '17 at 12:47
  • [see: 2-D Matrix with `malloc()`](https://stackoverflow.com/questions/46512314/graph-with-adjacency-matrix-in-c/46513988#46513988), and consider to `free()` allocated memory at last. – EsmaeelE Oct 10 '17 at 13:12

1 Answers1

0

First of all, the way you allocate memory for your 2D array is wrong. You need to change it to :

void main(){
    int s1 = 10;
    int s2 = 15;
    int** rack = malloc(s1*sizeof(int*));
    if (rack == NULL)
        return;
    for(int i=0; i<s1; i++) {
        rack[i]=malloc(s2*sizeof(int));
        if (rack[i] == NULL)
            return;
    }

    ... 

    free(rack);
    return;
}

Remember to always check malloc's result and also not to cast malloc's result.

In order to get your array's size, you can print s1*s2*sizeof(int) as you have allocated s1 pointers to int, each of which holds s2 ints.

Joster
  • 359
  • 1
  • 4
  • 19
Marievi
  • 4,951
  • 1
  • 16
  • 33
  • Please note that you are *not* freeing all the memory that you have allocated. – Bob__ Oct 10 '17 at 08:31
  • @Bob__ I did not because I answered only to the part of the allocation. I assumed that the OP freed his memory but showed us a minimal part. – Marievi Oct 10 '17 at 08:38
  • Due to differences in the range of `int` and `size_t`, `s1*s2*sizeof(int)` can overflow while `sizeof(int)*s1*s2*` does not. IAC, better to use `size_t` for array indexing and size than `int`. – chux - Reinstate Monica Oct 10 '17 at 13:55