TalkSick
So if I do not care about the entire set of structure values at a certain point so I can assume that the uninitialized fields are just garbage
No ,not garbage. The compiler won't initailize a garbage value but some sort of default value like '0' or 0.0 it depends on the data type of that particular variable
Serenity
Okay, thanks a lot ! it really helped me.
Anonymous
Anyone from US, UK and EU(especially Netherlands)
Anonymous
How to edit a printed text?
Anonymous
you can use \b backspace
Serenity
If I dynamically allocate a struct and some particular fields in it , and I want to free it at the end, Is it sufficient to free the struct pointer or do I have to free each specific allocation I've executed before for each struct field ?
Asdew
You need to free everything you have allocated yourself.
Serenity
Okay but what is the logic behind that ?
frakw
Will this code cause memory leak? class A { char *someHeapMemory; public: A() : someHeapMemory(new char[1000]) {} ~A() { delete[] someHeapMemory; } }; class B { A* APtr; public: B() : APtr(new A[10]) {} ~B() { delete[] APtr; } }; int main() { B* BPtr = new B[5]; delete[] BPtr; return 0; }
Anonymous
If that particular fields are allocated dynamically, you should free those field
Asdew
Okay but what is the logic behind that ?
Free cannot know that the thing you want to free is a struct and where there are pointers in the struct, if it even knew it is a struct.
Anonymous
A a; A aa(a); Calls delete twice on the same chunk of memory when a is destroyed
Anonymous
It will call a destructor to destruct Aptr in Bptr element
Anonymous
So that fine, but be cautious when use exception.
Marián
one thing that twists my mind a bit
Marián
Marián
in what scope is the EventHandler initialized and when is it popped from stack?
Marián
Marián
is the EventHandler object created on that += level scope? eg on same level as evt1 is this in this scenario
frakw
I saw this problem on stackoverflow https://stackoverflow.com/questions/677653/does-delete-on-a-pointer-to-a-subclass-call-the-base-class-destructor but i don't know what if i change B() : APtr(new A()) {} ~B() { delete APtr; } To B() : APtr(new A[10]) {} ~B() { delete[] APtr; }
frakw
So that fine, but be cautious when use exception.
So the delete[ ] also call destructor for all elements in array?
Anonymous
Yes, you can add something verbose to A destructor for verification btw
MrILL
Dima
lol
Asdew
Malloc stores some data, such as the size, but malloc does not know anything about the actual data.
Asdew
If I understand you correctly, I think that's what I tried to say.
Asdew
Yes, free just doesn't know what the data you allocated has.
Marián
yeah but it certainly has longer lifetime
Marián
i mean this->hook appends it on vector on class-level scope
Marián
and it's being called and invoked from totally different scope
Marián
from scope that is on evt1 level
Marián
and it still lives, so i guess that operator overloading doesn't act like a normal function?
Marián
oh wait, it does copy
Marián
okay, this is embarassing, let's pretend we didn't see it
Anonymous
What type of projects are making passive incomes?
,_
It approves useful content sharing not a spam
Dima
It approves useful content sharing not a spam
no, you should have read the ruels
Pavel
What type of projects are making passive incomes?
Shitty f2p mobile games like quizzes do
Anonymous
/get cppbook
Anonymous
/get cbook
Anonymous
/get cppbookguide
Anonymous
.
klimi
.
.
Artöm
in what scope is the EventHandler initialized and when is it popped from stack?
You may think that each line has its own scope, in which temporary objects are created
Ajay
struct COMPA{ bool operator()(pair<long,pair<long,long>> &a, pair<long,pair<long,long>> &b){ if(a.first!=b.first){ return a.first<b.first; } return a.second.first > b.second.first; } }; priority_queue<pair<long, pair<long, long>>, COMPA> q;
Ajay
Why does the above thing give compilation error? Why do I need to do it like priority_queue<pair<long, pair<long, long>>, vector<pair<long, pair<long, long>>> , COMPA> q;?
Nikk
/notes
Artöm
Because comparator is the third template argument
Nikk
/noendl
Anonymous
/notes
Nikk
#noendl
Ajay
Because comparator is the third template argument
so you whenever we need to supply the 3rd argument, we should always pass vector<type> as 2nd argument?
Artöm
Yes
Ajay
so this is always in case of priority_queues?
Artöm
so this is always in case of priority_queues?
Yes. But often T provides operator<
Ajay
Yes. But often T provides operator<
I hope T provides a operator< is alternative of providing a custom comparator as I did above, not making the comparator inside the class/structure of T for T.
Ajay
https://pastebin.com/pn35WKZ0 I don't understand why the vector and priority_queue in the above case produce different outputs, I mean how does the comparator function work in each of these cases?
Mar!o
What's the difference between vbroadcastsd and vbroadcastsdymm1 (AVX)
Artöm
int* is not char*
Artöm
fgets reads a string, not an int array
Artöm
Yes
Daniel
😹😹
Daniel
Yep
Serenity
If I advance a pointer, and I want to go back to the first value it has referred to , how can I do that ?
Serenity
It's a struct pointer
Marián
You may think that each line has its own scope, in which temporary objects are created
well the issue is that my mind somehow (don't ask me how) totally forgot about 'copying'
Marián
but nvm
Harleen
/notes
/notes
Serenity
How to advance a pointer to pointer ?
Serenity
If I have int **x
Serenity
How do I increase the last pointer ?
Artöm
Which is the last?