0

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]);
        }
    }
0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Nabla. The labels[0] is the first row of my two dimenstional array labels[][], so labels[0] is of type int * for an array of size column_num.
@TonyGW Correct, but then you also take the address of it, so it becomes int**.
or should I just use this: current_container->linked_to_container[0][j] = allcontainers[next_container].labels[0];
@TonyGW Then at least the types would match, I don't know what you are trying to archive with the code, so maybe that's correct.
the field linked_to_container is a two-dimenstional array of int pointers, which points to another container's label.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.