如何在totalview中将int *表示为数组?

发布于 2024-08-19 02:35:02 字数 98 浏览 3 评论 0原文

如何“潜水”一个指向动态分配的整数数组的 int * 并将其表示为固定的 int[] 数组?换句话说,如果我潜水 int * 它会显示地址和指向的 int,但我想查看所有整数的数组。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

极度宠爱 2024-08-26 02:35:02

我注意到这个问题上的 TotalView 标签。您是想问如何在 TotalView 中查看数组中的值吗?如果是这样,那么答案就很简单了。

假设您有一个 int * 类型的指针 p,并且当前它指向一个包含 10 个整数的数组。

步骤 1. 潜入指针。这是通过双击、单击鼠标中键或使用上下文菜单上的潜水选项来完成的——所有这些都是在将鼠标光标放在源代码窗格或堆栈帧窗格中的变量上之后完成的。

这将打开一个新窗口,其中显示

表达式:p
地址:0xbfaa1234
类型:int *

并在数据区域中向下会显示类似

0x08059199 -> 0x000001a5 (412)

这个窗口显示的是指针本身,列出的地址是指针的地址。该值(上例中的 0x08059199)是指针具有的实际值。箭头右侧的所有内容都只是一个“提示”,告诉​​您想要它指向的方向。

步骤 2. 再次按下指针。重复双击或鼠标中键,这次是在变量窗口中的数据值上。 (所以你双击0x08059199的地方)。

这将有效地“取消引用”指针。现在窗口的焦点不再是指针本身,而是指针所指向的东西。请注意,地址框现在包含 0x08059199,这是之前的值。

表达式:*(((int *) p))
地址:0x08059199
输入:int

并在数据区域中向下,它会显示类似

0x000001a5 (412)

步骤 3. 将数据窗口转换为您想要的类型。只需单击类型字段并将其更改为 int[10]。然后按回车键。

这告诉调试器 0x08059199 是 10 个整数数组的开头。

该窗口将增加两个新字段:切片和过滤器。您现在可以先不管它们,但它们以后可能会有用。

数据区域现在将显示两列“字段”和“值”以及 10 行。

字段列将是数组 [0] - [9] 中的索引,值列将告诉您每个数组位置中有哪些数据。

其他提示:

  • 在更复杂的数据结构中,您可能需要深入研究各个元素(也可能是指针,深入研究也会取消对它们的引用)

  • 您始终可以转换为不同的类型或长度,以“就好像”数据一样查看数据

  • 您可以通过单击值列并编辑在那里找到的内容来编辑实际数据值。当您想要从应用程序中引发特定的错误行为时,这非常有用

  • 您始终可以使用“<”撤消潜水操作变量窗口右上角的图标。

上有一些可能对您有用的在线视频

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

特别是有一个标记为“TotalView 入门”。

请随时联系 Rogue Wave Software 了解 TotalView 使用技巧!
roguewave dot com 的支持是一个很好的地址。

克里斯·戈特布拉斯
(roguewave dot com 的 Chris dot Gottbrath)
Rogue Wave Software TotalView 产品经理

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

不寐倦长更 2024-08-26 02:35:02

这不是很难,但我忘记了它到底是如何工作的。我给你找到了一个解释它的页面;)。我认为要使用名为 test 的整数进行点和数组,您应该使用 &test 来获取它。
只需查看此页面:

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

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/

我家小可爱 2024-08-26 02:35:02

如果不确切知道数组中有多少个整数,则无法有意义地执行此操作。

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

不知在何时 2024-08-26 02:35:02

如果您有一个 int *p 指向连续 int 数据中的第一个元素(无论是动态分配的还是静态数组),您可以将其索引为就像数组:

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);

您必须知道通过这种方式访问​​的有效数据的大小。

因此,假设我有上面的数据,并且想要编写一个函数来打印它。像这样声明函数是行不通的:

void print_array(int *data);

因为当您调用 print_array(data); 时,函数不知道要打印的元素数量。

您可以将 print_array() 定义为:

void print_array(int *data, size_t n);

其中 n 表示调用者必须提供的 data 指向的有效元素的数量。或者您可以决定每个“数组”都以一个哨兵值结尾,该值仅在数据末尾使用,否则不是有用的值:

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

然后您可以声明您的 print_array() as:

void print_array(int *data);

并继续在函数定义中索引到data,直到遇到哨兵:

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

因此,为了回答您的问题,您已经可以将指向有效动态分配数据的指针视为数组。当然,您必须记住分配数据的大小,这也需要在常规数组中执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文