开发者

how to represent int * as array in totalview?

How do I 'dive' an int * which points to a 开发者_如何学运维dynamically allocated array of integers and represent it as a fixed int[] array? Put otherwise, if I dive an int * it shows the address and the int pointed to, but instead I would like to see the array of all of the integers.


I noticed the TotalView tag on this question. Are you asking how to see the values in your array in totalview? If so then the answer is pretty easy.

Lets say you have a pointer p which is of type int * and you have it currently pointing towards an array with 10 integers.

Step 1. Dive on the pointer. That's accomplished by double clicking, clicking the middle mouse button, or using the dive option on the context menu -- all after having placed the mouse cursor on the variable int he source code pane or the stack frame pane.

This will bring up a new window that will say

Expression: p Address: 0xbfaa1234 Type: int *

and down in the data area will say something like

0x08059199 -> 0x000001a5 (412)

This window is showing you the pointer itself, the address listed is the address of the pointer. The value (0x08059199 in the example above) is the actual value that the pointer has. Everything to the right of the arrow is just a "hint" telling you want it points to.

Step 2. Dive on the pointer again. Repeat the double click or middle mouse button, this time on the data value in the variable window. (So you are double clicking where it says 0x08059199).

This will effectively "dereference" the pointer. Now the window is focused not on pointer itself but the thing that the pointer pointed to. Notice that the address box now contains 0x08059199 which was the value before.

expression: *(((int *) p)) Address: 0x08059199 Type: int

and down in the data area it will say something like

0x000001a5 (412)

Step 3. Cast the data window to the type you want. Just click in the type field and change it to say int[10]. Then hit return.

This tells the debugger that 0x08059199 is the beginning of an array of 10 integers.

The window will grow two new fields: Slice and Filter. You can leave those alone for now, but they can be useful later.

The data area will now show two columns "field" and "value" and 10 rows.

The field column will be the index in the array [0] - [9] and the value column will tell you what data you have in each array location.

Other tips:

  • In more complicated data structures you can may want to dive on individual elements (which might also be pointers, diving will dereference them as well)

  • You can always cast to different types or lengths to look at data "as if it was" whatever

  • You can edit the actual data values by clicking on the value column and editing what you find there. This is useful when you want to provoke specific mis-behavior from your application

  • You can always undo diving operations with the "<" icon in the upper right hand corner of the variable window.

There are some online videos that you might find helpful at

http://www.roguewave.com/products/totalview/resources/videos.aspx

in particular there is one labeled "getting started with TotalView".

Don't hesitate to contact us at Rogue Wave Software for TotalView usage tips! support at roguewave dot com is a good address for that.

Chris Gottbrath (Chris dot Gottbrath at roguewave dot com) TotalView Product Manager at Rogue Wave Software


It's not very hard, but i forgot how it exactly works. I found you a page which explains it tho ;). I think to point and array with ints called for example test you should get it using &test. Just check this page out:

http://www.cplusplus.com/doc/tutorial/pointers/


You can't meaningfully do this without knowing exactly how many ints are in the array.


If you have an int *p that points to the first element in a contiguous int data, either dynamically allocated, or a static array, you can index it as if it were an array:

int *data = malloc(3 * sizeof *data);
int *p;
/* malloc error detection omitted for brevity */
data[0] = 1;
data[1] = 2;
data[3] = 42;

p = data;
assert(p[0] == 1);
assert(p[1] == 2);
assert(p[2] == 42);

You have to know the size of the valid data you are accessing this way.

So, let's say I have data as above, and want to write a function to print it. It won't do to declare the function like this:

void print_array(int *data);

because when you call print_array(data);, the function doesn't know the number of elements to print.

You could define your print_array() like:

void print_array(int *data, size_t n);

where n denotes the number of valid elements pointed to by data, that the caller has to supply. Or you could decide that every "array" will end with a sentinel value, a value that is used only at the end of the data, and is not useful value otherwise:

data[2] = 0; /* data[0] and data[1] are useful, valid values
                and data[2] is 0 to signify the end of the data */

Then you can declare your print_array() as:

void print_array(int *data);

and keep indexing into data in the function definition till you hit a sentinel:

void print_array(int *data)
{
    size_t i;
    for (i=0; data[i] != 0; ++i)
        printf("%d\n", data[i]);
}

So, to answer your question, you can already treat a pointer to a valid dynamically allocated data as an array. You have to remember the size of the allocated data of course, which you would need to do in a regular array too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