1

Some simple code doesn't appear to be working, and I'm not sure why.

enter image description here

I'm dynamically creating an array of integers. The size of this array is shown as 15, but that's just because I'm trying to debug the program. The actual size is variable at runtime.

I browsed a few sites for how to dynamically create an integer array in C++. Sites like this one came up. Their code looks like my code: an array is created, and a for loop sets the value of each element in the array.

But after my for loop runs, I get an array of size 0. What could be the problem?

This is an extremely basic C++ program compiling on Windows using Visual Studio.

Jason
  • 6,878
  • 5
  • 41
  • 55
  • 2
    How are you determining the size of the array? Are you sure it's not just that the debugger doesn't understand `int *array = new int[15];` - I've yet to meet a debugger that DOES understand how to know the size of pointers... [Or understand the size of what is pointed to by the pointers, at leasts] – Mats Petersson Feb 09 '13 at 00:41
  • [Works for me](http://stacked-crooked.com/view?id=2a5fda30a5902bec4e60395b2da6e30f). – Rapptz Feb 09 '13 at 00:42
  • So this is the debugger's problem, especially since Rapptz got it working? – Jason Feb 09 '13 at 00:43
  • @MatsPetersson, normally the size of the array is determined by another integer variable (which is usually 12, but can change). Here it's a constant 15. – Jason Feb 09 '13 at 00:44
  • I believe you can tell the debugger that the value is an array instead of a singel value with ... or something. Look it up real quick. – Michael Dorgan Feb 09 '13 at 00:45
  • 1
    Like [this](http://stackoverflow.com/questions/972511/view-array-in-visual-studio-debugger)? – Jason Feb 09 '13 at 00:46
  • Oh my gosh that works. So it's a debugger thing. Thank you all so much. – Jason Feb 09 '13 at 00:46

2 Answers2

4

Pointer is different with array, debugger will only show first element content and its address. Your dynamic array is created properly, you could add array[0],... array[14] to watch window, you can see each variables in memory. enter image description here

Or past array memory address to DEBUG->Windows->Memory->Memory1, you can see each element in memory is initialized properly

enter image description here

If you declare static array, debugger should show you all elements:

 int array2[15];

These are basic debugging skills, it becomes handy when debugging applications.

billz
  • 44,644
  • 9
  • 83
  • 100
2

The debugger doesn't know it's a pointer to the start of an array, it thinks you have a plain pointer to an int (since that is the type of array: int*). So it shows the value of that int (the first element in the array), which is 0 here.

Your code is fine; you have indeed allocated (and initialized) an array of 15 ints correctly.

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • @Jason: Yep, the array is there. The debugger just doesn't know about it. There's not much you can do -- you need to remember the size of the array yourself in order for it to be useful anyway. If `new[]` works without throwing or returning a nullptr, then you can rest assured that it gave you everything you asked for. – Cameron Feb 09 '13 at 00:47
  • @Cameron: "There's not much you can do" - sure, there is. You can tell the debugger to interpret the pointer as an array and specify how many elements are in the array (if the debugger supports that). Then it will display all of the elements, not just the first one by itself. How you actuallly do this is debugger-specific, though. In Borland/Embarcadero, you can just right-click on the pointer and choose a "Range" menu item. In VC++, I'm not sure if it can be done in the debugger directly or not, but I do know that VC++ does support creating custom debug visualizers, if needed. – Remy Lebeau Feb 09 '13 at 01:04
  • @Remy: That's true ([this shows how for VS](http://stackoverflow.com/questions/972511/view-array-in-visual-studio-debugger), as posted in the comments by Jason), but you can't tell from that whether or not the elements are actually part of the array or belong to somebody other variable's memory. – Cameron Feb 09 '13 at 01:07