如果您有一个静态分配的数组,Visual Studio调试器可以轻松显示所有数组元素.但是,如果您有一个动态分配并由指针指向的数组,那么当您单击+以展开它时,它将仅显示该数组的第一个元素.是否有一种简单的方法来告诉调试器,将这些数据显示为Foo类型和X大小的数组?
我正在使用2D阵列的Vs2010 c ++工作.我从1D指针开始,使用[]操作如下:
class CMatrix
{
void clear();
public:
int nRows;
int nCols;
short * MyMat;
CMatrix();
CMatrix(int r,int c);
~CMatrix(void);
void SetMatrix(int r,int c);
short * operator[] (const int row)
{
return MyMat + (row*nCols);
}
};
Run Code Online (Sandbox Code Playgroud)
我不介意改为2D指针.
但是我的问题是调试.因为我使用指针,所以无法看到数组内容.
还有其他选择吗?
我不喜欢使用矢量.
首先:我知道这new是C++的做法.我只是表明有不止一种方法可以重现这个错误,而且两者都非常令人沮丧.
我有两种形式的源文件.我正在尝试调试另一个编程任务,但我不是在寻求帮助.基本上,我正在尝试重新实现set一个类,其中包含大小字段和指向int数组的指针.这是使用的代码new:
testnew.cpp
int main()
{
int size = 1;
int *elements = new int[size];
elements[0] = 0;
size++;
int * temp = new int[size];
for (int i = 0; i < (size - 1); i++)
{
temp[i] = elements[i];
}
delete[] elements;
temp[size] = size;
elements = temp;
elements[1] = 1;
delete[] elements;
}
Run Code Online (Sandbox Code Playgroud)
再次,使用不太优选的alloc功能:
testalloc.cpp
int main()
{
int size = 1;
int * elements = (int *) …Run Code Online (Sandbox Code Playgroud) 这对我来说似乎很简单,但我实际上找不到任何明确说明这一点的麻烦.
是std::string stringArray[3] 不是会创建一个std::string对象数组SomeType typeArray[3]?实际数字无关紧要; 我随便挑了3个.
在Visual Studio 2010调试器中,它似乎创建单个字符串而不是字符串数组.为什么?
狂野猜测:它是调用默认std::string构造函数,然后调用未使用的索引3访问吗?如果是这样,为什么不在空字符串上引起越界异常呢?
这是否与重载[]运算符有关?
有很多方法可以编写相同的东西,而无需专门使用std::string没有问题的数组,但这种行为的解释/理由是什么?这对我来说似乎违反直觉.
编辑:我发现这个线程std :: string数组元素访问,其中答案的注释似乎观察到相同的行为.
有没有特别需要注意的动态自定义类型数组?
我正在尝试创建ConditionParameter的动态数组(下面的定义)
ConditionParameter* m_params;
...
m_params = new ConditionParameter[m_numParams];
Run Code Online (Sandbox Code Playgroud)
但是上面一行的结果只是ConditionParameter类型的一个新对象,其地址存储在m_params中.
struct ConditionParameter
{
ConditionParameter() :
type(OBJ_TYPE_OBJECT),
semantic(OP_SEMANTIC_TYPE_NONE),
instance(NULL),
attrib(ATTRIB_TYPE_NONE),
value(0)
{}
ConditionParameter(const ConditionParameter& other)
{
attrib = other.attrib;
instance = other.instance;
semantic = other.semantic;
type = other.type;
value = other.value;
}
ConditionParameter& operator = (ConditionParameter& other)
{
attrib = other.attrib;
instance = other.instance;
semantic = other.semantic;
type = other.type;
value = other.value;
return *this;
}
ObjectType type;
OperandSemanticType semantic;
Object* instance;
AttributeType attrib;
int value;
};
Run Code Online (Sandbox Code Playgroud)