简体   繁体   English

C动态结构的动态数组

[英]C dynamic array of dynamic struct

I am trying to create a dynamic array of dynamic structs but I can't get it working properly. 我正在尝试创建动态结构的动态数组,但无法正常工作。 It prints all the info but it gives return error. 它会打印所有信息,但会返回错误。 If I comment the line containing: 如果我评论包含以下内容的行:

printf(" m[%i][%i] ID: %i VALUE: %f\n", i, j, m[i]->id, m[i]->values[j]);

It compiles OK and returns 0. What am I doing wrong? 它编译确定并返回0。我在做什么错? I am just beginning learing C and in the process of dealing with pointers. 我刚刚开始讲授C,并且正在处理指针。

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

typedef struct listDef{
   int id;
   float *values;
} ListSt;


int main()
{
    int max_fil, fil, col;
    max_fil = 4; /* Max 'ListSt' elements*/
    fil=2; /* Rows  */
    col=4; /* Columns */
    ListSt **m = NULL;
    int count = 0;
    int sizes[] = {4,6,8,10}; /* The sizes of each 4 elements to be created */
    m = (ListSt **)malloc(sizeof(int*)*max_fil); /* Assign a memory address for accesing 'm' (ListSt) */
    for(int i=0;i<fil;i++){
        m[i]->values = (float *)malloc(sizeof(float)*sizes[i]);
        m[i]->id = i;
        printf("-----------\n");
        printf("Element n.%i :\n\n", i);
        for(int j=0;j<sizes[i];j++){
            m[i]->values[j] = 0.1234*(i+1);
            /* If I comment the next line, it compiles OK. */
            printf(" m[%i][%i] ID: %i VALUE: %f\n", i, j, m[i]->id, m[i]->values[j]);

            int testint;
            float testfloat;
            testint = m[i]->id;
            testfloat = m[i]->values[j];
        }
    }

    free(m);
    return 0;
}
  1. m should be of type "pointer to ListSt ", not "pointer to pointer to ListSt ". m的类型应为“指向ListSt指针”,而不是“指向ListSt指针的指针”。

  2. You are allocating memory for an array of pointers: 您正在为指针数组分配内存:

    m = (ListSt **)malloc(sizeof(int*)*max_fil);

    ... but the elements the array remain uninitialized. ...但是数组中的元素仍未初始化。

    And by the way, that should be 顺便说一句

    sizeof (ListSt *) * (sizeof (sizes) / sizeof (* sizes))
    ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    size of an element number of elements in array "sizes"

  3. You then go on and use the value of m[0] , but you have never assigned a value to m[0] . 然后,继续使用m[0]的值,但从未将值分配给 m[0] This is called undefined behavior ; 这被称为未定义行为 at this point the program is free to do whatever it pleases, for example crashing with segmentation fault. 在这一点上,程序可以随意做任何事情,例如由于分段错误而崩溃。

    More specifically, you say m[i]->values = , but m[i] is an uninitialized variable, so m[i]-> dereferences an uninitialized pointer. 更具体地说,您说m[i]->values = ,但是m[i]是未初始化的变量,因此m[i]->取消引用了未初始化的指针。

OK. 好。 I think I have it. 我想我有。 If someone, please, could maybe check it, it would be great to have some comments about the possible errors or future problems, or just some improvements. 如果有人请检查一下,那么对可能的错误或将来的问题发表一些评论,或者只是进行一些改进,将是很棒的。

Here's the (modified) working code: 这是(修改后的)工作代码:

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

typedef struct listDef{
   int id;
   float *values;
} ListSt;


int main()
{
    int max_fil, fil, col;
    max_fil = 4; /* Max 'ListSt' elements*/
    fil=4; /* Elements  */
    ListSt *m = NULL;
    int count = 0;
    int sizes[] = {4,6,8,10,12,13}; /* The sizes of each 4 elements to be created */
    m = (ListSt *)malloc(sizeof (ListSt) * fil); /* Assign a memory address for accesing 'm' (ListSt) */

    for(int i=0;i<fil;i++){
        m[i].values = (float *)malloc(sizeof(float)*sizes[i]);
        printf("-----------\n");
        printf("Element n.%i :\n\n", i);
        for(int j=0;j<sizes[i];j++){
            m[i].id = i;
            m[i].values[j] = 0.0001*(i+1)*(j+1);
            printf(" m[%i][%i] id: [%i] val: %f\n", i, j, m[i].id, m[i].values[j]);
        }
    }

    free(m);
    return 0;
}

