-1

I am using the CUDD package for BDDs manipulation. I want to make a copy for a big data structure in it that is called the DdManager. The problem is : this data structure has so many pointers inside it , so when I make a direct copy it is a "shallow" copy(as some use the term) i.e. : the pointers in the new copy point to the same places pointed to by the original copy , so when I change in anyone of them I also change in the other which is undesirable .... Trying to make a copy function by hand is not feasible because the data structure is really big and very detailed with many pointer to other complex structures also !!! I have tried the vector solutions described here but I did not get the expected result because there are many nested structures and pointers and I want a completely new copy.

Here is a code sample of what I want to do :

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <vector>
using namespace std;
struct n1
{
    int a;
    char *b;
};
struct n2
{
    int **c;
    struct n1 *xyz;
};
typedef struct
{
    vector<struct n2> x;
}X;
int main()
{
    struct n2 s1;
    s1.xyz = (struct n1*)malloc(sizeof(struct n1));
    s1.xyz->a = 3;
    s1.xyz->b = (char*)malloc(5);
    s1.xyz->b[0] = '\0';
    strcat(s1.xyz->b,"Mina");
    s1.c = (int**)malloc(5 * sizeof(int*));
    for(int i = 0; i < 5; i++)
        s1.c[i] = (int*)malloc(5 * sizeof(int));
    for(int i = 0; i < 5; i++)
        for(int j = 0 ; j < 5 ; j++)
            s1.c[i][j] = i + j;
    X struct1,struct2;
    vector<struct n2>::iterator it;
    it = struct1.x.begin();
    it = struct1.x.insert(it,s1);
    it = struct2.x.begin();
    it = struct2.x.insert(it,struct1.x[0]);
    cout<<"struct2.x[0].c[1][2]  = "<<struct2.x[0].c[1][2] <<" !"<<endl; // This is equal to 3
    (struct2.x[0].c[1][2])++; //Now it becomes 4
    cout<<"struct2.x[0].c[1][2]  = "<<struct2.x[0].c[2][2] <<" !"<<endl; //This will print 4
    cout<<"s1.c[1][2]  "<< s1.c[1][2]<<" !"<<endl; // This will also print 4 ... that's the wrong thing 
    return 0;
}
4

2 回答 2

3

尽管其他人说你必须

手工制作复印功能

...要解决这个问题,我认为这对你来说是错误的方法。这就是为什么,这里有一个建议。

您正在尝试创建CUDD ddManager 对象的副本,该对象是复杂 CUDD 库的组成部分。CUDD 在内部使用某些对象的引用计数(这可能对您有所帮助......),但 ddManager 对象有效地代表了库的整个实例,我不知道引用计数如何跨实例工作。

CUDD 库及其相关的 C++ 包装器似乎没有提供必要的复制构造函数来创建 ddManager 的单独副本,并且添加这些可能需要付出巨大的努力,以及您尝试使用的库的详细内部知识作为客户。虽然可以做到这一点,但这是一件复杂的事情。

相反,我会考虑尝试将当前 BDD 写入文件/流/任何内容,然后将其读回 ddManager 的新实例。有一个名为dddmp的库可以帮助您解决这个问题。

我还建议修改 C++ 包装器以使 ddManager 类不可复制

于 2012-07-03T10:00:22.233 回答
1

“尝试手工制作复制功能是不可行的,因为数据结构非常大而且非常详细,还有许多指向其他复杂结构的指针!!!”

这正是你必须做的。客观的方法意味着您不会编写一个大的全能复制方法。相反,每个对象(结构)只复制它自己,然后调用它的子对象复制方法等,直到没有任何东西可以复制。

没有“向量解决方案”之类的东西,向量只是具有最小复制方法的最小对象。

struct和class没有区别,写copy方法就好了。

只有你知道你的数据的结构,所以你是唯一可以拯救人类(或复制这些数据)的人。

于 2012-07-03T09:37:26.667 回答