2

I would like to debug 2D array that is dynamicly allocated with malloc. All i want to know is if I can check with debugger what the array contains. I have made an simple program:

#include <stdio.h>
#include <stdlib.h>

   int main(void){
       int height = 5, width = 5;
//creating array
       int **arr = (int **)malloc(height * sizeof(int *));
       for (int i=0; i<height; i++)
           arr[i] = (int *)malloc(width * sizeof(int));
//filling array
       int counter = 0 ;
       for (int i = 0 ; i < height ; i++){
           for (int j = 0 ; j < width ; j++){
               arr[i][j] = counter;
               counter ++;
           }
       }
//breakpoint
//printing array to console
       for (int i = 0 ; i < height ; i++) {
           for (int j = 0; j < width; j++) {
               printf(" %d |", arr[i][j]);
           }
           printf("\n");
       }

   return 0;
}

When I want to breakpoint after filling the array with data I cant see the int values. All i can check is this : https://i.stack.imgur.com/2ZD1N.jpg

Jakub Zilinek
  • 219
  • 1
  • 2
  • 7
  • it may not be necessary to cast `malloc()`'s returned `void *` to , say, an `int *` – A P Jo Sep 25 '20 at 15:40
  • see https://stackoverflow.com/q/40327089/10622916 – Bodo Sep 25 '20 at 15:49
  • 1
    Does this answer your question? [In Clion's debugger, how do I show the entire contents of an int array](https://stackoverflow.com/questions/40327089/in-clions-debugger-how-do-i-show-the-entire-contents-of-an-int-array) – Bodo Sep 25 '20 at 15:49
  • @Bodo kinda. When I have 2d dynamicly allocated array Do i need to cast it on every arr * manually again? Also can i type the size variable instead of number the variable is ? This is after I cast int[5] * on arr : https://imgur.com/a/mHKAtqu And now if i want to see each line i need to cast int[5] on each line again ? Like this : https://imgur.com/a/Et9zVVd The question is if i can simple automatizate this. I wasnt able to figure it out. Thanks for help btw. – Jakub Zilinek Sep 25 '20 at 16:39
  • @JakubZilinek Please don't write additional questions in comments and avoid posting text as a screenshot. This looks like a clarification of your question. Please [edit] your question and add all information there. (Add a note for major edits.) Show the code as text in the question and add comments to mark where you want to see what data. I don't use this IDE, so I cannot give any detailed advice. Instead of specifying a watch expression with a cast you could also try to add a pointer variable for debugging purposes that is casted as necessary and remove it later. – Bodo Sep 29 '20 at 14:51

0 Answers0