When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved.
However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?
If you're responsible for managing memory you've allocated, then freeing it means it's gone; regardless of which part of your app did it. i.e. you're the one "counting the references".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
In case 1 you are creating a new block of dynamic memory space. When you call delete on the pointer it returns the memory space to the free pool, but the pointer still exists (until it goes out of scope, or the program ends).
In case 2 the pointer is referring to memory space that already exists, i.e. it was not created by new, so you cannot call delete on it.
The purpose of delete is not to delete things. What?? But I'm serious. The purpose of delete is to undo new.
As an approximation that will no doubt anger several C++ experts, you can view new as a combination of malloc and invoking the constructor, and you can view delete as a combination of invoking the destructor and free. That's not exactly literally how they work, but it's close enough that you can use that to reason about when you need them. (don't use this as an excuse to delete something that was malloc'ed)
Most of the time, if you want to get rid of something, delete is the wrong way to do it - because most things were not created by new. Trying to delete something else results at best in a failing safety check but in general corrupts the state of your application.
Most objects are destroyed implicitly, in various ways. Local variables by exiting a scope, values in containers by the container being destroyed, smart pointers do most of the rest. Explicitly using delete is occasionally necessary but it's probably better to actively avoid creating those cases. It's quite bug-prone, especially around exceptions and other non-trivial control flow.
Calin Negru wrote:
later if you use “delete” on the pointer will that delete the content of the variable too?
In the sense that its destructor is invoked (so you can implement it and delete the members that need to be deleted), yes. If you mean that in some other sense then, probably, no (but depending on what you mean).
Implementing classes with lots of raw-pointer members that you individually delete in the destructor is very mid-2000s. Typical advice is to delegate that to smart pointers and containers as much as possible, with the goal of following the Rule of Zero instead of one of the other rules-of-some-number, but sometimes you just have to do what you have to do.
Have you even looked at the documentation for QDebug?
QDebug &QDebug::operator<<(QChar t)
Writes the character, t, to the stream and returns a reference to the stream. Normally, QDebug prints control characters and non-US-ASCII characters as their C escape sequences or their Unicode value (\u1234). To print non-printable characters without transformation, enable the noquote() functionality, but note that some QDebug backends may not be 8-bit clean and may not be able to represent it.
which leads us to:
Disables automatic insertion of quotation characters around QChar, QString and QByteArray contents and returns a reference to the stream.
When quoting is disabled, these types are printed without quotation characters and without escaping of non-printable characters.
If you need quoted output, its simple enough to do it yourself, just escape the quotes (\") or use Raw literals e.g. std::string hello(R"x("Hello World")x") assigns "Hello World", *including the quote marks" to the variable hello,
NB. I have not tried this. I do not use QT. I don't know if this will achieve what you want. I do know how to use google, though. It's more than time enough that you did too.
Hello. I apologize in advance if I created the topic in the wrong place, because I think that my task is directly or indirectly related to WinApi. I will describe the task:
I am programming an imitation of manual work in applications for working on financial markets (Metatrader 4 and 5). I use AutoIt for this. I need to read the text of the SysListView32 items .I wrote the following code for this purpose .
Local $pid= ; here i write id of needed process
Local $hwnd= ; here i write handle of SysListView32
Local $maxtext= ;here i write max number of chars
Local $rectext=DllStructCreate("char ["&$maxtext&"]")
The problem is that the above code only gets text of items if the SysListView32 has either LVS_OWNERDATA or LVS_OWNERDRAWFIXED style. I have checked above code on different applications with SysListView32 that only had style LVS_OWNERDATA and checked above code on different applications with SysListView32 that only had style LVS_OWNERDRAWFIXED. In both cases i was able to get text from SysListView32 items. But above code is not getting text of SysListView32 items with combined LVS_OWNERDATA and LVS_OWNERDRAWFIXED styles . The SysListView32 of Metatrader application have combined styles LVS_OWNERDATA+LVS_OWNERDRAWFIXED.
Well here's the question:
How to get item text from SysListView32 from another application with both LVS_OWNERDATA and LVS_OWNERDRAWFIXED styles at the same time?
I would like to get some direction of action or example code (doesn't matter on AutoIt). Thank you.
I am not sure that you can get the information. When a view has one of the OWNERDRAW settings it does not have a backing list of items. As the view tries to display its contents it sends a message to the parent Window which tells the application to return the text for each item. So it is the application that holds the actual data, not the ListView.
There is no standard way of doing that. The data backing the owner-draw can be stored any way the application needs. It can be, literally, stored anywhere, in any format, with no public API to get at it.
Think of how you store data in your own applications. Do you expose that data in standardized ways across all of your applications? Do you expose that data in a public API? Of course not!
How can you insert the elastic walls of the snooker table and make all the 16 balls move and bounce off the walls?
Can someone pliz help me with the code of a functional snooker game using C programing.
Sorry this site does not provide code to order. To creatre such a program would need a good knowledge of C programming, general GUI processes and (probably) Windows GDI+ or Direct2D. This is not something that you could create in an afternoon.
Last Visit: 31-Dec-99 18:00 Last Update: 4-Oct-23 17:15