How to watch a range of values inside huge array?

3 07 2008


Assume you’ve an array with 1000 elements in it and you want to watch elements from 500 ~ 510, what will you do? One method is you can watch the array directly which will list all the 1000 variables and you’ve to scroll up to 500th element and have to watch. Is there any trick to watch just the range of 10 elements between 500 to 510?


Yes! Basically the idea is, you’ve to add – (Array+RangeStart)+RangeCount to the watch window. But there are different scenarios and obviously the trick varies slightly. See examples below.

1) If you have dynamically allocated array.
Check the code snippet.

// Array ptr with 1000 elements.
int* pArray = new int[1000];

// Initialize range 500 to 509 as 1.
for( int ArrayIndex = 500; ArrayIndex < 510; ++ArrayIndex )
{
    pArray[ ArrayIndex ] = 1;
}

In this case for watching range 500 ~ 510, you’ve to add – (pArray+500),10 to watch window. See the screenshot.

2) If you have array on stack.
If you’re array is on stack, if you add your array to watch window, the debugger automatically expands all elements, since it knows the max size of array. But we just want to see the range. See the code snippet.

// Array with 1000 elements.
int Array[1000] = { 0 };

// Initialize range 500 to 509 as 1.
for( int ArrayIndex = 500; ArrayIndex < 510; ++ArrayIndex )
{
    Array[ ArrayIndex ] = 1;
}

In this case for watching range 500 ~ 510, you’ve to add – ((int*)Array+500),10 to watch window. See the screenshot.


Watch window is a Treasure chest! isn’t it? 😉


Targeted Audience – Intermediate.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: