为什么调试器只显示数组指针中的一个元素? [英] Why does the debugger show only one element from my array pointer?

查看:48
本文介绍了为什么调试器只显示数组指针中的一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道 new 是执行此操作的C ++方法.我只是在说明,有多种方法可以重现此错误,而且两种方法都令人沮丧.

此源文件有两种形式.我正在尝试调试另一个编程任务,但我没有在此寻求帮助.基本上,我试图将 set 重新实现为一个具有大小字段和指向 int 数组的指针的类.这是使用 new 的代码:

testnew.cpp

  int main(){int大小= 1;int * elements = new int [size];elements [0] = 0;大小++;int * temp =新的int [size];对于(int i = 0; i<(size-1); i ++){temp [i] = elements [i];}delete []元素;temp [size] =大小;元素=温度;elements [1] = 1;delete []元素;} 

并再次使用不太受欢迎的 alloc 函数:

testalloc.cpp

  int main(){int大小= 1;int * elements =(int *)malloc(sizeof(int)* size);elements [0] = 0;大小++;elements =(int *)realloc(elements,size * sizeof(int));elements [1] = 1;免费(元素);} 

在两种情况下,我的目标都是创建一个数组,然后将其追加.但是,在这两种情况下,在Visual Studio 2010中构建并运行它之后,该数组根本不会增长,并且只有1个插槽"可供我的项目放入.在VS的调试器中,我观察了 elements 数组指针;随附的是屏幕截图.两种版本的代码都相同.

-断点位于 delete [] / free()调用中.

严重的是,我在做什么错?这必须是一个逻辑错误,我梳理了四分之一的 malloc / realloc new 实例,并读取和重新阅读我的教科书,我看不出有什么问题!

我觉得我应该有一行将分配的内存分割成一个数组,但是 new int [] 调用不可以吗?

解决方案

除了代码的其他问题外,您还必须执行
和:在Visual Studio调试器中查看数组?

First off: I know that new is the C++ way of doing this. I am simply showing that there is more than one way to reproduce this error, and both are incredibly frustrating.

I have two forms of this source file. I'm trying to debug yet another programming assignment, but I'm not asking for help on that. Basically, I'm trying to re-implement set as a class with fields for size and a pointer to an int array. Here's the code using 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;
}

and again, using the less-preferable alloc functions:

testalloc.cpp

int main()
{
    int size = 1;
    int * elements = (int *) malloc(sizeof(int) * size);
    elements[0] = 0;
    size++;
    elements =(int *) realloc(elements,size * sizeof(int));
    elements[1] = 1;
    free(elements);
}

In both cases, my goal is to create an array, and then append to it. However, in both cases, after building and running it in Visual Studio 2010, the array is not grown at all, and only has 1 "slot" for my items to go into. In VS's debugger, I have watches on the elements array pointer; attached is a screenshot. It is the same for both versions of the code.

-- the breakpoint is at the delete[]/free() call.

Seriously, what am I doing wrong? This has to be a logic error, and I've combed through a quarter dozen examples of both malloc/realloc and new, and read and re-read my textbook, and I can't see what's wrong!

I feel like I should have a line that splits the allocated memory into an array, but doesn't the new int[] call do that?

解决方案

Besides any other issues with the code, you must either do what Joe Gauterin mentions and have multiple watches, or set a watch like this:

elements,5

will result in:

I don't know if there is an updated list for VS2010, but this still works: Symbols for Watch Variables
And: View array in Visual Studio debugger?

这篇关于为什么调试器只显示数组指针中的一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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