Why use malloc with a structure?

Why should I use malloc when the same work can be done without malloc, as shown below.

#include <stdio.h> #include <conio.h> struct node { int data; struct node *l; struct node *r; }; int main(){ //Case 1 struct node n1; n1.data = 99; printf("n1 data is %d\n", n1.data); //Case 2 struct node *n2 = (struct node *) malloc (sizeof(struct node)); n2 -> data = 4444; printf("n2 data is:%d\n",n2 -> data); free(n2); return (0); } 
  • It is difficult for me to understand how n1, which is not initialized in a memory cell, is capable of storing data (99).
  • when to use case 1 and when to use case 2.
+6
source share
5 answers

Why should I use malloc if the same work can be done without malloc as below ..

You use malloc to allocate memory on the heap, and without malloc you put the structure on the stack.

It is difficult for me to understand how n1, which is not initialized to a memory location, can store data (99).

Initialized or not, when you assign data n1.data = 99; It is saved.

2) when to use case 1 and when to use case 2

Case 1 is used when you know that you will use a structure object in a limited scope and will not refer to structure data beyond its scope.

Case 2 is used when you will use your structure in several places, and you are ready to manage it manually (and carefully!). The advantage of this method is that you create and initialize the structure in some part of the program area, and you create the pointer and pass the pointer, because passing a 4-byte pointer is much more efficient than passing the structure itself.

+9
source
 int main() { struct node n1; n1.data = 99 

This backup space on the stack (in the main frame) is equivalent to the size of the struct node . This is called local, and it exists only in the context of main .

 struct node *n2 = (struct node *) malloc (sizeof(struct node)); 

This is a heap distribution. This memory exists no matter what context you are in. This is commonly referred to as "dynamic allocation."

These node structures are the basis for a linked list that can add, remove, reorder nodes, etc. optional.

See also:

+3
source

In the first case, memory is allocated on the stack. When the variable n1 exhausted, the memory is freed.

In the second case, memory is allocated on the heap. You must explicitly free memory resources (as you do with free ).

The rule of thumb may be that you use memory allocated by the stack for local temporary data structures of a limited size (the stack is only part of the computer’s memory, it differs for each platform). Use heap for data structures that you want to keep or large.

Googling for stack and heap will give you much more information.

+2
source

Usually you use only malloc if you do not know the size of memory that you will need before starting the application.

Your code is correct, but you cannot allocate memory dynamically. What if you want to store the measured value in node.date and you don’t know how many measures you will capture? Then you must malloc some new memory on every dimension that you take.

If you define all the nodes before the runtime (directly in the code), you cannot save more measures than you determined earlier.

Search related lists in c and you will find some good examples.

+1
source

The data type looks like a node in a tree. The two main reasons for using malloc for a node tree would be

  • To select an arbitrary number of nodes. The number of tree nodes will generally be a run-time value. For this reason, it is not possible to declare the correct number of local variables for such nodes, since all local variables must be declared at compile time. Meanwhile, malloc can be called at run time as many times as you want, allocating as many node objects as you need.

  • To make sure that the node will not be automatically destroyed when the local object expires (i.e. at the end of the block). Objects allocated by malloc live forever, i.e. Until you destroy them explicitly by calling free . Such an object will exceed the boundaries of blocks and the boundaries of functions. Something similar is possible with local objects, since local objects are automatically destroyed at the end of their block.

Your sample code does not depend on any of the benefits of dynamic allocation, since it really creates a real tree. It is simply declared as a single node. But if you try to build a complete tree with the number of nodes at runtime, yo will immediately realize that it is impossible to do this by declaring the nodes as local objects. You will inevitably have to allocate your nodes using malloc .

Your "like n1, which is not initialized in a memory location capable of storing data", should be caused by some confusion. struct node n1; is the definition of an object, which means that it assigns a memory location for n1 . It is the purpose of determining the object.

0
source

Source: https://habr.com/ru/post/955646/


All Articles