struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
What are we doing on the right-hand side of this declaration and assignment statement, why is this necessary, and where else should one use such a statement?
I'm learning C again after a few years, forgive me for asking something simple.
CodePudding user response:
Looking at that statement in reverse order:
sizeof(struct Node)
, this returns the size ofstruct Node
in bytes in the form of astd::size_t
.malloc( sizeof ( struct Node ) )
, this function dynamically allocates enough memory for your program at runtime in order to be able to store astruct Node
instance in it and then returns the address of that memory block.(struct Node*)
, this here is a C-style type cast. Becausemalloc
returns a pointer tovoid
(i.e. avoid*
) you need to cast it to a pointer of typestruct Node*
in order to assign it to another pointer of typestruct Node*
. Obviously, the types should match.struct Node* new_node =
, this is the declaration of a variable of typestruct Node*
namednew_node
and then you initialize it using the expression on the right-hand side of the=
. Keep in mind that this is an initialization and not an assignment.
With that said, the above code is C-style code. I would like to rewrite it in a C -style way just for the purpose of demonstration.
Here is a simple example of it:
#include <iostream>
struct Node
{
int m_data;
Node* m_next;
Node( const int data = 10 )
: m_data { data }
{
}
};
int main( )
{
Node* new_node { static_cast<Node*>( new Node{ 5 } ) };
std::cout << "Data: " << new_node->m_data << '\n'
<< "Address: " << new_node << '\n';
delete new_node;
new_node = nullptr;
}
CodePudding user response:
Let's break it down:
void *malloc(size_t size)
- gives you a beginning of a memory block of size size
that you can use to store your data.
In your case, you area creating a dedicated block of memory of exactly the size of Node sizeof(struct Node)
. As you can see in the malloc's signature - it returns a void* pointer, so you explicitly (using old C casting) cast it to a pointer to your struct (struct Node*)
and then store it in a pointer named new_node
.
CodePudding user response:
Okay, let me strip that down for you.
struct node* newnode = (struct node*) malloc(sizeof(struct node)); This statement has at least three operation that has to be understood.
struct node* newnode; This statement declares a variable named newnode as type of pointer to struct node. The structure is possibly defined somewhere before this code snippet.
malloc(sizeof(struct newnode)); malloc() is a standard library function in C that allocates memory of size equal to the size of newnode structure in the heap region during runtime and returns a void pointer to that location.
newnode = (struct node*) malloc(...); The pointer returned by malloc() function is type-casted into pointer to struct node type and then assigned to newnode variable.
So in a nutshell, the given code snippet dynamically allocates memory of size enough to hold node structure and type cast it to pointer to node structure, then assigns to newnode variable.