Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger Option to View Pointer as Array. #172

Closed
skl131313 opened this issue Aug 27, 2016 · 29 comments
Closed

Debugger Option to View Pointer as Array. #172

skl131313 opened this issue Aug 27, 2016 · 29 comments

Comments

@skl131313
Copy link

For dynamically allocated arrays are simply represented as a pointer, which has no other indicator that it is an array. It would be nice to have a feature to view a pointer as an array as to view more than simply the first element of the array. In the "variables" tab and also when previewing a variable by hovering over it while debugging.

@pieandcakes
Copy link
Contributor

@sprinkle131313 with our 0.9.3 release, we updated to enable gdb's pretty-printing in launch.json as a default. This should resolve this issue. Please comment/reopen with more information if it doesn't

@kevinmoran
Copy link

kevinmoran commented Feb 17, 2017

It would make a HUGE difference if I could type "some_pointer,x" in the watch window to view some_pointer as an array with x elements.

As it stands I have to use LLDB's memory read syntax, which I constantly have to google and sometimes doesn't seem to work. I've actually resorted to printf debugging for large arrays the last few days. Could we please reopen this issue or can I open another to look into something like that being implemented? It's my number 1 gripe with VSCode debugging at the moment (one of my only gripes).

@pieandcakes
Copy link
Contributor

pieandcakes commented Feb 17, 2017

@kevinmoran We have updated our version of the shipped LLDB to allow lldb's python pretty printer to work. Your other option is to look at our Natvis visualizer option. The commands you are putting in the watch window are sent to the underlying debugger to evaluate.

@kevinmoran
Copy link

kevinmoran commented Feb 17, 2017

Thanks for getting back to me, have a decent solution now. For anyone who found themselves here Googling this same problem, you can put this in the watch window:
*(int(*)[10])some_pointer
to print "some_pointer" as a 10-element array of type int.

Would still argue that having a simpler solution is a basic debugger feature that should be added, this is way too much syntax/thinking/remembering to do something basic. Debuggers should get out of your way, not challenge you when you're trying to think about your actual code.

@salda
Copy link

salda commented May 4, 2017

I agree with kevinmoran, that retyping the pointer is too complicated by hand and should be done by debugger.

@pieandcakes
Copy link
Contributor

@salda We will take it as a feature request but the underlying debugger is lldb or gdb so the change would need to happen in the debugger itself and not the extension.

@noobling
Copy link

noobling commented Nov 8, 2017

#172 (comment)
Has this feature been added for pointer to pointers? e.g. char**

@creikey
Copy link

creikey commented Jun 20, 2018

@noobling Yes, I am having trouble printing pointer to pointers as well

@giuscri
Copy link

giuscri commented Feb 3, 2019

For anyone who found themselves here Googling this same problem, you can put this in the watch window:
*(int(*)[10])some_pointer
to print "some_pointer" as a 10-element array of type int.

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

@ghost
Copy link

ghost commented May 16, 2019

If you do what @giuscri suggests and put a breakpoint in the function that computes the pointer, the watch will stall indefinitely and cannot be deleted (Windows 10)

eg *tests()@4 will stall if there is a breakpoint in tests(), a function which returns a pointer to a list of test results.

reloading the window after this happens seems to fix it.

@brunohenriquepj
Copy link

brunohenriquepj commented Apr 17, 2020

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

If you have the array length in a variable (e.g., length) you can do this: *some_pointer@length

@teichert
Copy link

Thanks @giuscri!
If anyone else was wanting to use *s@strlen(s) to display a c-string without typing in the length, it turns out that the thing to do in gdb is to use: $_strlen. So if you have a c-string, s, you can display the underlying char array by using the following as a watch expression (the + 1 is for showing the terminating null char): *s@$_strlen(s)+1

@tuket
Copy link

tuket commented Feb 19, 2021

For anyone who found themselves here Googling this same problem, you can put this in the watch window:
*(int(*)[10])some_pointer
to print "some_pointer" as a 10-element array of type int.

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

Looks like this doesn't work anymore?

@Gaming32
Copy link

Gaming32 commented Mar 9, 2021

For anyone who found themselves here Googling this same problem, you can put this in the watch window:
*(int(*)[10])some_pointer
to print "some_pointer" as a 10-element array of type int.

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

Looks like this doesn't work anymore?

It works fine for me

asialasr pushed a commit to asialasr/vscode-cpptools that referenced this issue Mar 12, 2021
Add plumbing for AD7 evaluation / property info flags
@tripathics
Copy link

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

If you have the array length in a variable (e.g., length) you can do this: *some_pointer@length

Thank you @brunohenriquepj , what would I do without you.. I tried all the suggestions above your answers, none of them worked on my linux machine. But You helped alot.. Will remember this

