0

I recently learned how to use fscanf_s, While tinkering with fscanf_s I made a code that reads a file and save it into an array. (The file contains 123 12 41 234 5 12 45 1 5 764 232)

First I made a piece of code to test if I made my While loop correctly

void arr_calc(FILE*f, ~~) {
    int i = 0, ~~;
    ...
    rewind(f);
    int arr[10];
    while (fscanf_s(f, "%d", &arr[i++]) != EOF);
}

When I checked the debug screen(autos), arr showed up like this

enter image description here

I could see what the arrwas holding with this, But when I changed the int arr[10] into malloc to make a variable sized array like this

void arr_calc(FILE*f, ~~) {
    int i = 0, * arr, ~~;
    ...
    rewind(f);
    arr = (int*)malloc((count) * sizeof(int));
    while (fscanf_s(f, "%d", &arr[i++]) != EOF);
}

I couldn't figure out what arrwas holding. - it only showed the first value

enter image description here

I presume that it has to do with the fact that I'm using a pointer for arr when I use malloc.

my question is: Is there a way that I can see the elements inside arr in the debug autos when using malloc(or realloc)?

If I said something wrong please correct me, Thank you

Maxjo
  • 67
  • 3
  • 4
    In the watch window add an expression like `arr,[count]`. You can see more in the [documentation](https://learn.microsoft.com/en-us/visualstudio/debugger/format-specifiers-in-cpp?view=vs-2019) – user786653 Feb 22 '21 at 16:19
  • duplicates: [View array in Visual Studio debugger?](https://stackoverflow.com/q/972511/995714), [Why does the debugger show only one element from my array pointer?](https://stackoverflow.com/q/9671591/995714) – phuclv Feb 22 '21 at 16:38
  • Does this answer your question? [How to display a dynamically allocated array in the Visual Studio debugger?](https://stackoverflow.com/questions/75180/how-to-display-a-dynamically-allocated-array-in-the-visual-studio-debugger) – phuclv Feb 22 '21 at 16:39

1 Answers1

1

That is normal and autos window cannot specify malloc type and it should work with the obvious definition size of your arr.

Instead, just as the community members said, use Watch Window, set arr,10 to get that you want. And then it will parse the array with the input size.

enter image description here

Mr Qian
  • 21,064
  • 1
  • 31
  • 41