2

I have a dynamically allocated array of structs.

I want to cast it to an array type so my debugger will show the whole array.

Is it possible?

I know that this cast is not a good idea, but it's only a cast for expression evaluation of the debugger. –

Yuval Cohen
  • 233
  • 3
  • 11
  • 1
    In MSVC debugger you can just write: `p, 100` assuming that p is struct ptr and 100 is dynamic array length. No need to change the code. Hopefully, another debuggers have similar tricks. – Alex F Jun 18 '13 at 09:01
  • 1
    You can cast it when asking the prompt to your debugger, in GDB that would be `p (struct my_struct[size])var` – Rerito Jun 18 '13 at 09:01
  • 4
    Yes it is possible. The real question is why you didn't allocate the array as an array type to begin with. [How to allocate arrays dynamically](http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c). – Lundin Jun 18 '13 at 09:02
  • 1
    This depends a lot on your debugger. Which one are you using? – starmole Jun 18 '13 at 10:16
  • MetaWare, I want to evaluate this pointer as a static-allocated array so I could see few cells at once. This is why I want to evaluate it as a casted array. All the above didn't work. – Yuval Cohen Jun 18 '13 at 10:51
  • if you know the exact size of the array at compile time, then you could just use an static array, if not, you would not know the exact array type you want to cast to, because the dimension of an static array should be decided at compile time. – Frahm Jun 18 '13 at 13:14
  • Thanks, I know that this cast is not a good idea, but it's only a cast for expression evaluation of the debugger. – Yuval Cohen Jun 18 '13 at 16:46
  • Can you use `p *(struct anonymous (*)[20])ptr`? The cast converts the pointer `ptr` to a pointer to an array of 20 `struct anonymous`, and the `*` then dereferences it that pointer, which should print out 20 entries, with luck. – Jonathan Leffler Jun 18 '13 at 17:02
  • 2
    The name of the debugger you are using - is it a secret? – user93353 Jun 18 '13 at 17:13
  • MetaWare (I wrote it) – Yuval Cohen Jun 18 '13 at 17:20
  • 4
    Let me make sure I have this right: You wrote your own debugger? And you're asking us how to use it? – TonyK Jun 18 '13 at 17:30

1 Answers1

0
  • I want to cast it to an array type so my debugger will show the whole array. Is it possible?

Yes. For example, if you allocated array of 100 elements of type t_my_struct then cast pointer to t_my_struct => pointer to array of 100 elements of type t_my_struct:

t_my_struct * Dynamic = ( t_my_struct * )calloc( 100, sizeof *Dynamic );
t_my_struct (* Static)[ 100 ] = ( t_my_struct (*)[ 100 ] )Dynamic;

Now you can see Static as static array in debugger. Works in MSVC.

kotlomoy
  • 1,420
  • 8
  • 14