|
They are all allocated on the stack & so gets cleaned automatically. You are almost nearly talking about something like this :
struct TestStruct
{
CObject* pObj;
TestStruct()
{
pObj = new CObject[20];
}
};
Then in the dtor you'll have to manually delete all the cobjects.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
Don't confuse it all.
Assume we have
struct TestStruct{
CObject obj[ 20 ];
};
{
TestStruct ts;
}
{
TestStruct *ts = new TestStruct;
delete ts;
}
{
TestStruct *ts = new TestStruct;
}
|
|
|
|
|
Thanks for clearing that up.
I was confused as well.
|
|
|
|
|
I think I'm not confused.
Here[^]
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
struct TestStruct{
CObject obj[ 20 ];
};
Didn't you notice I changed my example to
struct TestStruct{
CObject* pobj;
TestStruct()
{
pObj = new CObject[20];
}
};
?
I was trying to give an example where the destructors of the CObjects wouldn't be called. What am I confusing here? Whatever may be the creating type of the TestStruct object, heap or stack, here in my example, it's going to create all the CObjects in heap.
I'll explain you better. Suppose Now you create an object of TestStruct:
TestStruct obj;
So in the destructor we have to manually delete them all.
~TestStruct()
{
delete[] pObj;
}
That's what I explained. May be you didn't get my context.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
Easy, man! We are just talking here.
Just to be safe, I explained it all relevant to the question.
From your initial post, it was not totally clear that you meant this what you have shown here, and it is a different example from what was asked, so I just wrote it all out using the same code pattern from the initial question to avoid any possible confusion, because everything is fine as long as the person who asked the question got the right understanding not you or me.
|
|
|
|
|
|
I would expect them to be allocated on the stack IF my ts variable were a stack-based variable. But if you check my original post, you'll see that ts is allocated on the heap with new.
Are you saying that even though ts is created on the heap, that the CObject array inside the structure is created on the stack?
|
|
|
|
|
Richard Andrew x64 wrote: Are you saying that even though ts is created on the heap, that the CObject array inside the structure is created on the stack?
Exactly NOT. Actually my reply was not exactly an answer for your question. I was trying to explain the other way where it would result in a condition that you expected.
But in the reverse, I'd say, when you allocate CObjects in heap, no matter you create the TestStruct on heap or stack, they are going to remain in heap.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
constructor: all (or better say each)
destructor: all
Why did you have trouble with that?
|
|
|
|
|
Hi,
I need some help on DateTime control, by default it will displays the current date on datetime picker. If you have changed that date to next month then i need to display one error message box.
just like for example today date is 3/2/2009 and i have selected date in datetime picker as 20/2/2009.
This is wrong right..? So i need to throw one error message box, Can any one help which event will get fired when datetime picker will changed.
I have already tried with DTN_DATETIMECHANGE, it's not working, that the event is getting executing repeatedly..
Can any one help me regarding this..?
Thanks in advance...
|
|
|
|
|
Why DTN_DATETIMECHANGE does not work for you?
It is WM_NOTIFY message and once it is fired all you need is to cast
lpChange = (LPNMDATETIMECHANGE) lParam;
and read
DWORD dwflags; // this should be GDT_VALID
and then
SYSTEMTIME st;
to see what time was selected. Then if it is month from now display a message.
|
|
|
|
|
Thanks for your answer, i am able to solve the problem..
|
|
|
|
|
Hey ppl,
I have two pointers and one value:
int* p1 = new int;
*p1 = 10;
int *p2;
p2 = p1;
Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:
delete p1;
p1 = NULL;
Now p1 is NULL and referenced the only integer is deleted. Now if I try
delete p2;
p2 = NULL;
Just crashes cause int which is referenced by p2 is not valid anymore.
So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now?
Any workarounds?
|
|
|
|
|
dehseth wrote: p2 = p1;
But, why one would like to do that ? Heard of need for copy c'tor and assingment operators ? Or dangling pointers ?
|
|
|
|
|
Just need it...
|
|
|
|
|
dehseth wrote: I read about shared pointers with templated which are not good for me to use right now?
In case you change your mind: shared_ptr[^] from Boost[^].
Steve
|
|
|
|
|
Nope, but thanx...
|
|
|
|
|
You could always try designing your software so you've got a clear, single owner of resources such as allocated heap memory.
Alternatively,
- Use .NET - garbage collection means you don't need to worry about object ownership quite as much
- Use Objective C 2.0 - it allows garbage collection, and garbage collection means you don't need to worry about object ownership quite as much
- Use the Boehm allocator[^] - it's garbage collected, and garbage collection means you don't need to worry about object ownership quite as much
- As other posters have suggested, use Boost's shared_ptr class.
If you want to stick with Windows and C++, I'd go for designing your software to manage resources or Boost shared_ptrs.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
.NET won't satisfy my needs. Garbage collector slows me down.
|
|
|
|
|
Not as much as pointer errors will
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I have a program that finds trading opportunities. I want to be able to provide the ability to auto fill the order information in third party trade order-entry programs. What is a good place to start to build this? This is on Windows XP.
So far I have looked at GetWindowLongPtr and SetWindowLongPtr. If anybody has any sample code that would be awesome.
|
|
|
|
|
What is the program you are dealing with? Does it have a window with text fields you want to fill in?
If so you do this:
Open the program and reach the top window you want to deal with. The top window is a window with text fields.
Using Spy++ from Visual Studio tools you read the ID for each text field (or other control) that you want to fill in and check what its relation to the top window is
Once you have relation and ID you are very close.
Now in order to get the top window you need its name and/or its relation with the main application window. And then using FindWindow EnumWindows EnumChildWindows... you can reach the top window of each control.
Now you need GetDlgItem to get actual control that you want to fill in. Once you have HWND for each control SetWindowText() is sufficient for most text control, however for radio button etc. you need to use other functions to set the value or state. If you are sure that HWND you've got is from the control you need you can always use CWnd::FromHandle and even better upgrade such CWnd* to CEdit* or whatever control you have.
However once the program is changed and any control ID, or their position is changed, you have to fix your code as well.
The above procedure is used for automated testing a dialog and filling it from another application, so I know it must work.
Without Spy++ or a similar application all this would be almost impossible to complete.
|
|
|
|
|
Greetings to all.
At me a question on structured storages. I have one big file of the order 2 GBytes. I should be able read and write to it quickly. The number of such operations can reach some thousand. I have started to use it in hope that it will work quickly - before instead of one big file there were some tens thousand small ones. Unfortunately reading/writing with it works also slowly. And operations OpenStorage and OpenStream work slowly. Speed of work depends on the size of a file - for not so greater methods IStorage and IStream work fast. I assume, that have collided with restrictions of these objects (IStorage and IStream) and hardly it will be possible to overcome them. But there can be I am mistaken also somebody can prompt as to solve my problem. Or to specify existing equivalents IStorage and IStream which work quickly and them it is possible to use freely (better with an initial code). Below I shall set a brief example of that that I used:
1. Tried to open IStorage with different flags. Has noticed, that speed of work depends on it. Flags STGM_READWRITE, STGM_READ, STGM_WRITE;
2. Method Commit also brakes even if I do not use transacted mode;
3. Root storage I created so:
StgCreateDocfile (T2OLE (csPath), STGM_CREATE|STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE, 0, *lpRootStorage);
StgOpenStorage (T2OLE (csPath), NULL, STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE, 0,0, *lpStorage);
If at me a small storage file (the exact size I do not remember) methods of interfaces IStorage and IStream work very quickly, from 0 up to some milliseconds. If a file big the operating time can reach hundreds milliseconds.
|
|
|
|
|
You could try Memory Mapped files[^]. If that does not satisfy your performance requirements you might need to consider using a database engine.
|
|
|
|