使用类成员变量时访问读取错误

发布于 2024-08-28 11:06:41 字数 1231 浏览 8 评论 0原文

我有一个在头文件中声明了私有成员变量的类。在我的构造函数中,我传入一些文件名并使用这些名称创建其他对象。这很好用。然而,当我尝试添加另一个成员变量并在构造函数中初始化它时,我遇到了访问读取冲突。我把代码发给了别人,在他的电脑上运行得很好。知道可能出什么问题吗?

这是有问题的代码:

.h 文件:

class QUERYMANAGER {
    INDEXCACHE *cache;
    URLTABLE *table;
    SNIPPET *snip;
    int* iquery[MAX_QUERY_LENGTH];
    int* metapointers[MAX_QUERY_LENGTH];
    int blockpointers[MAX_QUERY_LENGTH];
    int docpositions[MAX_QUERY_LENGTH];
    int numberdocs[MAX_QUERY_LENGTH];
    int frequencies[MAX_QUERY_LENGTH];
    int docarrays[MAX_QUERY_LENGTH][256];
    int qsize;



public:
    QUERYMANAGER();
    QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
    ~QUERYMANAGER();

这是 .cpp 文件:

#include "querymanagernew.h"
#include "snippet.h"
using namespace std;



QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname){
    cache = new INDEXCACHE(indexfname, btfname);
    table = new URLTABLE(urltablefname);
    snip = new SNIPPET(snippetfname, snippetbtfname);

    //this is where the error occurs
    qsize = 0;


}

我完全不知道是什么原因造成的 - 有什么想法吗?

谢谢,BSG

I have a class with private member variables declared in a header file. In my constructor, I pass in some filenames and create other objects using those names. This works fine. When I try to add another member variable, however, and initialize it in the constructor, I get an access reading violation. I sent the code to someone else and it works fine on his computer. Any idea what could be wrong?

Here is the offending code:

The .h file:

class QUERYMANAGER {
    INDEXCACHE *cache;
    URLTABLE *table;
    SNIPPET *snip;
    int* iquery[MAX_QUERY_LENGTH];
    int* metapointers[MAX_QUERY_LENGTH];
    int blockpointers[MAX_QUERY_LENGTH];
    int docpositions[MAX_QUERY_LENGTH];
    int numberdocs[MAX_QUERY_LENGTH];
    int frequencies[MAX_QUERY_LENGTH];
    int docarrays[MAX_QUERY_LENGTH][256];
    int qsize;



public:
    QUERYMANAGER();
    QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
    ~QUERYMANAGER();

This is the .cpp file:

#include "querymanagernew.h"
#include "snippet.h"
using namespace std;



QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname){
    cache = new INDEXCACHE(indexfname, btfname);
    table = new URLTABLE(urltablefname);
    snip = new SNIPPET(snippetfname, snippetbtfname);

    //this is where the error occurs
    qsize = 0;


}

I am totally at a loss as to what is causing this - any ideas?

Thanks, bsg

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

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

发布评论

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

评论(4

盗梦空间 2024-09-04 11:06:41

建议,分解数组:

class QUERYMANAGER
{
// Snip
    int* iquery[MAX_QUERY_LENGTH];
    int* metapointers[MAX_QUERY_LENGTH];
    int blockpointers[MAX_QUERY_LENGTH];
    int docpositions[MAX_QUERY_LENGTH];
    int numberdocs[MAX_QUERY_LENGTH];
    int frequencies[MAX_QUERY_LENGTH];
    int docarrays[MAX_QUERY_LENGTH][256];
    int qsize;
// Snip
};

看起来你应该有另一个结构:

struct Info
{
    int* iquery;
    int* metapointers;
    int blockpointers;
    int docpositions;
    int numberdocs;
    int frequencies;
    int docarrays[256];
};

QueryManager 现在看起来像:

class QueryManager
{
    INDEXCACHE *cache;
    URLTABLE *table;
    SNIPPET *snip;
    int qsize;
    Info  details[MAX_QUERY_LENGTH];
};

这可能有助于更好地封装主题。

Suggestion, factor out the arrays:

class QUERYMANAGER
{
// Snip
    int* iquery[MAX_QUERY_LENGTH];
    int* metapointers[MAX_QUERY_LENGTH];
    int blockpointers[MAX_QUERY_LENGTH];
    int docpositions[MAX_QUERY_LENGTH];
    int numberdocs[MAX_QUERY_LENGTH];
    int frequencies[MAX_QUERY_LENGTH];
    int docarrays[MAX_QUERY_LENGTH][256];
    int qsize;
// Snip
};

Looks like you should have another structure:

struct Info
{
    int* iquery;
    int* metapointers;
    int blockpointers;
    int docpositions;
    int numberdocs;
    int frequencies;
    int docarrays[256];
};

And the QueryManager now looks like:

class QueryManager
{
    INDEXCACHE *cache;
    URLTABLE *table;
    SNIPPET *snip;
    int qsize;
    Info  details[MAX_QUERY_LENGTH];
};

This may help encapsulate themes a little better.

潇烟暮雨 2024-09-04 11:06:41

您的依赖项可能不正确,并且没有重建必要的文件。尝试“干净”重建。

作为样式注释,请使用初始值设定项列表。

QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname,
                           char *snippetfname, char *snippetbtfname) :
    cache(new INDEXCACHE(indexfname, btfname)),
    table(new URLTABLE(urltablefname)),
    snip(new SNIPPET(snippetfname, snippetbtfname)),
    qsize(0)
{
}

