神秘地改变结构成员(涉及的指针):

时间:2013-03-06 08:39:46

标签: c pointers random memory-leaks structure

好的,所以我会开始说我对编程很陌生,不要介意这个论坛,所以如果有更多/更少的信息,我应该让我知道。我希望我不是在问一个愚蠢的问题:P

无论如何,我正在尝试编写我的第一个自己的工作程序(除了一个简单的2d数组程序作为乘法程序)。在其中我基本上试图建立一个简单的博弈论模型。你有两个人可以做出选择,这是预先确定的,我将其表示为“int trait”,即0或1.但是我将其扩展到最初有100个人,并且一次选择2个人进行随机测试,根据他们的特点,他们会分配不同的分数来确定他们有多少“后代”需要进行进一步的测试,但这并不完全重要,因为我的问题是在此之前。

我通过制作结构来设置我的测试科目:

struct Person { //used for testing and reproduction initialization
    int trait; // 0 for normal - 1 for phycopath
    int score; // reproductive score
    } ;
struct Person *id;

然后像这样分配内存: id = (struct Person *) malloc(n * sizeof(struct Person));

并初始化结构:

for(i = 0; i < nn; i++) {
    id[i * sizeof(struct Person)].trait = 0;
    id[i * sizeof(struct Person)].score = 0;
}

for(i = nn; i < n; i++) {
    id[i * sizeof(struct Person)].trait = 1;
    id[i * sizeof(struct Person)].score = 0; }

在你说我做错了之前(我并不是说我不是),只是一些思考的问题,我检查它之后立即正确初始化:

for(i = 0; i < n; i++) {
        printf("id[%d].trait = %d\t", i, id[i * sizeof(struct Person)].trait);
        printf("id[%d].score = %d\n", i, id[i * sizeof(struct Person)].score);
    } 

现在我的问题是在我的代码中的某个地方(我已经做了一些测试向我指出问题的位置),测试对象的特征在我介于和包括26之间时始终被改变。和37(每次都是)。这告诉我,代码中的某个地方我的变量id []正在被玩弄,但我不记得它在初始化和测试之间。我的测试表明问题在于:

for(i = 0; i < nn; i++) {
        if(p > 0) {
        srand( time(NULL) );
        randn = rand() % p; //MAKE SURE THIS IS OK
        storea = randn;
        storeaa = randf[randn];
        testa = storeaa * sizeof(struct Person);
testa(以及稍后在程序中的testb)用于以与上述test-print语句完全相同的方式调用测试对象。<​​/ p>

程序编译并运行(只是不是我想要的方式),大约每十次我尝试运行它我得到一个错误:

  

断言`(old_top ==(((mbinptr)(((char *)&amp;((av) - &gt; bins [((1) - 1)*   2])) - __builtin_offsetof(struct malloc_chunk,fd))))&amp;&amp; old_size ==   0)|| ((unsigned long)(old_size)&gt; =(无符号长   long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *   (sizeof(size_t))) - 1))&amp; 〜((2 *(sizeof(size_t))) - 1)))&amp;&amp;   ((old_top) - &gt; size&amp; 0x1)&amp;&amp; ((unsigned long)old_end&amp; pagemask)== 0)'   失败。

这对我来说,随机数发生器可能是错误的?或内存泄漏 - 一些在哪里?说实话,我真的很无能为力。

我很抱歉被抽出来,希望不要太困惑的解释。我已经在这个问题上工作了2天,花了很多时间在网上进行研究,以免误解我的问题。任何出错的想法都将受到高度赞赏。为了完成这个问题,我将在下面发布我的非常草率的代码:

#include <stdio.h>
#include <stdlib.h> 
#include <assert.h>
#include <time.h>

struct Person { //used for testing and reproduction initialization
    int trait; // 0 for normal - 1 for phycopath
    int score; // reproductive score
    } ;
struct Person *id;
int n; // number of individuals


struct Person *temp;

//remember to type functions before main!!!
int main () {
    //beginning of initialization
    int n = 100; // number of members MUST BE EVEN!!!!!!
    int nn = (n / 2);
    printf("n = %d\nnn = %d\n", n, nn); 
    if(n == 100) { printf("n set to %d \n", n); }
    id = (struct Person *) malloc(n * sizeof(struct Person));//allocate memory for id
    temp = (struct Person *) malloc(n * sizeof(struct Person));
    assert(id != NULL);
    assert(temp != NULL);   
    int i = 0;
    int testa;
    int testb; //two subjects to be used for test
    //initialize rand-function array
    int p = n; //used for decrementing array and random size
    int *randf;
    int *randt;
    int randn;
    int t;
    int r;
    int kids;
    int offs;
    int tests = 0;
    int testkids = 0;
    int storea;
    int storeaa;
    int storeb;
    int storebb;
    int size = sizeof(struct Person);
    randf = malloc( n * sizeof(int));
    randt = malloc( p * sizeof(int));
    randf = realloc(randf, n * sizeof(int)); //allocation to randf
    assert(randf != NULL);
    assert(randt != NULL);

    for(i = 0; i < nn; i++) {
        printf("initialization of non-traits begun\n");
        id[i * sizeof(struct Person)].trait = 0;
        id[i * sizeof(struct Person)].score = 0;
        printf("Person %d set\n", i);
    }

    for(i = nn; i < n; i++) {
        printf("initialization of with-traits begun\n");
        id[i * sizeof(struct Person)].trait = 1;
        id[i * sizeof(struct Person)].score = 0;
        printf("Person %d set\n", i);
    } //initialization complete
    for(i = 0; i < n; i++) {
        printf("id[%d].trait = %d\t", i, id[i * sizeof(struct Person)].trait);
        printf("id[%d].score = %d\n", i, id[i * sizeof(struct Person)].score);
    }
    //beginning of test
    i = 0;


    for(i = 0; i < n; i++) {
        randf[i] = i;
    }
    //selection and usage of test subjects
    i = 0;
    t = 0;
    r = 0;
    for(i = 0; i < nn; i++) {
        if(p > 0) {
        srand( time(NULL) );
        randn = rand() % p; //MAKE SURE THIS IS OK
        storea = randn;
        storeaa = randf[randn];
        testa = storeaa * size;
        //printf("%d\n", id[testa].trait);
        p = p - 1;
        randt = realloc(randt, p * sizeof(int));
        assert(randt != NULL);
        for(t = 0, r = 0; t < p; t++) { //copy randf over to randt exluding t = randn
            if(t != randn) {
                randt[r] = randf[t];
                r++;
            }
            else {}
        }
        randf = realloc(randf, p * sizeof(int));
        assert(randf != NULL);  
//              randf = randt;//copy randt back over to randf               
                for (t=0; t<p; t++) {
                   randf[t] = randt[t]; }
        srand( time(NULL) );
        randn = rand() % p; //MAKE SURE THIS IS RIGHT
        storeb = randn;
        storebb = randf[randn];
        testb = storebb * size;
        //printf("%d\n", id[testb].trait);
        p = p - 1;
        if( p > 0 ) { 
        randt = realloc(randt, p * sizeof(int));
        assert(randt != NULL);
            for(t = 0, r = 0; t < p; t++) { //same as before
                if(t != randn) {
                    randt[r] = randf[t];
                    r++;
                }
                else{}  
            }
            randf = realloc(randf, p * sizeof(int));
            assert(randf != NULL);
            for (t=0; t<p; t++) {
                       randf[t] = randt[t]; }
        }
        }       
        tests++;
    //TESTING TIME!!!
        if(id[testa].trait == 0 && id[testb].trait == 0) {
            id[testa].score = id[testa].score + 2;
            id[testb].score = id[testb].score + 2;
            //printf("Test Successful %d\n", tests);
        } else if(id[testa].trait == 0 && id[testb].trait == 1) {
            id[testa].score = id[testa].score + 1;
            id[testb].score = id[testb].score + 3;
            //printf("Test Successful %d\n", tests);
        } else if(id[testa].trait == 1 && id[testb].trait == 0) {
            id[testa].score = id[testa].score + 3;
            id[testb].score = id[testb].score + 1;
            //printf("Test Successful %d\n", tests);
        } else if(id[testa].trait == 1 && id[testb].trait == 1) {
            //printf("Test Successful %d\n", tests);
            // NO CHANGE
        }else {
            printf("ERROR: Test %d\n", tests);
            printf("id[testa].trait = %d\t%d,%d\n", id[testa].trait, storea, storeaa);
            printf("id[testb].trait = %d\t%d,%d\n", id[testb].trait, storeb, storebb);
        }

    } //END OF TESTING
//Commence Reproduction
    for(i = 0, kids = 0; i < n; i++) {
        kids = kids + id[i * sizeof(struct Person)].score;
        testkids ++;
        //printf("%d kids calculated\n %d parents completed\n", kids, testkids);
    }
    temp = realloc(temp, kids * sizeof(struct Person));//REMEMBER TO FREE
    assert(temp != NULL);   
    r = 0;  
    for(i = 0; i < n; i++) {
        for(offs = id[i * sizeof(struct Person)].score; offs > 0; offs--) {
            temp[r * sizeof(struct Person)].trait = id[i * sizeof(struct Person)].trait;
            temp[r * sizeof(struct Person)].score = 0;
            r++;
        }
    }
    n = kids;
    id = realloc(id, kids * sizeof(struct Person));
    assert(id != NULL);
    for(i = 0; i < n; i++) {
    id[i * sizeof(struct Person)].trait = temp[i * sizeof(struct Person)].trait;
    id[i * sizeof(struct Person)].score = temp[i * sizeof(struct Person)].score;
    }
    printf("Done!");
    free(randf);
    free(randt);
    free(temp);
    free(id);   
    return 0;
}

1 个答案:

答案 0 :(得分:4)

您使用

  

id [i * sizeof(struct Person)]

没有意义。

它应该是

id[i]