Eclipse Debugging with Pointers and Arrays

In the C programming language it is good practice to pass values by reference (using a pointer), especially for large set of data. For example the following function takes a message string and pointer to integer data which then is printed to the console:

static void printData(const char *msg, const int *intBuf, size_t bufSize) {
  puts(msg); /* print message */
  for(int i=0; i<bufSize;i++) {
    printf("buf[%i] = %i\n", i, intBuf[i]);
  }
}

I can use it as below:

static int intArray[] = {0, 1, 2, 3, 4, 5};

printData("int buf Data dump", intArray, sizeof(intArray)/sizeof(intArray[0]));

Which then prints something like this to the console:

int buf Data dump
buf[0] = 0
buf[1] = 1
buf[2] = 2
buf[3] = 3
buf[4] = 4
buf[5] = 5

So far so good. Readers familiar with the C/C++ programming language know that pointers and arrays are kind of interchangeable: a pointer to an element can always be treated as a pointer to an array of elements. As with the code above, the function printData() receives two pointers which are actually pointers to arrays.

When debugging the function printData() in Eclipse/CDT (shown below with the MCUXpresso IDE 10.2 which is based on Eclipse Oxygen, but it is pretty much the same in any Eclipse version I’m aware of), then the parameters are displayed as normal pointers. Because this is what the function has received and knows about it.

Debugging Pointers

Debugging Pointers

Dereferencing the pointer in the Variables view will only show the first element:

First array element shown

First array element shown

But how to see the full array in the debugger?

For a pointer to a character the debugger can show the string (or array of char):

char array pointer

char array pointer

See Eclipse Debugging with Strings and Eclipse Debugging with Strings – Part 2 for a detailed discussion about how to debug string with Eclipse.

For pointers to other data types, that obviously is not the case, but Eclipse recognizes that the address matches the ‘intArray’ memory object:

intArray

intArray

How to see the array behind that pointer? For this there is the context menu to display a pointer as an array:

Display as Array

Display as Array

Because the size of the array is not defined, I can specify the length of the array to be displayed:

Display as Array Dialog Box

Display as Array Dialog Box

And voilà:

Array Values

Array Values

💡 Obviously it will show ‘random’ values if I decide to show more data elements than the array really has defined.

To go back and show the normal pointer again, I can restore the original type:

Restore Original Type

Restore Original Type

That’s it :-).

Happy Arraying 🙂

Links

What do you think?

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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.