Is there any difference in memory allocation difference in c and c++ ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there any difference in memory allocation difference in c and c++ ?

new char[size] and malloc() have same kind of heap

22nd Mar 2021, 11:13 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
4 Answers
+ 2
Martin Taylor I have doubt that both new/delete and malloc()/calloc()/realloc() are used together in C++ then is there any chances of failure or harmful for system. DHANANJAY
22nd Mar 2021, 12:32 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
Martin Taylor idk if there's some confusion or misunderstanding.. but I what I meant was that this syntax should be abandoned(the one you wrote). Array with smart pointer would be like: std::unique_ptr<int[]> ptr(new int[5]); That's its, no need to delete anything, it does it itself..
22nd Mar 2021, 2:22 PM
Maher Al Dayekh
Maher Al Dayekh - avatar
+ 1
Thanks all for replying. I need to know we are using C/C++ on the same system, Is there any chance of heap related problems because of both have been used together. Looks like smart pointers is better solution for no memory leak. DHANANJAY
23rd Mar 2021, 1:23 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
Best practice is smart pointers. For example: std::unique_ptr<int> ptr(new int(5); Is better than memory leak prone: int * ptr = new int(5); delete ptr; ptr = NULL;
22nd Mar 2021, 1:30 PM
Maher Al Dayekh
Maher Al Dayekh - avatar