指针提前访问违规

时间:2017-10-10 09:41:18

标签: c++ pointers struct

最近在使用API​​时学习Cpp,我遇到了指针问题。我将一个struct数组加载到一个指针中(这是我对发生的事情的理解),当我尝试前进到结构中的下一个元素时,我得到了以下错误:

抛出未处理的异常:读取访问冲突。 pt 是0x12181B4。发生

这是结构:

struct Record
  {
   __time32_t        ctm;                 
   double            low,high;                
   char              flags;                  
  };

然后我声明指向记录的指针:

Record* pt;

并通过此函数我将Record数组加载到pt(参数是一个描述请求的结构,并且total填充了返回的记录数):

pt = api->RecordRequest(&parameters, &trtotal);

返回指向结构数组Record的指针。

现在,当我尝试这样做时:

 if (trtotal > 0)
        {

            pt = pt + (trtotal-1);

// without the part below it works
            if (DIRECTION == 0) {
                while (HIGH != pt->high)
                {
                    pt--; // <- this is where the error happens
                }
            }else if(DIRECTION == 1) {
                while (LOW != pt->low) 
                {
                    pt--;
                }
            }
// read the pt like this pt->high ; pt->low; and do something with the info
}

我尝试做的基本方法是通过返回一个位置来移动数组中的下一个元素。

谁能告诉我自己做错了什么?

由于

修改1:

trtotal总是超过3个。

我做了这个例子,除了我无法重现错误。所以看来我有一个不同的问题,而不仅仅是缺乏如何访问结构数组的知识。

可能不止一次或类似地摧毁指针?我会继续挖掘。感谢您的反馈。建立这个例子有助于更多地了解实际发生的事情,即使它没有解决问题。

有没有办法转储pt指针的内容,看看究竟是什么类似于PHP中的var_dump?

#include "stdafx.h"

struct Record
{
    int        ctm;
    double            low, high;
    char              flags;
};


Record records[] = {
    { 0, 1.74, 1.75, 1 },
    { 1, 3.20, 3.21, 2 },
    { 2, 1.20, 1.21, },
    { 3, 1.16, 1.17, }
};



int trtotal = 4;
double difference;
Record* pt;
int DIRECTION = 1;
double HIGH = 1.20;
double LOW = 3.20;


int main()
{

    pt = records;


    if (trtotal > 0)
    {

        pt = pt + (trtotal - 1);

    if (DIRECTION == 0) {
            while (HIGH != pt->high)
            {
                pt--;
            }
        }
        else if (DIRECTION == 1) {
            printf("Direction is 1.\n");
            while (LOW != pt->low)
            {
                printf("Moving pointer one step back...\n");
                pt--;
            }
        }

        difference = pt->high - pt->low;
        printf("High is %f and Low is %f\n", pt->high, pt->low);

        printf("Difference is: %f\n", difference);
        printf("CTM is: %d\n", pt->ctm);

    }
    else {
        printf("Request failed.\n");
    }

    return 0;
}

编辑2:

刚刚想出如何继续处理RecordRequest函数返回的信息。

为了防止错误,我只需要初始化一个Record类型的新指针,然后在temp中加载pt(并使其前进),如:

Record* temp;

 if (trtotal > 0)
        {
            int step = 1;
            temp = pt + (trtotal-step);

    if (DIRECTION == 0) {
                while (HIGH != temp->high)
                {
                    if (step > trtotal) {
                        break;
                    }
                    temp = NULL;
                    step++;
                    temp = pt + (trtotal - step);

                }
            }else if(DIRECTION == 1) {
                while (LOW != temp->low) 
                {
                    if (step > trtotal) {
                        break;
                    }
                    temp = NULL;
                    step++;
                    temp = pt + (trtotal - step);

                }
            }
// read the pt like this temp->high ; temp->low; and do something with the info
}

显然,代码的实际问题是在pt = pt +(trtotal-1);之后用pt做了一些事情。在此之后用pt做的任何事情(例如pt--; pt ++;或pt = pt +(trtotal-2)都会引发访问冲突。

我还没弄明白为什么如果有人会启发我,我相信它会给我带来很多好处,而不是仅仅向前迈进而不知道为什么我这样做的新方法已经完成了有用。

谢谢!

0 个答案:

没有答案
相关问题