Question

I have a structure called container that has two fields: labels and linked_to_containers; The field labels is designed to be a 2-dimensional array of int, and the field linked_to_containers is designed to be a 2-dimensional array of int pointers. On top of this, I also have an array of struct container that are dynamically created in the initiation program. I have the following code written down, but one thing I'm unsure about is the first malloc I used inside of the function container_init(). As the struct container still does not have its size initialized, is this the right way to do malloc for creating an array of struct container?

Please see my question in my comments in the code, and I will appreciate your feedback.

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

typedef struct container {
    int *labels[2];                  /* two-dimensional array of int */
    int **linked_to_container[2];    /* two-dimensional array of pointers to label */
} container;

int get_next_container_index(int current_container_index, int max_index)
{
    if (max_index - current_container_index >= 1)
    {
        return current_container_index + 1;
    }
    else 
        return 0;        /* elements at two ends are linked */
}

container *allcontainers;   /* an array for all containers */

void container_init(int num_containers, int column_num)
{
    /* is this right to malloc memory on the array of container when the struct size is still unknown?*/
    allcontainers = (container *) malloc(num_containers * sizeof(container));

    int i;
    for (i = 0; i < num_containers; i++)
    {
        container *current_container = &allcontainers[i];
        current_container->labels[0] = malloc(column_num * sizeof(int));
        current_container->labels[1] = malloc(column_num * sizeof(int));

        current_container->linked_to_container[0] = malloc(column_num * sizeof(int *));
        current_container->linked_to_container[1] = malloc(column_num * sizeof(int *));

        int j;
        for (j = 0; j < column_num; j++)
        {
            current_container->labels[0][j] = 0;     /* initialize all labels to 0 */
            current_container->labels[1][j] = 0;

            int next_container = get_next_container_index(i, num_containers - 1);     /* max index in all_containers[] is num_containers-1 */
            current_container->linked_to_container[0][j] = &(allcontainers[next_container]->labels[0]);
        }
    }

No correct solution

OTHER TIPS

The line in question seems perfectly fine to me, the size of struct container is well-known, because of its definition. The only size not known is the size of the arrays that the pointers in the struct will eventually point to, but that doesn't affect the size of the pointers themselves and thus also not the struct's size.

The only issue I see is here:

current_container->linked_to_container[0][j] = &(allcontainers[next_container]->labels[0]);

linked_to_container[0][j] is of type int*, but labels[0] is of type int* and therefore &(labels[0]) is of type int**. I am not sure what you try to accomplish here, but you probably need another index to labels[0][...] or & shouldn't be there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top