C++ STL 矢量模板如何在 Visual Studio 编译器实现中存储其对象? [英] How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?

查看:30
本文介绍了C++ STL 矢量模板如何在 Visual Studio 编译器实现中存储其对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 autoexp.dat 和 DLL 扩展 Visual Studio 2003 调试器,以改进它在监视窗口中显示数据的方式.我使用 DLL 而不仅仅是基本的 autoexp.dat 功能的主要原因是我希望能够有条件地显示内容.例如我希望能够说如果名称成员不是空字符串,则显示名称,否则显示 [其他成员]"

I am extending the Visual Studio 2003 debugger using autoexp.dat and a DLL to improve the way it displays data in the watch window. The main reason I am using a DLL rather than just the basic autoexp.dat functionality is that I want to be able to display things conditionally. e.g. I want to be able to say "If the name member is not an empty string, display name, otherwise display [some other member]"

我对 OOP 很陌生,对 STL 没有任何经验.所以可能是我遗漏了显而易见的东西.

I'm quite new to OOP and haven't got any experience with the STL. So it might be that I'm missing the obvious.

我无法显示向量成员,因为我不知道如何获取指向存储实际值的内存的指针.

I'm having trouble displaying vector members because I don't know how to get the pointer to the memory the actual values are stored in.

我认为这些值存储在连续的内存块中是否正确?有没有办法访问指向该内存的指针?

Am I right in thinking the values are stored in a contiguous block of memory? And is there any way to get access to the pointer to that memory?

谢谢!

[edit:] 澄清我的问题(我希望):

[edit:] To clarify my problem (I hope):

在调试器调用的 DLL 中,我使用了一个名为 ReadDebuggeeMemory 的函数,该函数生成对象使用的内存的副本.它不会复制对象指向的内存.所以我需要知道内部指针的实际地址值,以便能够调用 ReadDebuggeeMemory .目前,获取向量内容的常用方法是返回垃圾,因为该内存尚未被复制.

In my DLL, which is called by the debugger, I use a function called ReadDebuggeeMemory which makes a copy of the memory used by an object. It doesn't copy the memory the object points to. So I need to know the actual address value of the internal pointer in order to be able to call ReadDebuggeeMemory on that as well. At the moment, the usual methods of getting the vector contents are returning garbage because that memory hasn't been copied yet.

[更新:]

我得到了垃圾,即使我正在查看正确的指针 _Myfirst 因为我正在创建向量的额外副本,而我应该使用指向向量的指针.那么问题就变成了:如何通过指向向量的指针来访问指向向量内存的指针?有意义吗?

I was getting garbage, even when I was looking at the correct pointer _Myfirst because I was creating an extra copy of the vector, when I should have been using a pointer to a vector. So the question then becomes: how do you get access to the pointer to the vector's memory via a pointer to the vector? Does that make sense?

推荐答案

标准向量中的元素被分配为一个连续的内存块.

The elements in a standard vector are allocated as one contiguous memory chunk.

你可以通过获取第一个元素的地址来获得一个指向内存的指针,这有几种方法:

You can get a pointer to the memory by taking the address of the first element, which can be done is a few ways:

std::vector<int> vec;
/* populate vec, e.g.: vec.resize(100); */

int* arr = vec.data();   // Method 1, C++11 and beyond.
int* arr = &vec[0];      // Method 2, the common way pre-C++11.
int* arr = &vec.front(); // Method 3, alternative to method 2.

但是,除非您需要将底层数组传递给某些旧接口,否则通常您可以直接在 vector 上使用运算符.

However unless you need to pass the underlying array around to some old interfaces, generally you can just use the operators on vector directly.

请注意,您最多只能访问返回值的 vec.size() 元素.超出此范围的访问是未定义的行为(即使您认为为它保留了容量).

Note that you can only access up to vec.size() elements of the returned value. Accessing beyond that is undefined behavior (even if you think there is capacity reserved for it).

如果你有一个指向向量的指针,你可以通过取消引用来做上面同样的事情:

If you had a pointer to a vector, you can do the same thing above just by dereferencing:

std::vector<int>* vecptr;

int* arr = vecptr->data(); // Method 1, C++11 and beyond.
int* arr = &(*vecptr)[0];  // Method 2, the common way pre-C++11.
int* arr = &vec->front();  // Method 3, alternative to method 2.

尽管如此,请尝试获得对它的引用.

Better yet though, try to get a reference to it.

您想出了解决方案:

int* vMem = vec->_Myfirst;

只有在该特定编译器版本的特定实现上才会起作用.这不是标准的,所以不能保证这在编译器之间工作,甚至在不同版本的编译器之间工作.

The only time this will work is on that specific implementation of that specific compiler version. This is not standard, so this isn't guaranteed to work between compilers, or even different versions of your compiler.

如果您仅在该单一平台上进行开发似乎还可以编译器,但最好选择标准方式.

It might seem okay if you're only developing on that single platform & compiler, but it's better to do the the standard way given the choice.

这篇关于C++ STL 矢量模板如何在 Visual Studio 编译器实现中存储其对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