Aborted Core只用大字符串转储

时间:2017-02-16 13:17:54

标签: c crystal-reports

所以这个程序崩溃并告诉我" Aborted(core dumped)"但只有当我对'生成'#34;大于6 ...我知道我已经上传了整个代码的痛苦但我真的无法弄清楚它是从#34; fibonacci_quasicrystal_generator(GENERATIONS, crystal);",作为刚打印后的printf语句,然后出现消息。代码如下:

#define GENERATIONS 5
#define OUTFILE "frequencies.txt"
#define GNUPLOT_EXE "gnuplot"
#define GNUPLOT_SCRIPT "frequencyplot.script"

static void fibonacci_quasicrystal_generator(int generations, char * chain);
static int plot();

int main()
{
    double k = 1.0, m_a = 100.0, m_b = 1.0, m = 0.0;
    char * crystal = malloc(2);
    //strcopy(crystal, "A");    //gsl_vector * y_vector = gsl_vector_calloc(CHAIN_LENGTH);
    fibonacci_quasicrystal_generator(GENERATIONS, crystal);
    if (crystal == NULL){
        printf("Crystal write failed.");
        exit(0);
    }
    int chain_length = strlen(crystal);
    printf("%i member Crystal generated, after %i generations.\n", chain_length, GENERATIONS);
    gsl_matrix * a_matrix = gsl_matrix_calloc(chain_length, chain_length);
    gsl_matrix * b_matrix = gsl_matrix_calloc(chain_length, chain_length);
    gsl_matrix_set_identity(b_matrix);
    gsl_vector * eigenvalues_vector = gsl_vector_calloc(chain_length);
    for (int i = 0; i < chain_length; ++i){
        if (crystal[i] == 'A'){
            m = m_a;
        } else {
            m = m_b;
        }
        for (int j = 0; j < chain_length; ++j){
            if ((i == j) && (i != 0 && i != chain_length)){
                gsl_matrix_set(a_matrix, i, j,(2*k)/m);
            }
            else if (i == j-1){
                gsl_matrix_set(a_matrix, i, j,(-1)*(k/m));
            }
            else if (i == j+1){
                gsl_matrix_set(a_matrix, i ,j, (-1)*(k/m));
            }
        }
    }
    gsl_eigen_gensymm_workspace * workspace = gsl_eigen_gensymm_alloc(chain_length);
    gsl_eigen_gensymm(a_matrix, b_matrix, eigenvalues_vector, workspace);
    gsl_eigen_gensymm_free(workspace);
    free(crystal);
    gsl_matrix_free(a_matrix);
    gsl_matrix_free(b_matrix);
    gsl_sort_vector(eigenvalues_vector);
    FILE * outfile = fopen(OUTFILE, "w");
    for (int i = 0; i < chain_length; ++i){
        fprintf(outfile, "%e \t%i \r\n", pow(gsl_vector_get(eigenvalues_vector, i),2), i);
    }
    fclose(outfile);
    gsl_vector_free(eigenvalues_vector);
    plot();
    return 0;
}

static void fibonacci_quasicrystal_generator(int generations, char * chain){
    printf("generating fibonacci quasicrystal...\n");
    int i;
    i = 0;
    char * chain_1 = malloc(2), * chain_2 = malloc(2), * tmp = malloc(2);
    strcpy(chain_1, "B");
    strcpy(chain_2, "A");
    size_t chain_1_size = strlen(chain_1) + 1, chain_2_size = strlen(chain_2) + 1;
    if (generations == 1){
        chain = realloc(chain, chain_1_size);
        snprintf(chain, chain_1_size, "%s", chain_1);
    }
    else if (generations == 2){
        chain = realloc(chain, chain_2_size);
        snprintf(chain, chain_2_size, "%s", chain_2);
    }
    else if (generations > 2){
        size_t chain_3_size = strlen(chain_1) + strlen(chain_2) + 1;
        char * chain_3 = malloc(chain_3_size);
        printf("%i\n", generations);
        for (i = 0; i < generations - 1; ++i){
            printf("%i\n", i);
            snprintf(chain_3, chain_3_size, "%s%s", chain_1, chain_2);
            chain_1_size = chain_2_size;
            chain_2_size = chain_3_size;
            if ((tmp = realloc(chain_1, chain_1_size)) != NULL){
                chain_1 = tmp;
            }
            if ((tmp = realloc(chain_2, chain_2_size)) != NULL){
                chain_2 = tmp;
            }
            snprintf(chain_1, chain_1_size, "%s", chain_2);
            snprintf(chain_2, chain_2_size, "%s", chain_3);
            if (i < generations - 2){
                chain_3_size = strlen(chain_1) + strlen(chain_2) + 1;
                if ((tmp = realloc(chain_3, chain_3_size)) != NULL){
                    chain_3 = tmp;
                } else {
                    printf("oops!\n");
                    exit(1);
                }
            }
        }
        chain = realloc(chain, chain_3_size);
        snprintf(chain, chain_3_size, "%s", chain_3);
        free(chain_3);
    }
    free(chain_1);
    free(chain_2);
}

static int plot(){
    char command[PATH_MAX];
    snprintf(command, sizeof(command), "%s %s", GNUPLOT_EXE, GNUPLOT_SCRIPT);
    system(command);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

问题是char *chain进入fibonacci_quasicrystal_generator函数具有局部作用域:该函数不修改main的crystal指针,因此指针保留2个字节。

您可以将功能更改为

static char *fibonacci_quasicrystal_generator(int generations, char * chain)
{
   // YOUR STUFF

   return chain;
}

使用

从main调用它
crystal = fibonacci_quasicrystal_generator(GENERATIONS, crystal);

你可以使用双指针实现相同的功能

static void ibonacci_quasicrystal_generator(int generations, char ** chain)