Vlad
Just put cout into constructor
Aakash
Yes it does
Can your point me to the source!
Hmaada
so it doesn't work i wrote smthg like this and it doesnt work too if(str==NULL){ printf("test : %s\n",str ); return -1;
ah i think i found it if(str[0] == NULL){ printf("test : %c\n",str); return -1;
Hmaada
so the real question must be how to verify if a pointer that must conatin a string is empty?
Vlad
Can your point me to the source!
https://en.cppreference.com/w/cpp/language/new
Pavel
New doesn’t call the constructor! It’s just an operator!
I'm pretty sure they asked about new, not about operator new. Also "if in short". Or what do you mean?
KiiroFruit
Hello
Anonymous
Igor🇺🇦
explain please
What do you want to be explained?
Anonymous
what is this node* is it pointer
Anonymous
Please explain the meaning of node*. What is the purpose of this * in node?
Anonymous
Is it pointer ?
Cva
Anyone have single linkedlist program?
Anonymous
Normally we write a pointer like this *node, but why we put * in behind the node?
Igor🇺🇦
Normally we write a pointer like this *node, but why we put * in behind the node?
Node* is a pointer to an object of type Node. That's the syntax. When you have: Node *node; It means that you declared a pointer to type Node with the name node. You can assign address of other instances of type Node to pointer called node.
Anonymous
Anyone having shaumi DSA book
Aakash
I'm pretty sure they asked about new, not about operator new. Also "if in short". Or what do you mean?
Whether new_expr or new_op; both don’t call constructor; neither does delete calls destructor. It’s the object that’s calling constructor & upon scope completion it’s calling destruction. new_expr only allocate object; whereby new is parser code; so it knows, rather standard/logic, that object calls constructor, new will allocate memory for both constructor & object; as upon delete it fres the allocation in which object releases the destructor. new doesn’t call constructor! Delete doesn’t calls destructor. They are memory allocation and terminators. Constructors & destructors are object(s) scope based. Example below!
Aakash
Scope of constructor & destructors —- include <iostream> using namespace std; class X { int scope=0; public: X(int scope) { ///constructor-declaration cout <<"Object Constructor:" <<(X::scope=scope) <<endl; } ~X() { ///destructor-declaration cout <<"Object Destructor :" <<scope <<endl; } }; int main() { ///scopr:0 cout <<"__scope:0__" <<endl; X x(0); { ///scope:1 cout <<"__scope:1__ " <<endl; X x(1); { ///scope:3 cout <<"__scope:3__ " <<endl; X x(3); } } { ///scope:2 cout <<"__scope:2__ " <<endl; X x(2); } cout <<"\n__scope:0__" <<endl; return 0; } 🙃
Anonymous
/get cbook
Anonymous
/get cppbookguide
Pavel
I'm not really getting it, I mean calling a constructor is a vital part of the construction of the object, and construction of the object is a part of new (they don't happen separately one after another: if constructor throws, new also fails and deallocation happens, maybe not exactly in these words but the idea is the same).
Pavel
And to explain the difference between new and malloc for someone who is new to the language and doesn't need to know all the details yet (they are not going to start writing a compiler tomorrow I hope), I think it is still a decent explanation to say that new calls the constructor whether malloc doesn't.
Aakash
So, your point is, "new is not calling constructor, new makes constructor being called"?
“New” is not calling constructor! Let me explain.. with codes.
Aakash
“New” is not calling constructor! Let me explain.. with codes.
New is just allocating space for both object and constructor(which object is calling) ...
Aakash
Try godbolt this basic code!! — class X { public: X() {} ~X() {} }; int main() { X x; //scope:1 return 0; } This will tell you about generic constructor & destructor functionality. 🙃 ..wait more code coming for new/delete.
Aakash
Now godbolt this! class X { public: X() {} ~X() {} }; int main() { X *x = new X; delete (x); return 0; } 🙃 Check what is being called with “new”
Pavel
New is just allocating space for both object and constructor(which object is calling) ...
What do you mean for object and constructor? Constructor is just a function it doesn't need memory allocated additionally
Vlad
Vlad
That's all he has to know
Vlad
Where as with malloc it won't
Aakash
While executing new expression constructor shall be called
New allocated memory for both object & constructor!! Let me share the parser code
Aakash
There's no memory allocated for constructor
Yes; memory is being allocated as new_exp is a parser; it’s hard coded to compiler.. let me share the parser link
Pavel
Now godbolt this! class X { public: X() {} ~X() {} }; int main() { X *x = new X; delete (x); return 0; } 🙃 Check what is being called with “new”
Not sure what I supposed to see, new calls operator new and constructor :shrug: https://godbolt.org/z/jj5ET3
Vlad
Yes; memory is being allocated as new_exp is a parser; it’s hard coded to compiler.. let me share the parser link
I really don't care for this controversy. The point was malloc doesn't construct objects. You'd have to call placement new afterwards to begin object's lifetime
Aakash
And before freeing you'd have to call placement delete as well
New/delete are acting like malloc/free; nothing beyond, except some memory & Value checks. More with new/delete is the object/constructor & template (allocation only support) now calling. It’s just the allocator & terminator. Nothing beyond.
Vlad
If it's "nothing beyond"
Aakash
Not sure what I supposed to see, new calls operator new and constructor :shrug: https://godbolt.org/z/jj5ET3
See both the examples with new/delete and without it. Constructors are called & destroyed by object scope aka validity.
Aakash
Then why placement new exists?
Malloc allocated the structure size only; new allocated both structure size +constructor size.
Vlad
What the heck are you talking about lmao?
Aakash
That’s why I was trying to share the parser code! You make check with parser under cp/parser.c line 8980 new_expr parsing..
Aakash
Object calls the constructor; so before placing object, construct also need to be placed before new for allocation, else memory allocation with new will fail.
Aakash
I gave the simple codes with & without new/delete; you don’t get it it’s your concern.
Vlad
I really don't know what are you trying to prove, to whom and why. After the new expression the constructor WAS already called that's all that matters.
Pavel
See both the examples with new/delete and without it. Constructors are called & destroyed by object scope aka validity.
Not sure what do I need to get from it. The fact that constructor can be called not only in new statement doesn't mean that it is not called in new statement.
Pavel
And before freeing you'd have to call placement delete as well
Is there "placement delete"? Or you meant calling the destructor explicitly?
Asad
The second, I believe
Aakash
The second, I believe
Take a simple example! >> New allocates fresh memory location & size to objectX. This objectX code calls constructor code. Ex: you meet a girl; you ask her for date (dinner) at a good restaurant (allocated space). The girl comes with her child (constructor) on the date, you need to adjust the child too in the allocated space!! Once dinner is over.. >> Delete deletes the allocated space/memory location as allocated by new! This deletion of objectX location, the objectX code calls destructor code. New/Delete are memory location allocators & memory location terminators. New/delete works on pointers; It’s the objectX code that is calling constructor & destructor code. ... Now you may guess what went on that dinner date. 😄🙃
Pavel
Take a simple example! >> New allocates fresh memory location & size to objectX. This objectX code calls constructor code. Ex: you meet a girl; you ask her for date (dinner) at a good restaurant (allocated space). The girl comes with her child (constructor) on the date, you need to adjust the child too in the allocated space!! Once dinner is over.. >> Delete deletes the allocated space/memory location as allocated by new! This deletion of objectX location, the objectX code calls destructor code. New/Delete are memory location allocators & memory location terminators. New/delete works on pointers; It’s the objectX code that is calling constructor & destructor code. ... Now you may guess what went on that dinner date. 😄🙃
Do you want to say that objects created with new use the mechanism of handling objects with automatic storage duration (the mechanism that calls constructor and destructor for variables on stack)? These are different mechanisms. Yes, in both ways constructor is being called but that's it. There's no scope with new, no destructor called automatically. And even if you right and somewhere in standard it says that the same mechanism should be used for handling lifetimes of the objects created with new and within scope, then it really makes no difference here, you call new, you get constructor called before new finishes its work.
Aakash
Do you want to say that objects created with new use the mechanism of handling objects with automatic storage duration (the mechanism that calls constructor and destructor for variables on stack)? These are different mechanisms. Yes, in both ways constructor is being called but that's it. There's no scope with new, no destructor called automatically. And even if you right and somewhere in standard it says that the same mechanism should be used for handling lifetimes of the objects created with new and within scope, then it really makes no difference here, you call new, you get constructor called before new finishes its work.
—your quote— ...then it really makes no difference here, you call new, you get constructor called before new finishes its work. — Yes it does makes huge difference! You call constructor AFTER the new call, NOT before, like you said. New —allocates—>ObjectX—-call—>Constructor Same applies for delete—> New has to adjust both object & constructor. Please see parser code for new! Parser knows ALL execution. Please see it for yourself in “main:” line 21 & 24. — https://godbolt.org/z/jj5ET3
Vlad
Please can we end this silly controversy already?
Tahmedur
Hi is there anyone who teach me c++ oop concept Well? i will pay
PO
HI someone can explain to me why I must have a memory leak with this line of code ? int load_pnm(PNM **image, char* filename){ assert(image != NULL && filename != NULL); PNM *ptr = malloc(sizeof(PNM)); //Allocation memory for structur if(!ptr){ printf("Load Function : Structe Memory Allocation Failed \n\n"); return -1; }
PO
Valgrind show me a memory leak
PO
https://www.tutorialspoint.com/how-do-malloc-and-free-work-in-c-cplusplus
ok I have forgotten cast-type, I've added (PNM*) before malloc still getting same error
PO
int load_pnm(PNM **image, char* filename){ assert(image != NULL && filename != NULL); PNM *ptr = (PNM*) malloc(sizeof(PNM)); //Allocation memory for structur if(!ptr){ printf("Load Function : Structe Memory Allocation Failed \n\n"); return -1; } ==14168== HEAP SUMMARY: ==14168== in use at exit: 24 bytes in 1 blocks ==14168== total heap usage: 2 allocs, 1 frees, 1,048 bytes allocated ==14168== ==14168== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==14168== at 0x483877F: malloc (vg_replace_malloc.c:307) ==14168== by 0x109737: load_pnm (pnm.c:143) ==14168== by 0x1093B5: main (main.c:76) ==14168== ==14168== LEAK SUMMARY: ==14168== definitely lost: 24 bytes in 1 blocks ==14168== indirectly lost: 0 bytes in 0 blocks ==14168== possibly lost: 0 bytes in 0 blocks ==14168== still reachable: 0 bytes in 0 blocks ==14168== suppressed: 0 bytes in 0 blocks ==14168== ==14168== For lists of detected and suppressed errors, rerun with: -s ==14168== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
PO
this is not the part where the leak happens
there is the only place where I use mallo()
Anonymous
there is the only place where I use mallo()
follow your ptr through the code to see where you forgot to free().
Anonymous
/rules
Ralph
Hello
Ralph
Does anyone knows what are non-copyable types?
Ralph
I want to implement them to a function not a class
Ralph
On google all the examples are on a class
Igor🇺🇦
I want to implement them to a function not a class
You copy fields in class and you may want to avoid this. That's why you may have non copyable classes. But functions don't have members. What do you want to make "non copyable" in functions?
Ralph
I have a unique_ptr that is non-copyable. My aim is to reverse a vector in which the content are the values in unique_ptr
Igor🇺🇦
I have a unique_ptr that is non-copyable. My aim is to reverse a vector in which the content are the values in unique_ptr
So you just need to use comparator in std sort that compares values *a > *b. Why does it matter if you can or cannot copy?
Vlad
Does anyone knows what are non-copyable types?
It is types that have both operator=(const T&) and copy constructor deleted
Ralph
It is types that have both operator=(const T&) and copy constructor deleted
Yes i saw this but this only work for classes not functions