开发者

Access reading error when using class member variable

开发者 https://www.devze.com 2022-12-26 13:11 出处:网络
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 anoth

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


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.


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)
{
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消