whoisthefirst 2018-10-11 14:13
浏览 400

C语言编辑矩阵运算,数据溢出问题

if (td == 0){
float c1 = 2;
float c2 = 2;
float MaxDT = 20;
uint16_t N = 40;//Initialize the individuals in the tribe
uint16_t Nu = 10;
int D = Nu * 2;//Search Space dimension
int stop_index = 0;

    /*v*/
    rand_1 = rand(rand_1);
    rand_result = rand_1 / data2;
    float v_rand1 = -5 + 10 * rand_result;
    //int column1 = Nu * 2;
    Matrix_t mat_v1 = ones(N, Nu * 2);
........

问题出在最后一句mat_v1处,断点运行在倒数第二句mat_v1行列数值都特别大,运行到最后一句就数据溢出了

附上引用ones函数代码:
Matrix_t ones(uint16_t m, uint16_t n){
if (m <= 0 || n <= 0){
    exit(1);
}
Matrix_t mat = create_mat(m, n);
uint16_t i, j;
for (i = 0; i < n; i++){
    for (j = 0; j < m; j++){
        mat.data[i][j] = 1;
    }
}
return mat;
}
附上引用create函数代码:
Matrix_t create_mat(uint16_t row, uint16_t column){
Matrix_t mat;
if (row <= 0||column<=0)
{
    //printf("error, in create_mat: row <= 0||column<=0\n");
    exit(1);
}
if (row > 0 && column > 0)
{
    mat.row = row;
    mat.column = column;
    mat.data = (float **)malloc(row*sizeof(float *));//先指针的指针
    if (mat.data == NULL)
    {
        //printf("error, in create_mat: mat.data==NULL");
        exit(1);
    }
    uint16_t i;
    for (i = 0; i < row; i++)
    {
        *(mat.data + i) = (float *)malloc(column*sizeof(float));//再分配每行的指针
        if (mat.data[i] == NULL)
        {
          //printf("error, in create_mat: mat.data==NULL");
          exit(1);
        }
    }
    clear_mat(&mat);
}
return mat;

}
新人,没有悬赏。。。望路过大神相助!

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示