*some_pointer@length

BTW I have just started coding with C.

@JJJohan
Copy link

JJJohan commented Jul 29, 2021

For anyone who found themselves here Googling this same problem, you can put this in the watch window:
*(int(*)[10])some_pointer
to print "some_pointer" as a 10-element array of type int.

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

Looks like this doesn't work anymore?

For MSVC (i.e. when using cppvsdbg instead of cppdbg) substitute the @ symbol with a comma, and omit the dereferencing, i.e. i.e. some_pointer,10. Only just figured this out myself!

@tripathics
Copy link

tripathics commented Jul 29, 2021

For anyone who found themselves here Googling this same problem, you can put this in the watch window:
*(int(*)[10])some_pointer
to print "some_pointer" as a 10-element array of type int.

or, even simpler: *some_pointer@3. Bonus point, check out *argv@argc!

Looks like this doesn't work anymore?

For MSVC (i.e. when using cppvsdbg instead of cppdbg) substitute the @ symbol with a comma, and omit the dereferencing, i.e. i.e. some_pointer,10. Only just figured this out myself!

@JJJohan Can I ask what is cppvsdbg? And is it available on Linux?

@JJJohan
Copy link

JJJohan commented Jul 29, 2021

@JJJohan Can I ask what is cppvsdbg? And is it available on Linux?

@tripathics This is referring to the debugger configured in the launch.json file in VS Code. It'll be Windows specific as it's meant for using Visual Studio's toolset as the backend. It's briefly described in the launch json reference.

@tripathics
Copy link

@JJJohan Can I ask what is cppvsdbg? And is it available on Linux?

@tripathics This is referring to the debugger configured in the launch.json file in VS Code. It'll be Windows specific as it's meant for using Visual Studio's toolset as the backend. It's briefly described in the launch json reference.

Btw I am having problem with 2d or 3d array in watch.. idk how to use that pointer syntax for multidimensional arrays in watch.. can you help me?

@AmirhosseinR
Copy link

For 2d array use this (int pointer_2d[10][15];):
(int()[10][15])pointer_2d
and so on.

@batunpc
Copy link

batunpc commented Aug 21, 2021

For anyone who having issues still I found this working on my macOS if I put this on my watch window:
pointerName,[size]
(pay attention having no asterisk at the beginning)

@z6m8n7ljw
Copy link

For anyone who having issues still I found this working on my macOS if I put this on my watch window: pointerName,[size] (pay attention having no asterisk at the beginning)

The only way by far that works on my VS for mac !

@q1uyj
Copy link

q1uyj commented Jan 28, 2022

All what you said doesn't work for me :(
image

@z6m8n7ljw
Copy link

All what you said doesn't work for me :( image
In your case, this would be just like this: arr,[5]

@motla
Copy link

motla commented Jan 30, 2022

All what you said doesn't work for me :(

@Ch1ldr3n What debugger are you using? I presume LLDB cause I got the same issue.

@Ronalyo
Copy link

Ronalyo commented Oct 18, 2022

here's what i've gathered, hope this helps

general forms for expanding arrays (of any dimension, with arbitrary length):

  1. **...arr@...@len2@len1: same amount of asterisks as dimensions, goes from top to bottom, where order of len is reversed
  2. *(type(*)[len1][len2][...])arr: goes from bottom to top, order of len is as specified

there's also one for expanding 2 dimensions specifically:
*arr@len2,len1: len order reversed, seperate lens with comma

  • bottom means element level, top means highest level, see examples for more clarity

Examples:
image

  1. 1st(top) dimension has len = 4, 2nd has 2, 3rd has 3. unspecified dimensions use default len
    image
  2. highest shown(not top) dimension has len = 3, next has 2, next(bottom) has 4. unspecified dimensions isn't shown
    image
    extra: special 2d expansion is basically method 1. with different syntax
    image

@dsabala
Copy link

dsabala commented Jan 19, 2023

What is the reference of *some_pointer@3 syntax? Where is documentation of such a statements?

@spking11
Copy link

spking11 commented Mar 8, 2023

What is the reference of *some_pointer@3 syntax? Where is documentation of such a statements?

Here, and the comma syntax is here.

@jackiesogi
Copy link

jackiesogi commented Jan 29, 2024

All what you said doesn't work for me :( image

I think it is because everyone use different types of debugger, it may look the same on the surface since vscode provides an unified UI, but the underlying debugger tool we are using is not the same. For example, I use LLDB as the debugger(you can search for the extension in vscode), the syntax some_pointer,[N] (no space) works well for me.

So, take a look at the documentation of the debugger(or debugger extension) you're using, and find something like Variable Formatting, you will learn about the syntax !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests