Should I allocate the member of the date structure as well?

Asked

Viewed 82 times

2

Assuming the following structure:

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

Let’s say I want to keep one Lnode in the heap:

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

I must now also use the malloc for the date member?

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

or this already happens automatically when I allocate a Lnode?

3 answers

1


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

In this case both next how much data must have an allocation before.

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

  • One is the operator’s use & that picks up the address that something something. The most common is to use with things that are in the stack.
  • The other is the use of an allocator like the malloc() that returns an address (a pointer.

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

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

It doesn’t work, or at least it doesn’t do what you think it does. It’s a flexibility to have the type void *, but when allocating space to object pointed 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, that’s it, not allocating space for the data that should be stored.

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

  • Grateful for the response!

0

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

Each of these members is not initialized; that is, the value of each of the members cannot 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 point to something that already exists, you can put that address there without making new allocation.

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

If you want to point to an object that doesn’t exist yet, you have to make room for that object and put 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);

-1

Yes, how data is a pointer you must allocate memory to it separately after you have allocated Lnode.

When you allocate memory to Lnode will be for the structure, that is, memory for the whole id and for two pointers next and data, so if you want to store something in those last two you have to allocate memory to them separately because, repeating, the allocation of Lnode allocates memory to your pointers and not to the space they need to store data.

  • Grateful for the response!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.