|
Vector impliments a grow policy so that it can limit the number of times it needs to recreate itself. It is unlikely to be as efficient as the custom code you are writing.
If you need this level of efficiency, I'd advise to write your own container so that it exposes random access iterators, so you can copy between it and STL containers, and use the provided algorithms, use it with streams, etc.
A pointer is a random access iterator, so it's not much harder than providing begin() and end() methods.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?!
- Jon Hulatt, 22/3/2002
|
|
|
|
|
Thank, you Christian. I take your reply as an encouragement, not a warning. I will toddle along the path I have choosen for a little longer and see where it takes me. The vectors will be around as a fallback.
Regards, Haakon.
|
|
|
|
|
The containers in STL normally implement a allocation strategie of 2*n elements, so if you request for instance 24 elements the vector will allocate space for 48 elem +/- , this is for performance reasons, since pre-allocating memory it's more cheap than allocating the memory one step at the time. But the normal allocator strategy is slow when you are always allocating small objects on the heap, then if this is your case perhaps you need a custom block allocator, this is tricky business. For a good book explaining custom allocators and their use with STL, I advise you to buy Modern C++ Design by Andrei Alexandrescu , it's a great book and it have a full chapter explaining the allocator philosophy on STL and the implementation of a custom allocator for efficient use with small objects on the heap.
Check his site: http://www.moderncppdesign.com
and the source code of Loki(c++ advanced library), that includes the custom allocator, a note here, VC6++ neither VS. NET fully compiles this library due to lack of support of pts(partial template specialization), The sp1 of VS.net will compile this
loki source code
Cheers,
Joao Vaz
|
|
|
|
|
Thank you for your advice, Joao.
Maybe I will buy the book. However, you say that custom block allocators are tricky. It appears to me that if I do my own allocation/reallocation I'm in full control. I don't do very fancy stuff on my variable arrays (e.g. sorting), it is mostly sequential access. But I do a very large number of iterative calculations on them, and I have a vague idea that if I keep my memory compact I keep these calculations run faster. The routine must constantly load new elements into the cache for computing, and the elements should therefore contain no dead meat. (Is this rubbish, or..?)For the same reason I will probably use CString (4 bytes long) instead of char m_name[32], but from where will the memory be allocated for the contents of the CString? My heap created with HeapCreate, or the default process heap? Is <string> any better?
There are certainly a very large number of difficult choises to make for us poor beginners....
Regards, Haakon
|
|
|
|
|
Haakon, CString is a MFC class , not a STL class, in STL we have the string template class.
The CString class internally will by default allocate the memory by a factor of 2, and internally maintains lists(not sure how many) of memory blocks , it controls the used memory blocks and unused memory blocks,
it guards the size and the nº of slots available for allocating in the used slots due to requests of different sizes, then when we ask to allocate something, it will search for the best size match on the slots, if it finds it uses the slot, if not it will request a unused block from another list and will allocate accordingly. This algorithm is used to prevent to minime memory fragmentation and improve allocattion time. A proper and much more correct and accurate explanation of this is on MFC Internals.
The stl string implememention is faster than the mfc counterpart, and it uses similar(or more complex) algorithms.
For articles talking about allocators check this articles, both from by Matt Austern, the chair of the C++ standardization committee’s library working group.
1º The Standard Librarian: What Are Allocators Good For?
2º
The Standard Librarian: A Debugging Allocator
After reading this, perhaps you will feel the wish for not doing custom allocators ...
A note, implementing a block allocator for vector it's more tricky, because the standars mandates that the elements can be accessed on contiguous memory blocks .
ex:
vector<100> x;
while (...) { doSomething(&v[i++]); }
For other example of custom block allocator see here at CodeProject
A Custom Block Allocator for Speeding Up VC++ STL
By Joaquín M López Muñoz
As a sidenote this allocator don't work with vector neither deque(this containers allocates chunks like I tried to explaining).
Cheers,
Joao Vaz
|
|
|
|
|
Damn...
I am a bit shocked that the STL list, map, and sets don't allocate nodes in buckets. Not only do you increase allocation performance, you increase memory performance by reducing the number of page faults. The theory being that when the map nodes are allocated in buckets, you greatly reduce the chance that the nodes are spread out all over the memory address space.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
Tim Smith wrote:
you greatly reduce the chance that the nodes are spread out all over the memory address space.
Yeap, it's true, but for that you have hash_map and other hash* that will included on the next standard and normally every STL library that I'm aware provides some sort of hash counterparts.
Of course it's a pitty, because of the lack of time the hash implementations didn't went incorporated on the standard , but oh well, for this, we have the possibility of breaking our heads with custom allocators. It's a shame that typedef templates don't exist on standard, because it's very ugly to deal with the rebind template hack for simulating a typedef template used in constructing a custom allocator ... Even after reading several times the chapter of Effective STL on allocators and Modern C++ Design, I fell nauseous(spelled correctly ?) ...
Sometimes I wish the STL could be made more simpler ...
Cheers,
Joao Vaz
|
|
|
|
|
Thank you, Joao.
I appreciate your help and will carefully read the articles you refer to, and try to figure out about allocators. Forget the spell checker, it's the content that matters. You come out clear and precise!
(In my previous reply I mentioned stl::string as an alternative to CString, but I enclosed it in brackets - that made it disappear from the text!)
Regards, Haakon.
|
|
|
|
|
Haakon S. wrote:
Forget the spell checker
90% of the time I use it, now the others 10% are a complete disgrace
Sorry for the errors , sometimes I write so fast and don't check my poor english Damn it, damn it ...
Okay, have fun reading the articles
Joao Vaz
|
|
|
|
|
Using buckets in maps really has nothing to do with hash. It is just a standard optimization. It really comes into play more when your maps are stored on disk. But, the same still holds true for memory maps.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
Sh*t, sorry Tim , I re-readed again your post and on the 1st try I misunderstanded it.
*Grin*
You are right about this, I don't understand either and I'm not a algorithm boy, the STL guys must had a reason to implement it without buckets ... now, the reason, I liked very much to known
Cheers,
Joao Vaz
|
|
|
|
|
If I use CAsyncSocket a method called OnClose is called when a connection is closed! okay, but if I don't use MFC to build a soccket app. How does it work then ? ?
hope you understand! short summary: I want an similar functionality in a non-MFC Winsock program to CAsyncSocket's OnClose!
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
maybe this will help.
You do a recv(), if it returns 0, it means the connection on the other side is closed, so you close your socket too.
|
|
|
|
|
Wee!
Tackar så mycket (thank you so much)!
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
Do you know what's the difference between checking and unchecking this option?
I mean, from the programmer's point of view.
It must be a change in getting messages form the message queue, because i
wrote an application which works perfectly when the option is enabled, and has
a strange behavior when it is disabled. It's about moving controls in a dialog window.
rechi
|
|
|
|
|
bogdan_rechi wrote:
Do you know what's the difference between checking and unchecking this option?
Pretty much the obvious: you get a lot more move and paint messages. If you depend on that granularity, i suppose something could break.
Developers that like shiny objects also dig case mods and scratch-and-sniff stickers. Klaus Probst, The Lounge
|
|
|
|
|
What is the difference between the dialog types available in resource editor?
In resource editor, when we select dialog and reght click ,we will get menu .From the menu,Select dialog.Then different types of dialogs will be listed if we select '+' sign infrontof the dialog.
I want to know the difference between them.
Any help would be grateful.
Be creative
Once your mind is streched by a new idea, it will never regain its original dimension
|
|
|
|
|
Nothing realy.....
they are just simple dialog resources.
only thing changed is the Child / Popup styles
|
|
|
|
|
Hi!
I work with database from MFC (Access).I want to send Sql command.
My function recieve parameter and I use variable type CString with format.
For example:
strSql.Format("SELECT COUNT([TblCatalogList].[CatalogId]) FROM [TblCatalogList] WHERE [TblCatalogList].[PcBoard] LIKE '%s%'",strBoard);
But it does not work and failed becouse unknown format.LIKE must have this format,for example
LIKE '123%' it means that I want to find string that begin from 123,but how must I write if I have a parameter???
Help me,please with syntax!!!
|
|
|
|
|
What does strSql contain after the format statement? I suspect from the example you gave that you'll be missing a percentage and quote sign. This is because the two characters "%'" will be removed from your string. To get a '%' character in a string you need to put two of them (%%):
strSql.Format("SELECT COUNT([TblCatalogList].[CatalogId]) FROM [TblCatalogList] WHERE [TblCatalogList].[PcBoard] LIKE '%s%%'",strBoard);
Hope this helps.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
To get a literal % sign you'll need to specify it as %% in the format string.
You know, for kids!
|
|
|
|
|
Hi.
I would like to enable and disable one or more toolbar *buttons*. According to what Prosise mentioned, it can be done using the Enable() that the framework passes to a menu/toolbar update message handler. However, I am not quite clear on how to disable a specific toolbar button. For example, let say I have a toolbar with five buttons.
[button1][button2][button3][button4][button5]
How would I enable/disable, say, button2?
Thanks,
Kuphryn
|
|
|
|
|
When you create each button in the toolbar, it should have an associated command ID. These IDs are passed in as an array in the CToolBar::SetButtons() command. If you create a toolbar in the resource editor (this is from memory, I don't normally do it this way) I think you just select one of the buttons on your toolbar and bring up the properties, then add a command ID there.
Anyway, this ID is the command ID used in the ON_UPDATE_COMMAND_UI update handler (ie you have one handler per toolbar button, not for the toolbar as a whole). If you have a range of toolbar buttons and you don't want to write code for all of them, you can also use ON_UPDATE_COMMAND_UI_RANGE.
So, in summary, each Update Command handler should apply to a single toolbar button.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
Okay. Thanks.
I am uncertain one something. Here is what I am trying to do. When the application first starts, I want to disable one or more buttons. When the user select a certain option via menu, then the program would enable the button. I need a way to get a hold of a pointer to the toolbar right when the application starts up so I can disable one or more buttons. It can be done with commandUI and commandUI_RANGE, but how about any other arbitrary time?
Kuphryn
|
|
|
|
|
Well, you can put your command handler anywhere that message are received. The best place to put them in this case would probably be the CFrameWnd-derived class where you create the toolbar. The same class would also have a handler for the menu item. For example, if the menu item is ID_ALLOW_BUTTON and the button itself is ID_MY_BUTTON:
In the header:
bool m_bButtonAllowed;
afx_msg void OnAllowButtonMenu();
afx_msg void OnUpdateMyButton(CCmdUI *pCmdUI);
afx_msg void OnMyButton();
In your constructor:
m_bButtonAllowed = false;
In the message map:
ON_COMMAND(ID_ALLOW_BUTTON, OnAllowButtonMenu)
ON_UPDATE_COMMAND_UI(ID_MY_BUTTON, OnUpdateMyButton)
ON_COMMAND(ID_MY_BUTTON, OnMyButton)
Then:
CMainFrame::OnAllowButtonMenu()
{
m_bButtonAllowed = true;
}
CMainFrame::OnUpdateMyButton(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_bButtonAllowed ? 1 : 0);
}
CMainFrame::OnMyButton()
{
AfxMessageBox("Got me!");
}
Hope this helps.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|