Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
randn=rand()%p//确保这是好的 storea=randn; storeaa=randf[randn]; 种皮=storeaa*大小; //printf(“%d\n”,id[testa].trait); p=p-1; randt=realloc(randt,p*sizeof(int)); 断言(randt!=NULL); 对于(t=0,r=0;t_C_Pointers_Random_Memory Leaks_Structure - Fatal编程技术网

randn=rand()%p//确保这是好的 storea=randn; storeaa=randf[randn]; 种皮=storeaa*大小; //printf(“%d\n”,id[testa].trait); p=p-1; randt=realloc(randt,p*sizeof(int)); 断言(randt!=NULL); 对于(t=0,r=0;t

randn=rand()%p//确保这是好的 storea=randn; storeaa=randf[randn]; 种皮=storeaa*大小; //printf(“%d\n”,id[testa].trait); p=p-1; randt=realloc(randt,p*sizeof(int)); 断言(randt!=NULL); 对于(t=0,r=0;t,c,pointers,random,memory-leaks,structure,C,Pointers,Random,Memory Leaks,Structure,您对 id[i*sizeof(结构人)] 没有道理 应该是的 id[i] 你使用 id[i*sizeof(结构人)] 没有道理 应该是的 id[i] 编译器知道什么id是指针,所以从id[]数组索引中去掉sizeof。它引用了索引n*sizeof(person)处的person,就像您编写它的方式一样。编译器知道id是指向什么,所以从id[]数组索引中去掉sizeof。它引用了索引n*sizeof(person)处的person,就像您编写它的方式一样。在这种情况下,我应该使用id=(str

您对

id[i*sizeof(结构人)]

没有道理

应该是的

id[i]
你使用

id[i*sizeof(结构人)]

没有道理

应该是的

id[i]

编译器知道什么id是指针,所以从id[]数组索引中去掉sizeof。它引用了索引n*sizeof(person)处的person,就像您编写它的方式一样。编译器知道id是指向什么,所以从id[]数组索引中去掉sizeof。它引用了索引n*sizeof(person)处的person,就像您编写它的方式一样。在这种情况下,我应该使用id=(struct person*)malloc(n)而不是id=(struct person*)malloc(n*sizeof(struct person)),我也不明白为什么我的print语句检查它的初始化是否正确?否。在使用malloc分配内存时,您必须提供
n*sizeof(struct Person)
的大小。malloc不知道内存中将存储什么类型的数据。但是,当您使用类型为
Person*
的变量
id
时,必须将其索引为
[i]
。编译器知道
Person
的大小,并访问数组中的下一个人。。简而言之,只有在malloc语句中才能使用n*sizeof(…)。其他地方不需要sizeof语句。好的,这确实有效,我应该不那么固执,以前也尝试过,尽管我仍然不明白为什么初始的print语句有效。它有效的原因是因为在分配和打印时使用了相同的索引。问题是您正在访问的内存位置不是由malloc语句分配的。在这种情况下,我应该使用id=(struct Person*)malloc(n)而不是id=(struct Person*)malloc(n*sizeof(struct Person))我也不明白为什么我的print语句检查初始化是否正确?不。在使用malloc分配内存时,必须提供
n*sizeof(struct Person)
的大小。malloc不知道内存中将存储什么类型的数据。但是,当您使用类型为
Person*
的变量
id
时,必须将其索引为
[i]
。编译器知道
Person
的大小,并访问数组中的下一个人。。简而言之,只有在malloc语句中才能使用n*sizeof(…)。其他地方不需要sizeof语句。好的,这确实有效,我应该不那么固执,以前也尝试过,尽管我仍然不明白为什么初始的print语句有效。它有效的原因是因为在分配和打印时使用了相同的索引。问题是您正在访问的内存位置不是由malloc语句分配的。
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);
    } 
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);
#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;
}
id[i]