0

Im quite noob in c++ so i ask you to explain it the in the simplest way you can, please! I know that to say:

int * p = new (x) int;

is like to say:

operator new (sizeof(int), x);

But why?? Can someone do me some simple exaple and explain it? And if i'm wrong, could you try to correct me, and explain it? I come from C# language so for me if a little bit disorienting to see this new syntax. I have like the feeling that there is much more to understand about the way this new is used in c++. I'm i wrong? Thanks a lot!

Update:

"I just found this useful videos" where is explained very well some content related to this topic. If someone need it just enjoy:

in this video this guy explain very well tyhe basics of new in c++:

https://www.youtube.com/watch?v=NUZdUSqsCs4

Here an interesting video about operator overloading in c++:

https://www.youtube.com/watch?v=mS9755gF66w&t=683s

And here the most important one where this guy explain very well something called "placement new" In C++:

https://www.youtube.com/watch?v=2bsGFQgBMXs

All these 3 videos togheter gave me a lot more awareness about the topic!

  • That's just the way language is defined. Expression `new (x) int` is to be interpreted by the compiler as `operator new(sizeof(int), x)`. I don't think there exists any explanation other than "standard says so". Similarly, `std::string("Hello, ") + std::string("World!")` is equivalent to `operator+(std::string("Hello, "), std::string("World!"))` – Yksisarvinen Jun 13 '22 at 22:45
  • C++ is full of handy (and sometimes not so handy) shortcuts. This is just another one of those. – Paul Sanders Jun 13 '22 at 22:51
  • Hello, thanks for reply. I come from C# so it's new for me to see this syntax "new (x) int". should i interpret it as the new() of c# or there much more in deep? – Vincent Mazzarella Jun 13 '22 at 23:06
  • @VincentMazzarella `new (x) int` is [placement-new](https://en.cppreference.com/w/cpp/language/new) syntax. The `(x)` is just the syntax needed to pass extra parameters to an implementation of `operator new`. For example, when allocating a new object at a pre-defined memory location. However, most of the time, when you use `new` in C++, you use it by itself, just like you do in C#, eg: `int * p = new int;` – Remy Lebeau Jun 13 '22 at 23:13
  • It's not too deep. Just a language provided hook to influence how memory allocation happens. Also, I'd avoid constantly drawing parallels with C#. Syntax similarities aside, the languages have very different programming models. Constant comparison is only likely to confuse. – StoryTeller - Unslander Monica Jun 13 '22 at 23:13
  • @PaulSanders Hey, i see! So how should i use this operator new(sizeof(int) x)? In c# when i want initialize something i usually use new something() and i have nothing to worry about. It's very explicit and intuitive. But here looks a bit more tricky and cumbersome, right? – Vincent Mazzarella Jun 13 '22 at 23:14
  • 1
    @VincentMazzarella "*how should i use this `operator new(sizeof(int), x)`?*" - 99% of the time, you shouldn't use that directly at all. But when you do, you should use the `placement-new` syntax `new (x) int` and let the compiler handle calling the `operator new()` for you. But that is very corner-case. "*In c# when i want initialize something i usually use `new something()`*" - do the same in C++, eg: `new int`. A better option is to not dynamically allocate memory when you can avoid it. But if you must, use `std::make_unique()` instead and let it handle calling `new` for you. – Remy Lebeau Jun 13 '22 at 23:16
  • 1
    In modern C++ you shouldn't use `new` at all (or very rarely). I suggest getting [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from, trying to learn by parallels from C# is going to be very, very troublesome experience. – Yksisarvinen Jun 13 '22 at 23:19
  • @RemyLebeau I think im starting to undertand it better. So it looks like i can allocate more than 1 object in that specific memory location right? But this use more memory? Sorry maybe i am confusing things but i want to understand it better! – Vincent Mazzarella Jun 13 '22 at 23:21
  • 1
    @VincentMazzarella To be honest, as you are a noob, I'm surprised you have even come across `placement-new` and `operator new` yet, as those are advanced topics. – Remy Lebeau Jun 13 '22 at 23:22
  • 1
    @VincentMazzarella if you want to allocate more than 1 object at a time, use an array instead, eg: `int *p = new int[count];` Which is very similar to C#: `int[] p = new int[count];`. Just make sure you `delete[]` whatever you `new[]` (and `delete` whatever you `new`). You don't have to worry about that in C#, but in C++ you do. Let `std::unique_ptr` handle calling `delete`/`delete[]` for you. Or, use a standard container, like `std::vector`, which is a wrapper for a dynamic-length array. – Remy Lebeau Jun 13 '22 at 23:24
  • @Yksisarvinen Haha i see.. there are some similarities but i think c++ need to have more awareness of how things works behind! Actually im doing a course right now. But i didn't ecountered yet this cases so i just wated to be prepered in advance! – Vincent Mazzarella Jun 13 '22 at 23:28
  • 1
    It's really not wise for a beginner to be dipping their toes in this part of the language. You need to first lay some solid groundwork on "ordinary" memory management in C++. Without some years of solid experience doing normal stuff, you'll likely end up losing those toes. – paddy Jun 13 '22 at 23:28
  • @RemyLebeau Because my prof is trying to explaing it making comparisons with c# but he didn't covered the argument in deep because my course if for game developer. But since i discovered that i really love programming i want to understand c++ better and deeper. – Vincent Mazzarella Jun 13 '22 at 23:31
  • @paddy Yeah, maybe im fixating to much on this argument. Is just that i want to understand :P anyway really thanks a lot everybody for the answers! Tomorrow i will read all of it with calm! – Vincent Mazzarella Jun 13 '22 at 23:34
  • @VincentMazzarella "*my prof is trying to explaing it making comparisons with c#*" - that is not a good way to teach C++. "*he didn't covered the argument in deep*" - I wouldn't expect him to. This is not a topic beginners should be tackling yet. "*my course is for game developer*" - that is a fairly advanced use of C++. Game development has its own share of deep things you need to understand, regardless of the language used. – Remy Lebeau Jun 13 '22 at 23:34
  • @RemyLebeau Maybe i explained myself bad.. He isn't saying that is the same thing. He is just rushing some arguments maybe because is not really important for our goals and maybe i just misunderstood how new is used here. – Vincent Mazzarella Jun 13 '22 at 23:39

0 Answers0