0

I don't know the right order to use malloc in my case.

Structures:

typedef struct buffer_char
{
    size_t size;
    char *p;
} buffer_char;

typedef struct buffer_buffer_char
{
    size_t size;
    buffer_char *p;
} buffer_buffer_char;

typedef struct LPWA_COMMAND
{
    LPWA_COMMAND_TYPE type;
    buffer_char text;
    buffer_buffer_char writeargs;
    buffer_buffer_char readargs;

} LPWA_COMMAND;

typedef struct buffer_LPWA_COMMAND
{
    size_t size;
    LPWA_COMMAND *p;

} buffer_LPWA_COMMAND;

And now I want to malloc in init(*buffer_LPWA_COMMAND):

buffer_LPWA_COMMAND test;
init(&test);

It is enough for me to know how to malloc buffer_buffer_char, but I wanted to share all of the code.

Would this be correct?:

void buffer_char_init(buffer_char *buf)
{
    buf->p = (char*)malloc(sizeof(char));
    buf->size = 0;
}

void buffer_buffer_char_init(buffer_buffer_char *buf)
{
    buf->p = (buffer_char*)malloc(sizeof(buffer_char));
    buffer_char_init(&buf->p[0]);
    buf->size = 0;
}

Thank you!

Terror404
  • 51
  • 6
  • It is unclear how this structure is used. Just initializing everything to 0 should be sufficient, but it really depends on what assumptions the rest of the code makes regarding the structure contents. – chqrlie Apr 01 '20 at 12:46
  • @chqrlieforyellowblockquotes every pointer that needs malloc in this case, gets realloc later. Just simple append functions. – Terror404 Apr 01 '20 at 12:57
  • 1
    @PaulOgilvie It does not give a compilation error. I'm pretty sure, that &buf->p[0] is the same as buffer_char *buf, because buf->p[0] is buffer_char. That's the whole reason for doing it this way. It should represent something like: string[]; – Terror404 Apr 01 '20 at 13:03
  • are you sending an array of `buffer_buffer_char` to function `void buffer_buffer_char_init(buffer_buffer_char *buf)`? – hanie Apr 01 '20 at 13:08
  • @hanie just a single structure of the type buffer_buffer_char like this: buffer_buffer_char foo; buffer_buffer_char_init(&foo); – Terror404 Apr 01 '20 at 13:12
  • @Terror404, `&buf->p[0]` is not the same as `buffer_char *buf`. `&buf->p[0]` is address of the value `buf->p[0]`. It's is a `reference`. `buffer_char *buf` is a pointer. They are not the same. – Hitokiri Apr 01 '20 at 13:21
  • @Hitokiri buffer_char_init(buffer_char *buf) is a call by pointer function, so i have to call it with the reference to have the pointer for use in the function. Or am I wrong? How would you call this function in my case? – Terror404 Apr 01 '20 at 13:29
  • @Terror404: if an empty array (size == 0) can have a nullpointer, just initializing the whole structure to 0 should suffice. – chqrlie Apr 01 '20 at 23:09

2 Answers2

0

As the comments above, &buf->p[0] is not the same as buffer_char *buf. &buf->p[0] is address of the value buf->p[0]. It's is a reference. buffer_char *buf is a pointer.

The code below may be help you have idea to initialize your structs:

void init_buffer_char(buffer_char * buf_c, int length) {
    // Allocate the pointer "buf_c"
    buf_c = malloc(sizeof(buffer_char));
    // Allocate the pointer that points to character
    buf_c->p = malloc(sizeof(char) * length);
    buf_c->size = 0;
}

void init_buffer_buffer_char (buffer_buffer_char * buf_buf_c) {
    buf_buf_c = malloc(sizeof(buffer_buffer_char));
    init_buffer_char(buf_buf_c->p);
    buf_buf_c->size = 0;
}

See the difference between pointer and reference here

Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • 1
    Those functions don't seem to do anything apart from leaking memory. Perhaps you meant the function arguments to be pointers to pointers to structs? – Ian Abbott Apr 01 '20 at 13:39
  • Yup. I just write a simple example to allocate his structs. The argument of each function is the pointer that points to the struct. Of course, he can return a pointer or using others functions for allocating the memory – Hitokiri Apr 01 '20 at 13:45
0

since you mentioned you are just sending a single structure of the type buffer_buffer_char to function void buffer_buffer_char_init(buffer_buffer_char *buf) using &buf->p[0] is not making sense ,when you only allocate memory for one buffer_char (not an array) here buf->p = (buffer_char*)malloc(sizeof(buffer_char));.

also in void buffer_char_init you are are allocating memory only for one char ,not for a string.

void buffer_char_init(buffer_char * buf)
{
    buf->p = (char*)malloc(sizeof(char)*(sting length);
    buf->size = 0;
}

void buffer_buffer_char_init(buffer_buffer_char * buf)
{
    buf->p = (buffer_char*)malloc(sizeof(buffer_char));
    buffer_char_init(buf->p);
    buf->size = 0;
} 
hanie
  • 1,863
  • 3
  • 9
  • 19