Allocate structure and memory for elements in one malloc

I am sure this is the main question, but I could not determine if this is a legitimate memory allocation strategy or not. I am reading data from a file and I am filling out the structure. The size of the members is variable on every read, so my structural elements are type pointers

struct data_channel{
    char *chan_name;
    char *chan_type;
    char *chan_units;
};

So, before reading, I will find out that the size of each line is so that I can allocate memory for them, my question is: can I allocate memory for the structure and lines in one malloc, and then fill the pointer in?

Say that the size of chan_name is 9, chan_type 10 and chan_units 5. Therefore, I would highlight and do something like this.

struct data_channel *chan;

chan = malloc(sizeof(struct data_channel) + 9 + 10 + 5);
chan->chan_name = chan[1];
chan->chan_type = chan->chan_name + 9;
chan->chan_units = chan->chan_type + 10;

, , , . , , . , 7 , 100 . , , 700 , 800. . , . , , , ?

+2
4

chan_name - 8- , chan_type - 9- , chan_units - 4 , , , chan_name.

( ), . , , .

+5

. , , ; , .

struct data_channel
{
    char *chan_name;
    char *chan_type;
    char *chan_units;
};

struct data_channel *chan;
size_t name_size = 9;
size_t type_size = 10;
size_t unit_size = 5;

chan = malloc(sizeof(struct data_channel) + name_size + type_size + unit_size);
if (chan != 0)
{
    chan->chan_name  = (char *)chan + sizeof(*chan);
    chan->chan_type  = chan->chan_name + name_size;
    chan->chan_units = chan->chan_type + type_size;
}

- , . , .

, int, , . .

struct data_info
{
    char *info_name;
    int  *info_freq;
    char *info_unit;
};

size_t name_size = 9;
size_t freq_size = 10;
size_t unit_size = 5;
size_t nbytes = sizeof(struct data_info) + name_size + freq_size * sizeof(int) + unit_size;
struct data_info *info = malloc(nbytes);

if (info != 0)
{
    info->info_freq = (int *)((char *)info + sizeof(*info));
    info->info_name = (char *)info->info_freq + freq_size * sizeof(int);
    info->info_unit = info->info_name + name_size;
}

( int), . , . , .

C11 (_Alignof _Alignas <stdalign.h>, max_align_t <stddef.h>), ( , , , , , C, .

, , C99 "struct hack", (FAM). .

    struct data_info
    {
        char *info_name;
        char *info_units;
        int  info_freq[];
    };

    size_t name_size = 9;
    size_t freq_size = 10;
    size_t unit_size = 5;
    size_t nbytes = sizeof(struct data_info) + name_size + freq_size * sizeof(int) + unit_size;
    struct data_info *info = malloc(nbytes);

    if (info != 0)
    {
        info->info_name  = ((char *)info + sizeof(*info) + freq_size * sizeof(int));
        info->info_units = info->info_name + name_size;
    }

, FAM, info_freq. , .

, ( , ). , . , realloc(); , , .

: 64- , , , , .

struct data_channel
{
    char chan_name[16];
    char chan_type[16];
    char chan_units[8];
};

40 . 64- 24 24 (9 + 10 + 5) , 48 .

+3

, , , , . :

#include <stddef.h>
#include <stdlib.h>

struct StWithArray
{
    int blahblah;
    float arr[1];
};
struct StWithArray * AllocWithArray(size_t nb)
{
    size_t size = nb*sizeof(float) + offsetof(structStWithArray, arr);
    return malloc(size);
}

.

:

#include <stddef.h>
#include <stdlib.h>

struct data_channel
{
    char *chan_name;
    char *chan_type;
    char *chan_units;

    char actualCharArray[1];
};

struct data_channel * AllocDataChannel(size_t nb)
{
    size_t size = nb*sizeof(char) + offsetof(data_channel, actualCharArray);
    return malloc(size);
}
struct data_channel * CreateDataChannel(size_t length1, size_t length2, size_t length3)
{
    struct data_channel * pt = AllocDataChannel(length1 + length2 + length3);
    if(pt != NULL)
    {
        pt->chan_name = &pt->actualCharArray[0];
        pt->chan_type = &pt->actualCharArray[length1];
        pt->chan_name = &pt->actualCharArray[length1+length2];
    }
    return pt;
}
+1

. , , .

mallocs frees , , , .. Valgrind . .

, mallocs , mallocs malloc, .

The last thing you need to consider is how often you call mallocs. If this is often the case, the cost of several mullocks can be expensive.

0
source

All Articles