指向C ++中结构的指针数组的指针

时间:2018-05-17 17:12:40

标签: c++ data-structures

我需要一些帮助,我正在学习数据结构,我有一个任务是编写一个基于struct的指针数组的程序,它可以添加元素并用array做其他任务。我有下一个级别的模型:

first level --> net of shops
    second level --> shop
        third level --> goods

我为此写了类型

typedef struct
{
    QString date;
    QString prod_code;
    QString name;
}goods;



typedef struct
{
    QString address;
    QString number;
    void **sublevel;
}shop;

typedef struct
{
    QString website;
    QString name;
    QString owner;
    QString address;
    void **sublevel;
}net;

然后我创建了全局变量void ** Start指向指针数组:

// init list

void ** init_list()
{
    void** p = new void*[SIZE_AR];

    p = p+2;
    ((int*)p)[COUNT_POS] = 0;
    ((int*)p)[SIZE_POS] = SIZE_AR;

    return p;
}
 void ** Start = init_list();

COUNT_POS - index of elements where I store count of currently used elemnets
SIZE_POS - size of array allocated in dynamic memory
SIZE_AR - default size for array

但是当我尝试将元素添加到最后一级时,我得到了分段错误 (前两个工作正常):

// expand array if it overfilled
void ExpandArrPtr (void **&ar,  int &SizeAr, int Cnt)
{
    void **arW;
    arW = new void*[SizeAr+DELTA+2];

    for (int K = SizeAr-1; K >= 0; K--) {
        arW[K+2] = ar[K];
    }
    SizeAr = SizeAr + DELTA;
    ar=ar-2;

    delete []ar;
    ar=arW+2;

    ((int*)ar)[COUNT_POS]  = Cnt;
    ((int*)ar)[SIZE_POS]  = SizeAr;
}

// binary search

void bin_search(void **start, QString key, int &pos, bool &find, Cmpmethod func)
{
    int mid;
    int high, low;
    find = false;
    if((int*)start[COUNT_POS] == 0)
    {
        pos = 0;
        qDebug()<<"zero elem\n";
        return;
    }
    low = 0;
    high = ((int*)start)[COUNT_POS] - 1;
    do
    {
        mid = (high + low) / 2;
        int result = func(start[mid], key);
        if(result == 0)
        {
            pos = mid;
            find = true;
            return;
        }
        else if(result == 1)
        {
            high = mid - 1;
        }
        else
        {
            low = mid + 1;
        }
    }while(low <= high);

    pos = low;
}

// function for adding  in any level
void addtosort(void **&start, void *pnew, int pos)
{
    int count = ((int*)start)[COUNT_POS];
    int size = ((int*)start)[SIZE_POS];

    if(count == size)
    {
        ExpandArrPtr(start, size, count);
    }
    if(pos == count)
    {
        start[pos] = pnew;
    }
    else
    {
        for(int i = count;i >= pos;i--)
        {
            start[i+1] = start[i];
        }
        start[pos] = pnew;
    }

    count++;
    ((int*)start)[COUNT_POS] = count;
}

void add_goods(void **&Start, goods * Pnew)
{
    int pos;
    bool find;

    bin_search((((shop*)(Start))->sublevel), Pnew->name, pos, find, compare_goods);
    addtosort((((shop*)(Start))->sublevel), Pnew, pos);
}
// finding the item  in second level to add

void find_place(QString key)
{
    int pos;
    bool find;
    int count = ((int*)Start)[COUNT_POS];
    for(int i = 0;i < count;i++)
    {
        bin_search(((net*)(Start)[i])->sublevel, key, pos, find, compare_shop);
        if(find)
        {

            goods * Pnew = new goods;
            Pnew->date = "foo"
            Pnew->name = "bar"
            add_goods(((net*)(Start)[pos])->sublevel, Pnew);
            break;
        }
    }
}

什么会导致这样的问题?

0 个答案:

没有答案