And it prints: 它打印:

-----------
Element n.0 :

 m[0][0] id: [0] val: 0.000100
 m[0][1] id: [0] val: 0.000200
 m[0][2] id: [0] val: 0.000300
 m[0][3] id: [0] val: 0.000400
-----------
Element n.1 :

 m[1][0] id: [1] val: 0.000200
 m[1][1] id: [1] val: 0.000400
 m[1][2] id: [1] val: 0.000600
 m[1][3] id: [1] val: 0.000800
 m[1][4] id: [1] val: 0.001000
 m[1][5] id: [1] val: 0.001200
-----------
Element n.2 :

 m[2][0] id: [2] val: 0.000300
 m[2][1] id: [2] val: 0.000600
 m[2][2] id: [2] val: 0.000900
 m[2][3] id: [2] val: 0.001200
 m[2][4] id: [2] val: 0.001500
 m[2][5] id: [2] val: 0.001800
 m[2][6] id: [2] val: 0.002100
 m[2][7] id: [2] val: 0.002400
-----------
Element n.3 :

 m[3][0] id: [3] val: 0.000400
 m[3][1] id: [3] val: 0.000800
 m[3][2] id: [3] val: 0.001200
 m[3][3] id: [3] val: 0.001600
 m[3][4] id: [3] val: 0.002000
 m[3][5] id: [3] val: 0.002400
 m[3][6] id: [3] val: 0.002800
 m[3][7] id: [3] val: 0.003200
 m[3][8] id: [3] val: 0.003600
 m[3][9] id: [3] val: 0.004000

Process returned 0 (0x0)   execution time : 0.106 s
Press any key to continue.

I have changed the line: 我改变了这一行:

return *m;

with: 有:

return m[i];

And it works as expected! 它按预期工作!

The final working code, for if it can help someone: 最终的工作代码,如果它可以帮助某人:

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

int max_fil, count;
typedef struct listDef{
   float *values;
   int id;
   int nElements;

} ListSt;



ListSt ListSt_init(ListSt *m, int i, int s){
    m[i].values = (float *)malloc(sizeof(float)*s);
    printf("-----------\n");
    printf("CREATE Element n.%i :\n\n", i);
    for(int j=0;j<s;j++){
        m[i].id = i;
        m[i].nElements = s;
        m[i].values[j] = 0.0000 + count*(0.1);
        printf("m[%i][%i]  ID: %i  TotalElements: %i  Value[%i]: %f\n", i, j, m[i].id, m[i].nElements, j, m[i].values[j]);
        count++;
    }

    return m[i];
}


int main()
{
    max_fil = 100; /* Max 'ListSt' elements addresses to be created */
    count = 0; /* Just a global counter for each 'values' */
    int sizes[] = {4,8,4}; /* The sizes of each 4 elements to be created */
    ListSt *m = NULL;
    m = (ListSt *)malloc(sizeof (ListSt) * max_fil); /* Assign a memory address for accesing 'm' (ListSt) */

    /* Lets create three lists */
    m[0] = ListSt_init(m, 0, sizes[0]);
    m[1] = ListSt_init(m, 1, sizes[1]);
    m[2] = ListSt_init(m, 2, sizes[2]);

    printf("\n-----------\n");
    printf("PRINT RESULT :\n");
    printf("-----------\n\n");
    /* Now we show the elements.... */
    for(int i=0;i<3;i++){
        printf("RESULT Element n.%i :\n\n", i);
        for(int j=0;j<sizes[i];j++){
            printf("m[%i][%i]  ID: %i  TotalElements: %i  Value[%i]: %f\n", i, j, m[i].id, m[i].nElements, j, m[i].values[j]);
        }
    }
    free(m);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM