|
Stack memory is generally required for the function call information (return values and return address), as well as for the automatic variables in the function being called. One function may consume only 32 bytes of stack while another consumes more than 8KB of stack.
It is possible to recursively iterate over any size list... Given you have enough stack space and your environment supports it. Realistically, this depends on (1) how much stack is consumed by the recursive function, (2) how many elements are in the list, and (3) how much free stack space you have remaining at the time you start recursion (just because you have 1MB of stack by default in Win32, that does not mean that by the time your function is first called that you will have much of it left).
Iteration is generally better done using loops instead of recursion. It is also easier to debug, because you can more easily break at a certain loop counter than a certain recursion depth.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Thanks,
Here is another one.
I additionally have a bin tree with many thousands of elements.
I use recursive functions to walk through the leafs because its more intuitive.
Will it be faster if I use loops instead?
|
|
|
|
|
It may be faster to use loops, depending on the setup required for the recursive function call (stack frame setup, construction/destruction of any local objects, exception handling setup if used, etc.). If your function uses poor coding habits, like using a CString instead of a local buffer for simple formatting, using the loop will very likely be faster than recursion, because you can eliminate the overhead of allocating/deallocating the memory for the string. (You may not notice a difference when only calling the function 100 or so times, but when you start hitting the 1000+ ranges, you may.)
A (well-balanced) binary tree is likely to have less recursive demand, whereas a list is a linear structure so your recursion level would be equal to the number of items in the list. Trees tend to have less depth when used/populated correctly.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Thanks,Specifically I am working on a graphics algorithm which uses a binary tree
to draw triangles(which are the leafs) on the screen. Now if you consider that I would
like to have ~60000 leafs displayed every frame using a recursive function,
this is quite cumbersome.Additionally the tree may not be so balanced, but the maximum
depth of the tree wont be larger than ~14.Thats why I asked.
|
|
|
|
|
Each function call in a recursive loop consumes stack space. If you're passing parameters, that consumes even more stack space. I thought the max stack space for a given app was limited to 64k...
Of course, I'm old, so it may have changed since I last checked it out.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
In general, using recursion in pure imperative languages such as C++ is a bad idea - it can lead to stack overflows and also tends to be slower than iteration.
Functional languages such as OCaml are able to internaly convert recursion (at least tail recursion) to iteration and then it is OK to use recursion.
|
|
|
|
|
Well, I think there are definitely some very good uses of recursion in C++ such as generically printing out the data in an XML DOM tree, where there is no absolutely defined node structure. I'm not sure how you'd go about doing that otherwise. That said, DOM tree hierarchies typically don't go down that many levels -- that would be both a memory usage and usability nightmare.
|
|
|
|
|
Hi
I create new process (exe) with CreateProcess function.
now Iv got a handle (hProcess) and would like to know whether or not the process is alive?
|
|
|
|
|
Here's one way...
if (WAIT_OBJECT_0 != ::WaitForSingleObject(hProcess, 0))
{
// process is still active
}
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
|
Hello!
We have 2D array, Data[20][10], it contains some values, want some suggestions on how to count the distinct values in the array, without getting into 19 loops from 0 to 19 checking against each value!!!
We are using VC++ 6.
Thanks
Adeel
--
|
|
|
|
|
What is the range of your values? If it's not a huge range (more than 0-1000000), you can use a bucket sort technique, which is to basically initialize an array of bools of size 1000000, and set all initial values to false (I'm not sure what values they default to). Then, iterate through all the data in your 2D array and for each value, check if the bool array entry corresponding to the value is true. If it is, then you know you have duplicate values. If it's false, then set the bool array entry to true, so next time, it will detect a duplicate value.
Example: If the current iteration of your 2D array gives you a value of 5, you set boolArray[5] = true.
If you wish to count the values, then you can use an int array instead of a bool array and just increment as you go.
|
|
|
|
|
madeelch1986 wrote: We have 2D array, Data[20][10], it contains some values, want some suggestions on how to count the distinct values in the array
Stealing from Cyrilix, here's one way...
char Data[20][10];
bool Flags[256];
memset(Flags, 0, sizeof(Flags));
int UniqueCount = 0;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
if (false == Flags[(unsigned char)Data[i][j]])
{
Flags[(unsigned char)Data[i][j]] = true;
++UniqueCount;
}
}
}
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Completely newbie to this site (and Visual C++) so please be gentle if I am doing something wrong.
I am attempting to modify someone else's code and am not sure what I am doing wrong. The project setting indicated "not MFC" and "Win32 debug" which I didn't understand but couldn't find anything in the search. Without changing anything else, I just wanted to add a combo box with a list of items which can be selected. I dropped a combo box control on the dialog form and added 3 items to the data tab and recompile to see if the box displayed properly. The combo box is on the form but nothing is in the drop down list? What might I be doing wrong? I tried creating a new dialog project and the combo box is working fine. Any help would be appreciated.
David
|
|
|
|
|
dkyue92708 wrote: Completely newbie to this site (and Visual C++)
Why are you working on a production project if you are new to C++?
|
|
|
|
|
I am not working on any production project. I work with IP Security cameras and we provide sample C++ SDK to our customer as a courtesy and I am learning to support it. I am taking baby-steps and thought that it would be easy enough to add a simple combo box.
BTW, what would you recommend for me to 'come up to speed' quickly on C++/Visual C++?
|
|
|
|
|
dkyue92708 wrote: BTW, what would you recommend for me to 'come up to speed' quickly on C++/Visual C++?
That's a tough one. Everyone is different. Some people never "come up to speed". What is your background/experience?
|
|
|
|
|
I worked on embedded firmware maintenance for SCSI tape drives for two years using C, then moved to supporting test utilities for the same tape drives using C again. I briefly started learning C++ but seemed to have hard time with the OOP concept so I kinda know enough to be dangerous The trouble is I am in Sales Support now so I don't get to code very often but want to get back into it.
Anyway, do you have any suggestion for my original question as to why the data list for my combo box is not showing up (drop down box is empty)?
|
|
|
|
|
I believe Iain is correct but not having done any GUI stuff without MFC in... well a long time, I am not sure.
|
|
|
|
|
dkyue92708 wrote: "not MFC" and "Win32 debug" which I didn't understand but couldn't find anything in the search.
"not MFC" means just that. The project does not use MFC in its code. "Win32" refers to the target (as opposed to something like ATL) and "debug" means the project is compiled and linked with debugging support. "Release" doesn't include debug stuff like symbol information and optimizes the code.
dkyue92708 wrote: I dropped a combo box control on the dialog form and added 3 items to the data tab and recompile to see if the box displayed properly. The combo box is on the form but nothing is in the drop down list?
Did you actually drop the list down or just assume nothing is in it since nothing is displayed in the visible portion? If the data is there when you do open the drop-list, you need to select an item in your initialization in order to see data in the edit area upon startup.
Judy
|
|
|
|
|
Thanks for the explanation. I need to read more.
On the combo box, I actually clicked on the drop-down arrow and the box opened up but nothing is in it. I drop the same combo box in a new project (MFC AppWizard) and the list does show up. Thanks!
|
|
|
|
|
dkyue92708 wrote: The combo box is on the form but nothing is in the drop down list?
That's because you need to increase the height of the combobox. By default, it's too small.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for the feedback but that's not it. I left-click on the combo box control on the form and adjusted the size already. When I click on the drop-down arrow on the form, a drop-down box does appear that is large enough to fit the 3 items I specified in the property data sheet but it is blank.
|
|
|
|
|
With VS6, comboboxes have two separate vertical sizing methods. One sets the height of an unexpanded control, while the other sets the height of an expanded control. If you are using VS200x, I'm not sure how it behaves.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The items in the data tab are stored as a specific resource type. When a dialog is created using MFC, one of the jobs of CDialog::OnInitDialog is to go looking for this specific resource type, and populate the combo box for you.
It may be in OnCreate (in hindsight I'm pretty sure it is), but that doesn't really change anything here.
As you're not using MFC, no code ever goes looking for the data in the resources to add to your combo box, so it remains empty.
To recap: The data tab in the resource editor is only of use if you're using MFC. You're not.
In your DlgProc for that dialog, you'll need to handle WM_INITDIALOG and add the data there. Whether you hard code the values, read them from string resources, do some digging and write a function to replicate what MFC does is up to you.
Good luck,
Iain.
|
|
|
|