What does 'new' do that malloc/realloc do not do ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What does 'new' do that malloc/realloc do not do ?

Today vs code gave me this warning: "moving an object of non-trivially copyable type ‘class vektor<int>’; use ‘new’ and ‘delete’ instead [-Wclass-memaccess]". Why does vs code encourage me to use new instead of realloc ? What does new do that realloc doesn't/can't do ??

26th Jun 2023, 10:26 PM
Ali_combination
Ali_combination - avatar
5 Answers
+ 4
Like malloc(), new allocates space in memory. Unlike malloc(), new initializes the memory and calls the class constructor. I am struggling to understand in what manner you were trying to use realloc() on an object pointer, and struggling even more to understand why. It would be more helpful if you link your source code and explain the thought behind what you were trying to do. Maybe there is a more conventional way to do it.
27th Jun 2023, 5:20 AM
Brian
Brian - avatar
+ 3
We use use malloc to allocate new memory to store data while realloc is used to reassign memory to mostly add more space or memory to add data.
27th Jun 2023, 4:05 AM
Leeroy Mokua
Leeroy Mokua - avatar
+ 2
What is the code that causes this warning? What C++ standard are you using? Is it your own `vektor` template class or `std::vector`?
26th Jun 2023, 11:59 PM
Евгений
Евгений - avatar
0
Евгений true. I was using my vektor and the compiler gave me this warning
28th Jun 2023, 9:04 PM
Ali_combination
Ali_combination - avatar
0
Ali_combination What about my other questions?
28th Jun 2023, 11:37 PM
Евгений
Евгений - avatar