0

I have recently set up my vs code to be able to debug in c++
But one problem I have is that when I create a 2d array and try to see it in the variables window, I can only see the number of elements but not the elements themselves, ??is all you can see there
See it here: enter image description here

How can I see the inner arrays instead of question marks?

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    You have non-standard code. The size of the array must be a compile-time constant. The poor IDE may not know how to display the array because it can't figure out the correct size. See if `int Arr[4][4];` renders correctly. If it does we can proceed from there. – user4581301 Jan 22 '22 at 20:34
  • 1
    Handy reading: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). The image shows the problem with the IDE, groovy, but the code should be added as text. – user4581301 Jan 22 '22 at 20:35
  • 1
    By the way, good on you for trying to figure out the debugger. Too many people don't. – user4581301 Jan 22 '22 at 20:37
  • Does not answer how to show the contents of c style arrays, instead, you should consider using std containers. `std:array,4> Arr` in your case. – t.niese Jan 22 '22 at 20:52
  • @user4581301 it works when the size parameters are numbers but I have to get n and m as an input... –  Jan 22 '22 at 21:07
  • 2
    That'd be `std::vector> Arr(n, std::vector(m));` . The rest of your code stays the same, and as a bonus the debugger *should* allow you to drill into the subvectors. make sure to include `` – WhozCraig Jan 22 '22 at 21:12
  • @WhozCraig Great, I'll try it, but does it have the same time efficiency? Because that's very important for the code I'm currently working on, also do you think you could do the same thing with arrays? I'm not sure because I don't fully understand the second part. –  Jan 22 '22 at 21:22
  • The one-shot time it is done is marginal compared to the overall run-time of your program. Obviously it won't be as-fast as what is effectively `sub esp, n*m*sizeeof(int)`, a single asm instruction. But it *is* language compliant, and *will* work on any toolchain supporting every conceivable C++ standard. – WhozCraig Jan 22 '22 at 21:42
  • And when you do need that bit of extra speed, you do [something like this](https://stackoverflow.com/a/2076668/4581301). It puts all of the data in one `vector`, so it's 1D, and then wraps the 1D structure with a class that does the math to make it look 2D to outsiders. – user4581301 Jan 23 '22 at 03:09
  • Or what I can do is debug using the vector technique and when I run it put it back to an array –  Jan 23 '22 at 11:07
  • Only if A) you always use a compiler that allows Variable Length Arrays and B) you restrict the values of `m` and `n` so they are not likely to cause overflows with any reasonable stack size. – user4581301 Jan 24 '22 at 02:05

0 Answers0