Results 1 to 9 of 9

Thread: C++ Create an Array in a loop

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    C++ Create an Array in a loop

    I have a loop - and within that loop I want to create an array.

    Code:
    for(k = 0; k < nKeywordskt; k++) {
    	if (flags[k] & bitFw) {
    		x = rwslot[k] & 65535;
    		y = (x * 2);
    		fwMapOff = fwmap[y];							// Offset into fw2list
    		fwMapLen = fwmap[y + 1];						// Length of run in fw2list
    		int fw2On[fwMapLen];		// **** this is the array I want to "re-create" within the loop ****
    		for (x = fwMapOff; x < (fwMapOff + fwMapLen); x++) 
    		{
    			z = fw2list[x];							// This is the fw2Rules slot to run for the FW found on this WORD [k]
    Each time I hit the line of code int fw2On[fwMapLen]; I want to re-create the array fw2On[] with the size fwMapLen.

    This size - fwMapLen - will be different each time I loop through the k-loop.

    Is this kind of re-dimension allowed?

    Do I have to "set each" spot to 0 - or is it pre-initialized each time?

    Is it bad for me to do this - should I "know" the max fwMapLen size before I start looping and make my "work array fw2On[]" only once - at the beginning??

    Help!!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: C++ Create an Array in a loop

    Actually - that code doesn't fly at all - can't create an array with a variable length - I guess.

    I actually need just BIT's for ON/OFF state of each "item" in the x-loop - what kind of data structure could I use for this??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: C++ Create an Array in a loop

    Does what you currently have compile? I would have assumed that it doesnt, since array declaration requires a constant expression, and fwMapLen seems to be an ordinary variable.
    The reason for needing a constant expression is that the fw2on array will be declared on the stack, and its size must be known beforehand.
    So basically what you will need to do is allocate space for your array on the heap;
    Code:
    int *fw2on = new int[fwMapLen];
    Deallocate arrays by using delete[]:
    Code:
    delete[] fw2on;
    But allocation is relatively expensive, so if performance is of any concern, you would be better of allocation as much space as you will ever happen to need for fw2on once and then never re-allocate. (IF you happen to know the maximum value that fwMapLen can take on).

    Depending on what you are doing with the fw2on array later on, the std::vector<int> class could be a good alternative too. It behaves very similarly to the List<T> in .NET, holding an internal array that it expands fairly efficiently when needed.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: C++ Create an Array in a loop

    Thanks for the info.

    This code - int *fw2on = new int[fwMapLen]; - what makes it use the HEAP in that syntax? The NEW? Or the "*" in front of fw2on??

    Could I allocate a BIT array on the HEAP with new bit[fwMapLen]?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: C++ Create an Array in a loop

    I am calling this C++ function from VB.Net - so I am able to create an ARRAY on the VB side prior to calling the C++ and I'll just use that. Since I need to know on/off states - I'm creating an INTEGER array so that each slot in the array handles 32-bits of on/off's.

    I've got this so far

    Code:
    ' In VB.Net
    Dim rFWMap2Max As Integer = m_dcxE.PreceptFWMap2Max
    Dim rFW2On(CInt((rFWMap2Max + 31) / 32)) As Integer
    .
    .
    .
    y = findHyperWords2(allText, allText.Length..., rFW2On...)
    Code:
    // In C++
    
    __declspec(dllexport) int findHyperWords2(WCHAR *strSearch, int lenSearch..., int *fw2On
                                                                                                              ...) {
    .
    .
    .
    for(k = 0; k < nKeywordskt; k++) {
    	if (flags[k] & bitFw) {
    		x = rwslot[k] & 65535;
    		y = (x * 2);
    		fwMapOff = fwmap[y];								// Offset into fw2list
    		fwMapLen = fwmap[y + 1];							// Length of run in fw2list
    		for (x = 0; x < int(((fwMapLen + 31) / 32)); x++)
    		{
    			fw2On[x] = 0;	// *** Clear up bits in the INTEGER array - one slot for each 32 bits needed
    		}
    		for (x = fwMapOff; x < (fwMapOff + fwMapLen); x++) 
    		{
    			z = fw2list[x];							// This is the fw2Rules slot to run for the FW found on this WORD [k]
    Basically creating an INTEGER array - one slot for each of 32 bits required - and passing that into the C++ function.

    In C++ - in my loop - I clear up at least the number of "slots" I need for the size of my current max in the loop.

    I think I'm off by one on the creation in VB (too large) - and probably need to bury some of this in another C++ helper function - so I don't have to keep worrying about "ferrying" my "x-loop" value into an offset and bit-spot in my new fw2On[] array.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: C++ Create an Array in a loop

    Quote Originally Posted by szlamany View Post
    Thanks for the info.

    This code - int *fw2on = new int[fwMapLen]; - what makes it use the HEAP in that syntax? The NEW? Or the "*" in front of fw2on??

    Could I allocate a BIT array on the HEAP with new bit[fwMapLen]?
    The new keyword is what does it. Everything you declare in a function gets allocated on a stack. So the pointer fw2on is declared on the stack, but the memory it points to is allocated at runtime on the heap.
    The new keyword not only allocates the necessary memory on the stack but invokes constructors (if any), initializes vtables, among other things.

    There are no smaller datatypes than char in C++, but there are classes that lets us do work on bit-level and hide the fact that it is using larger datatypes underneath, take a look at the std::bitset class.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: C++ Create an Array in a loop

    I'm curious - would you always chose a standard class like the std::bitset or would you tend to code-your-own-solution?

    I'm new to this C++ stuff - but I'm kind of thinking I want to build up my own library of helper functions for requirements like this.

    Am I nuts?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: C++ Create an Array in a loop

    I actually enjoy rolling my own implementations every now and then, depending on what needs to be done.

    Implementations given to you by, for instance, the standard template library, are written to be as general as possible to fit as many needs as possible. Generality often comes with a cost, and there is a chance that you might be able to write something that is more specific to your problem, and gain a performance boost. Be it lowered memory usage or simply "faster code".
    There is also the enjoyment of writing things yourself and beating a complex task. If we never reinvented the wheel, how could we ever come up with something better?

    That said, the advantages of sticking to either the STL or any other large cross-platform library such as boost is that they are well-tested, well-designed, cross-platform implementations. It makes it easier for other developers to read and understand your code.

    Is this project of yours a work-related project or is it something you are doing on your free time? I would usually avoid writing my own implementations of what already exists in STL (or .NET if its that kind of project) on projects at work.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: C++ Create an Array in a loop

    Work-related or free time? Interesting question!

    I'm self-employed and this is for a project I'm working on with two partners (who are doing the marketing and trying to get funding part of the project).

    I've already written my own Shell sort - found the standard sort implementations did not work with my "pointer" sort-array. Had to go digging back through the forums here for code I posted from mainframe programs I wrote in the 1980's to find trust-worthy shell sort logic. I think I want to "own" my bit-setting functions - I'm currently coding up declareBitArray and setBitArray functions.

    Been working on this project for 18 months now - done with prototypes and now working on the first alpha-version for testing at customer sites.
    Last edited by szlamany; Nov 4th, 2012 at 07:42 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width