Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi !!
i want to ask a little question i learned this from some site !! i dont know what does this means also i want its alternate in form of " new " if any (dynamic memory)

" new_node = (struct node *)malloc(sizeof(struct node)) " // here i am encountering problems and please tell me its alternate !!

What I have tried:

C++
struct node *new_node;
	char ch='y';
	do
	{
		new_node = (struct node *)malloc(sizeof(struct node));
		cout << "\n Enter the data:";
		cin>>new_node->data;
		new_node->next = NULL;
Posted
Updated 16-Dec-16 12:07pm
Comments
Manoj Kumar Choubey 16-Dec-16 11:32am    
malloc just allocate row memory and fee allocate but new operator call constructor and deallocates the memory and after that will deallocate.
Richard MacCutchan 16-Dec-16 11:59am    
Not correct.
[no name] 16-Dec-16 11:34am    
Read the documentation for things you do not understand
http://www.cplusplus.com/reference/cstdlib/malloc/
http://www.cplusplus.com/reference/new/operator%20new/
Richard MacCutchan 16-Dec-16 11:58am    
Then please tell us exactly what problems you are encountering.
mayashah 17-Dec-16 0:59am    
actually i dont want to use malloc !!
i want that i use "new" instead of malloc but i dont know how to use it !!

malloc is a library function which is short for "memory allocate" - and it's job is to return a pointer to a free block of memory which is the size you specify in the parameter. You can then cast that pointer to whatever type you need to use it for, and use it as an instance of that structure. This allows you to create dynamic instances of memory at run time, instead of having to create them all at compile time.
If you use malloc to allocate memory, you must use free to release it when you are finished with it, or your application will show a "memory leak" and will use more and more memory the longer it runs.

Depending on your system, you may be able to use new and delete instead.
 
Share this answer
 
malloc - cppreference.com[^] (Edit: for Memory ALLOCation) is a function from the C standard library to allocate memory from the heap.

It can be still used with C++ but new should be used instead (see also std::malloc - cppreference.com[^]) .

One difference is that malloc returns a void* pointer that must be casted to the type on the left side with C++:
struct node *new_node;

// OK with C, but fails to compile with C++:
new_node = malloc(sizeof(struct node));

// OK with C and C++:
new_node = (struct node *)malloc(sizeof(struct node));

// Recommended with C++:
new_node = reinterpret_cast<struct node *>(malloc(sizeof(struct node)));

If you compare this with C++ new
C++
new_node = new node;
you will notice the next difference:
There is also no need for the sizeof() operator because the compiler knows the type and it's size.

Another difference is that new - when called for classes - calls the constructor and allows passing arguments to the constructor.

One thing to observe is that always the corresponding release function must be used (free resp. delete).
 
Share this answer
 
v2
You should really take time to learn properly C/C++.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
Comments
mayashah 17-Dec-16 1:01am    
sorry no offense but i am not here to see suggestive answers like that !!!
Patrice T 17-Dec-16 1:21am    
No offense. I posted these links because you already got 2 answers.
Since malloc is learned very early, I really think these book will be intersting for you.
You may prefer tutorials.
mayashah 17-Dec-16 10:16am    
Thanks i appreciate !! i will defiantly concern these !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900