-3

I am trying to define two dynamic 2-dimensional arrays using malloc and here is the code (I know that M = 118 and N = 186):

int **Cf;
Cf = (int **) malloc(M * sizeof(int *));
for (i = 0; i < N; i++)         
    Cf[i] = (int *) malloc(N * sizeof(int));

The array Cf allocates memory with no problem. However, my program crashes when it comes to Ct. I debugged the code and found out that Ct[i] = (int *) malloc(N * sizeof(int)) fails when N=164 which is kinda weird.

  • 5
    Perhaps better naming of your variables and symbolic constants are in order? – Some programmer dude Jun 20 '17 at 17:31
  • 1
    Why are you indexing to the wrong variable limit? Auto-UB – ThingyWotsit Jun 20 '17 at 17:31
  • 1
    And please read [this question and its answers about casting `malloc` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jun 20 '17 at 17:32
  • 1
    Rather than `N,M,i`, use something like `HEIGHT, WIDTH, h`. – chux - Reinstate Monica Jun 20 '17 at 17:33
  • Also note that you don't have any 2D arrays. – ThingyWotsit Jun 20 '17 at 17:34
  • 1
    @Afshin Ahmadi Ignore the reference "this question and its answers about casting malloc in C". There are advices of low-quakified programmers. – Vlad from Moscow Jun 20 '17 at 17:34
  • @xing I am a bit confused! Shouldn't I use N since I am trying to define a MxN matrix? – Afshin Ahmadi Jun 20 '17 at 17:35
  • 2
    @AfshinAhmadi You declared at first M pointers. So the loop should use the value of M. – Vlad from Moscow Jun 20 '17 at 17:35
  • @Someprogrammerdude actually I have a better naming of variables in my program. Just simplified it here. – Afshin Ahmadi Jun 20 '17 at 17:37
  • 1
    @AfshinAhmadi Use castings of malloc. It makes your code more clear and readable and allows to avoid many non-obvious errors. And moreover you will be able to use such a code in C and C++ programs. – Vlad from Moscow Jun 20 '17 at 17:37
  • @VladfromMoscow can you give me an example? – Afshin Ahmadi Jun 20 '17 at 17:46
  • @AfshinAhmadi Example of what? – Vlad from Moscow Jun 20 '17 at 17:51
  • @AfshinAhmadi To cast or not to cast, that is the [question](https://en.wikipedia.org/wiki/To_be,_or_not_to_be) best for another post like [this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). The best answer, IMO, is to follow your group's coding standards. – chux - Reinstate Monica Jun 20 '17 at 17:53
  • "Program stops when defining dynamic 2D array" - There is no 2D array in your code and noting which can be used as one or point to one! A pointer is not an array. (And that has nothing to do with dynamic allocation) – too honest for this site Jun 20 '17 at 18:18
  • @xing The code crashes when the array size is like 20,000 x 20,000. But it works fine when array size is like 118x118!!! – Afshin Ahmadi Jun 21 '17 at 01:20
  • 1
    If you have a new problem, ask a new question, and this time put together a complete example that demonstrates it, something others could compile. You should also consider how much memory a 20kx20k array uses, perhaps you're running out. Add some error checking and narrow down the problem. – Retired Ninja Jun 21 '17 at 01:22
  • @xing I checked the malloc and it failed. The funny thing is that the first array (Cf) allocates perfectly, but when it comes to the second array (Ct) malloc fails. They both have the same size (20kx20k). I also tried calloc and encountered same issue. These variables are defined in a function. What do you think could cause this problem? I have more than enough memory on my computer! – Afshin Ahmadi Jun 21 '17 at 15:12
  • @xing I checked the memory usage and I still have 9GB of free RAM. It is just weird. I also tried to allocate these two arrays in an empty project and it just worked fine! – Afshin Ahmadi Jun 21 '17 at 15:35
  • @xing Do you know a more efficient way of allocating dynamic 2d arrays? I may encounter 100kx100k matrices in the future. – Afshin Ahmadi Jun 21 '17 at 15:54
  • @xing Thank you, Xing. I will ask this question in a separate post. Stackoverflow has blocked me for 1 day because of negative points in this post! – Afshin Ahmadi Jun 21 '17 at 16:19

1 Answers1

1

Key issue: Wrong range used. Should be <M, not <N. @xing

Recommend using sizeof against the referenced object than the type. Easier to code, review and maintain.

Note that the casts are not required. @Some programmer dude

int **Cf = malloc(sizeof *CF * M);
assert(Cf);

//           M, not N
// for(i=0;i<N;i++){
for(size_t m=0; m < M; m++){ 

    printf("%zu\n", m);
    Cf[m] = malloc(sizeof *(Cf[m]) * N);
    assert(Cf[m]);
} 

Code also used m instead of i to help make clear that the m index goes with M.


To use calloc()

// int **Cf = malloc(sizeof *CF * M);
int **Cf = calloc(M, sizeof *CF);

    // Cf[m] = malloc(sizeof *(Cf[m]) * N);
    Cf[m] = calloc(N, sizeof *(Cf[m]));
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • If i use calloc, I won't be able to access array elements in the form of array[i][j], right? – Afshin Ahmadi Jun 20 '17 at 17:50
  • @AfshinAhmadi [That](https://stackoverflow.com/questions/44659534/program-stops-when-defining-dynamic-2d-array-in-c/44659704#comment76304971_44659704) is incorrect. Code can access via `Cf[m][n]` if `malloc()`, `calloc()` (or `realloc()` with prior allocaitons) was used like above. – chux - Reinstate Monica Jun 20 '17 at 17:55
  • The code crashes when the array size is like 20,000 x 20,000. But it works fine when array size is like 118x118!!! – Afshin Ahmadi Jun 21 '17 at 01:19