Should I allocate the structure member date as well?

2

Assuming the following structure:

typedef struct lnode{
    struct lnode *next;
    void *data;
    int id;
}Lnode;

Let's say I want to store a Lnode in heap :

Lnode *exp = malloc(sizeof(*exp));

Should I now also use malloc for the date member?

exp->data = malloc(sizeof( void*)); ???

Does this happen automatically when I allocate a Lnode ?

    
asked by anonymous 01.02.2016 / 17:36

3 answers

1

Yes, you should. Every object pointed to by a pointer needs to be allocated somewhere. It can even be allocated in stack , but this is rare and would not work in this case. Then every object that will be pointed to by a variable, even if it is inside a structure, will be placed in the heap , you should have memory allocated with malloc() or some substitute for it.

In this case both next , and data must have an allocation before.

Since they are pointers, are they pointing to where? How can you get a pointer? There are two basic forms (there are others of course):

  • One is the use of the & operator that takes the address from something. The most common is to use with things that are in stack .
  • The other is the use of an allocator such as malloc() that returns an address (a pointer.

Automatic allocation in C only when stack , and only for the element in stack , obviously not for possible notes inside it.

exp->data = malloc(sizeof( void*));

This does not work, or at least does not do what you think. It is a flexibility to have type void * , but when allocating the space for an object pointed to by that pointer, you have to know the actual size of this object. This case is allocating space for a pointer, which will probably be 4 or 8 bytes depending on the architecture. Just this. You are not allocating space for the data that should be stored.

What is the size of the object that goes in data ? 50 bytes? Allocate 50 bytes. Is the size of 10 integers? 10 * sizeof(int) . And so on.

    
01.02.2016 / 17:52
0

When you allocate space for an object of type struct lnode you are allocating space for all its members (two pointers and one integer).

Each of these members is not initialized; that is, the value of each member can not be used. But you can assign a value to each member .

struct lnode *exp = malloc(sizeof(*exp));
exp->next = NULL; /* OK */
exp->data = NULL; /* OK */
exp->id = 42;     /* OK */

If you want the member data to point to something that already exists, you can enter that address without making a new allocation.

double foobar = 3.14159;
exp->data = &foobar;

If you want to point to an object that does not yet exist, you have to allocate space for that object and enter the address in data .

exp->data = malloc(sizeof (double));
if (!exp->data) { fprintf(stderr, "Sem memória.\n"); exit(EXIT_FAILURE); }
*(double*)exp->data = 3.14159; // evita ponteiros para void!
// ...
free(exp->data);
    
24.08.2016 / 16:26
-1

Yes, since data is a pointer you should allocate memory for it separately after you have allocated Lnode .

When you allocate memory for Lnode it will be for the structure , ie memory for the integer id and for two pointers next and data , so if you want storing something in the latter two will have to allocate memory for them separately because, by repeating, the Lnode allocation allocates memory to its pointers and not to the space they need to store data.

    
01.02.2016 / 17:52