并且您可能不需要创建这些项目指针:

class QUERYMANAGER {
    INDEXCACHE cache;
    URLTABLE table;
    SNIPPET snip;
...

QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname,
                           char *snippetfname, char *snippetbtfname) :
    cache(indexfname, btfname),
    table(urltablefname),
    snip(snippetfname, snippetbtfname),
    qsize(0)
{
}

Your dependencies are probably not right, and the necessary files aren't getting rebuilt. Try a "clean" rebuild.

As a note to style, use initializer lists.

QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname,
                           char *snippetfname, char *snippetbtfname) :
    cache(new INDEXCACHE(indexfname, btfname)),
    table(new URLTABLE(urltablefname)),
    snip(new SNIPPET(snippetfname, snippetbtfname)),
    qsize(0)
{
}

and you may not need to make those items pointers:

class QUERYMANAGER {
    INDEXCACHE cache;
    URLTABLE table;
    SNIPPET snip;
...

QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname,
                           char *snippetfname, char *snippetbtfname) :
    cache(indexfname, btfname),
    table(urltablefname),
    snip(snippetfname, snippetbtfname),
    qsize(0)
{
}
拔了角的鹿 2024-09-04 11:06:41

你建得干净吗?由于访问最后一个成员变量会崩溃,但分配给较早的成员变量却可以正常工作,要么您在使用实例时没有正确构造/分配实例,要么您的对象文件引用了旧版本的标头,而该标头没有'对象中还没有 qsize,因此没有分配足够的空间。或者类似的东西。

Have you built clean? Since accessing the last member variable blows up, but assigning to earlier ones works OK, either you're not constructing/allocating the instance right when you do use it, or you have object files that refer to older versions of the header that didn't have qsize in the object yet, and thus aren't allocating enough space. Or something along those lines.

黒涩兲箜 2024-09-04 11:06:41

正如预期的那样,这在我的机器上运行得很好:

#include <cstdlib>

struct INDEXCACHE {};
struct URLTABLE {};
struct SNIPPET {};

const std::size_t MAX_QUERY_LENGTH = 256;

class QUERYMANAGER {
    INDEXCACHE *cache;
    URLTABLE *table;
    SNIPPET *snip;
    int* iquery[MAX_QUERY_LENGTH];
    int* metapointers[MAX_QUERY_LENGTH];
    int blockpointers[MAX_QUERY_LENGTH];
    int docpositions[MAX_QUERY_LENGTH];
    int numberdocs[MAX_QUERY_LENGTH];
    int frequencies[MAX_QUERY_LENGTH];
    int docarrays[MAX_QUERY_LENGTH][256];
    int qsize;



public:
    QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
};

QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname)
    : cache(new INDEXCACHE(/*indexfname, btfname*/))
    , table(new URLTABLE(/*urltablefname*/))
    , snip(new SNIPPET(/*snippetfname, snippetbtfname*/))
    , qsize(0)
{
}

int main()
{
    QUERYMANAGER foo("blargl", "frxnl", "wrgxl", "brlgl", "srgl");
    return 0;
}

所以错误一定是在您没有显示的代码中。

顺便说一句,除了宏之外,所有大写名称都是 boo。它们使您的代码更难阅读,并使每个习惯更常见编码风格的人感到困惑。

As expected, this runs just fine on my machine:

#include <cstdlib>

struct INDEXCACHE {};
struct URLTABLE {};
struct SNIPPET {};

const std::size_t MAX_QUERY_LENGTH = 256;

class QUERYMANAGER {
    INDEXCACHE *cache;
    URLTABLE *table;
    SNIPPET *snip;
    int* iquery[MAX_QUERY_LENGTH];
    int* metapointers[MAX_QUERY_LENGTH];
    int blockpointers[MAX_QUERY_LENGTH];
    int docpositions[MAX_QUERY_LENGTH];
    int numberdocs[MAX_QUERY_LENGTH];
    int frequencies[MAX_QUERY_LENGTH];
    int docarrays[MAX_QUERY_LENGTH][256];
    int qsize;



public:
    QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
};

QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname)
    : cache(new INDEXCACHE(/*indexfname, btfname*/))
    , table(new URLTABLE(/*urltablefname*/))
    , snip(new SNIPPET(/*snippetfname, snippetbtfname*/))
    , qsize(0)
{
}

int main()
{
    QUERYMANAGER foo("blargl", "frxnl", "wrgxl", "brlgl", "srgl");
    return 0;
}

So the error must be in the code you're not showing.

BTW, all upper-case names are boo except for macros. They're making your code harder to read and confuse everyone used to a more common coding style.

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